Last updated: 18-December-2023
Tracelogger
Currently, there are two (overloaded) methods of the class TracerFactory which evaluate a given XML trace-config: readConfiguration(File configFile) and readConfiguration(InputStream inputStream). The latter one is well suited for configurations which are given as resources on the classpath. Both methods are validating their input against a XML schema which is referenced in the first section. The overall structure of the XML configuration is divided into four parts: the pool of named tracers, the default tracer (which must always be a NullTracer), a map which maps threads on named tracers and a queue of uniform tracers. These parts will be discussed in the sections 2-5.

(1) XML Schema

The applied XML schema is attached to the classpath as resource, see TraceConfigSchema.

(2) Pool

This section is mandatory for historical reasons, but its content might be empty. The uniquely named tracers together with their individual configurations belong into this part. A complete FileTracer configuration is shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8" ?>
<TraceConfig xmlns="http://www.christofreichardt.de/java/tracer">
<Pool>
...
<TraceLogger name="ExampleTracer" class="de.christofreichardt.diagnosis.file.FileTracer">
<LogDir>./log/examples</LogDir>
<AutoFlush>true</AutoFlush>
<BufSize>1024</BufSize>
<Limit>1048576</Limit>
<Context>
<Thread name="main">
<Online>true</Online>
<DebugLevel>5</DebugLevel>
</Thread>
</Context>
</TraceLogger>
...
</Pool>
</TraceConfig>
The specification of the Limit and the Context is optional. If no Limit is given the file size of the accompanying log file is unbounded. If no Context is specified you must initialise a Context with explicit values for DebugLevel and Online by applying initCurrentTracingContext(int debugLevel, boolean online) otherwise calling initCurrentTracingContext() is sufficient. A Context is always associated with a certain thread. A NetTracer needs no log directory but a listener address, see below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8" ?>
<TraceConfig xmlns="http://www.christofreichardt.de/java/tracer">
<Pool>
...
<TraceLogger name="TestTracer-2" class="de.christofreichardt.diagnosis.net.NetTracer">
<Listener>
<Port>1234</Port>
<Host>localhost</Host>
</Listener>
<AutoFlush>true</AutoFlush>
<BufSize>1024</BufSize>
<Context>
<Thread name="TestThread-2">
<Online>true</Online>
<DebugLevel>5</DebugLevel>
</Thread>
</Context>
</TraceLogger>
...
</Pool>
</TraceConfig>

top

(3) Default Tracer

The specification of a DefaultTracer is optional. If no DefaultTracer is given the JDKLoggingRouter is used. The default tracer is returned by the methods getCurrentPoolTracer() or getCurrentQueueTracer() of the class TracerFactory if no specified tracer can be found for the current thread or - in case of a QueueTracer - if the queue tracers are disabled. Only NullTracers are allowed, e.g.:
1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<TraceConfig xmlns="http://www.christofreichardt.de/java/tracer">
...
<DefaultTracer class="de.christofreichardt.diagnosis.LogbackRouter"/>
...
</TraceConfig>

top

(4) Map

Within this section you can map threads on pooled tracers so that you can easily request a tracer for the current thread by calling getCurrentPoolTracer() or getTracer(Thread thread). See, for example, the configuration below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?xml version="1.0" encoding="UTF-8"?>
<TraceConfig xmlns="http://www.christofreichardt.de/java/tracer">
<Pool>
<TraceLogger name="TestTracer-0" class="de.christofreichardt.diagnosis.file.FileTracer">
<LogDir>./log/</LogDir>
<AutoFlush>true</AutoFlush>
<BufSize>1024</BufSize>
<Limit>1048576</Limit>
<Context>
<Thread name="main">
<Online>true</Online>
<DebugLevel>5</DebugLevel>
</Thread>
<Thread name="TestThread-0">
<Online>true</Online>
<DebugLevel>3</DebugLevel>
</Thread>
<Thread name="TestThread-1">
<Online>false</Online>
<DebugLevel>2</DebugLevel>
</Thread>
</Context>
</TraceLogger>
<TraceLogger name="TestTracer-1" class="de.christofreichardt.diagnosis.file.FileTracer">
<LogDir>./log/</LogDir>
<AutoFlush>true</AutoFlush>
<BufSize>1024</BufSize>
<Context>
<Thread name="TestThread-1">
<Online>true</Online>
<DebugLevel>5</DebugLevel>
</Thread>
</Context>
</TraceLogger>
<TraceLogger name="TestTracer-2" class="de.christofreichardt.diagnosis.net.NetTracer">
<Listener>
<Port>1234</Port>
<Host>localhost</Host>
</Listener>
<AutoFlush>true</AutoFlush>
<BufSize>1024</BufSize>
<Context>
<Thread name="TestThread-2">
<Online>true</Online>
<DebugLevel>5</DebugLevel>
</Thread>
</Context>
</TraceLogger>
</Pool>
<Map>
<Threads>
<Thread name="TestThread-0">
<TraceLogger ref="TestTracer-0" />
</Thread>
<Thread name="TestThread-1">
<TraceLogger ref="TestTracer-1" />
</Thread>
<Thread name="TestThread-2">
<TraceLogger ref="TestTracer-2" />
</Thread>
<Thread name="main">
<TraceLogger ref="TestTracer-0" />
</Thread>
</Threads>
</Map>
</TraceConfig>
Given three pooled tracers, the main- and the TestThread-0-threads are mapped on the TestTracer-0, the TestThread-1-thread is mapped on the TestTracer-0 and the TestThread-2-thread is mapped on the TestTracer-2. E.g. calling getCurrentPoolTracer() from the main-thread or from TestThread-0 gives you the TestTracer-0.

top

(5) Queue

A blocking queue containing uniform tracers can be configured within this segment. By invoking takeTracer() you receive the next available tracer from the queue or the invocation blocks if the queue is empty because all tracers are currently allocated. This approach might be useful for debugging code running within an application server serving web requests. QueueTracers should not be used for long running tasks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<TraceConfig xmlns="http://www.christofreichardt.de/java/tracer">
...
<Queue>
<Enabled/>
<Size>5</Size>
<TraceLogger name="QueueTracer" class="de.christofreichardt.diagnosis.file.QueueFileTracer">
<LogDir>./log/queue</LogDir>
<AutoFlush>true</AutoFlush>
<BufSize>1024</BufSize>
</TraceLogger>
<Online>true</Online>
<DebugLevel>3</DebugLevel>
</Queue>
</TraceConfig>

top

imprint