Project

General

Profile

Logging guidelines » History » Version 2

Tobias Wich, 07/25/2012 11:31 AM

1 1 Moritz Horsch
h1. Logging guidelines
2
3
{{toc}}
4
5
h2. Framework
6
7
h3. SLF4J (Simple Logging Facade for Java)
8
9
"Documentation":http://www.slf4j.org/docs.html
10
11
h3. Maven
12
13 2 Tobias Wich
No dependency SHOULD be added to the module POM. The slf4j dependency is already defined in the ecard client root POM.
14
slf4j needs a backend implementation. For the tests, logback-classic is pulled in by the root POM. For production use, the client artifact must pull in a compile dependency on a backend and configure it appropriately.
15 1 Moritz Horsch
16
h3. Preamble
17
18
<pre>
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21
22
private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class);
23
</pre>
24
25
h3. SLF4J with java.util.logging backend
26
27 2 Tobias Wich
*TODO:* rewrite for logback-classic
28 1 Moritz Horsch
29 2 Tobias Wich
This code SHOULD only be using in TestNG.
30
31 1 Moritz Horsch
<pre>
32
// Reset configuration to avoid multiple log entries.
33
java.util.logging.LogManager.getLogManager().reset();
34
// Add new console handler
35
ConsoleHandler ch = new ConsoleHandler();
36
ch.setLevel(Level.ALL);
37
// Set logging level
38
LogManager.getLogger(MYCLASS.class.getName()).setLevel(Level.FINE);
39
40
// Log message as jdk logging level INFO
41 2 Tobias Wich
logger.info("Log my message: {}", message);
42 1 Moritz Horsch
// Log message as jdk logging level FINE
43 2 Tobias Wich
logger.debug("Log my message: {}", message);
44 1 Moritz Horsch
// Log message as jdk logging level WARNING
45 2 Tobias Wich
logger.warn("Log my message: {}", message);
46 1 Moritz Horsch
// Log message as jdk logging level SEVERE
47 2 Tobias Wich
logger.error("Log my message: {}", message);
48 1 Moritz Horsch
</pre>
49
50
51
52
h2. Levels
53
54
h3. Exceptions
55
56
<pre>
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59
60
private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class);
61
62
try{
63
 ...
64
} catch (Exception e){
65 2 Tobias Wich
  logger.error(ex.getMessage(), ex);
66 1 Moritz Horsch
}
67
</pre>
68 2 Tobias Wich
Or set a descriptive message:
69
<pre>
70
try{
71
 ...
72
} catch (Exception e){
73
  logger.error("Error while reading value X.", ex);
74
}
75
</pre>
76 1 Moritz Horsch
77 2 Tobias Wich
78 1 Moritz Horsch
h3. Messages
79
80
Messages including  APDUs, eCard-API messages, PAOS messages.
81
82
<pre>
83
import org.slf4j.Logger;
84
import org.slf4j.LoggerFactory;
85
86
private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class);
87
88 2 Tobias Wich
logger.debug("Message received:\n{}", message);
89
logger.debug("Message sent:\n{}", message);
90 1 Moritz Horsch
</pre>
91
92
h3. Events
93
94
E.g. GUI events.
95
96
<pre>
97
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
99
100
private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class);
101
102 2 Tobias Wich
logger.debug("Event: {}", event);
103 1 Moritz Horsch
</pre>
104
105
h2. Notes
106
107 2 Tobias Wich
h3. Trace statements as the following example shows are omitted, because there is a "slf4j agent":http://www.slf4j.org/extensions.html#javaagent which can automate this task.
108 1 Moritz Horsch
109
<pre>
110 2 Tobias Wich
public void foo(Bar b) {
111
    logger.trace("Enter function foo: {}", b);
112
    ...
113 1 Moritz Horsch
</pre>