You are here: Advanced Features > Diagnostics > Diagnostic Messages Filter

Diagnostic Messages Filter

The standard listeners can potentially produce quite a lot of messages. By writing your own DiagnosticListener you can filter that information.

On the stage of application tuning you can be interested in optimizing performance through indexing. Diagnostics can help you with giving information about queries that are running on un-indexed fields. By having this information you can decide which queries are frequent and heavy and should be indexed, and which have little performance impact and do not need an index. Field indexes dramatically improve query performance but they may considerably reduce storage and update performance.

In order to get rid of all unnecessary diagnostic information and concentrate on indexes let's create special diagnostic listener:

private static class DiagnosticFilter implements DiagnosticListener{
    private final Set<Class> filterFor;
    private final DiagnosticListener delegate;

    private DiagnosticFilter(DiagnosticListener delegate,Class<? extends Diagnostic>...filterFor) {
        this.delegate = delegate;
        this.filterFor = new HashSet<Class>(Arrays.asList(filterFor));
    }

    public void onDiagnostic(Diagnostic diagnostic) {
        Class<?> type = diagnostic.getClass();
        if(filterFor.contains(type)){
            delegate.onDiagnostic(diagnostic);
        }
    }
}
DiagnosticsExamples.java: A simple message filter

After that we can use the filter-listener. It takes two arguments. The first one is a regular listener, the second is a list of all messages which are passed through.

EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
configuration.common().diagnostic()
        .addListener(new DiagnosticFilter(new DiagnosticToConsole(), LoadedFromClassIndex.class));
DiagnosticsExamples.java: Filter for unindexed fields