Logging guidelines » History » Version 4
Hans-Martin Haase, 08/12/2015 08:50 AM
Remove section about logging in tests.
1 | 1 | Moritz Horsch | h1. Logging guidelines |
---|---|---|---|
2 | |||
3 | h2. Framework |
||
4 | |||
5 | h3. SLF4J (Simple Logging Facade for Java) |
||
6 | |||
7 | "Documentation":http://www.slf4j.org/docs.html |
||
8 | |||
9 | h3. Maven |
||
10 | |||
11 | 2 | Tobias Wich | No dependency SHOULD be added to the module POM. The slf4j dependency is already defined in the ecard client root POM. |
12 | 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. |
||
13 | 1 | Moritz Horsch | |
14 | h3. Preamble |
||
15 | |||
16 | <pre> |
||
17 | import org.slf4j.Logger; |
||
18 | import org.slf4j.LoggerFactory; |
||
19 | |||
20 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
21 | </pre> |
||
22 | |||
23 | h2. Levels |
||
24 | |||
25 | h3. Exceptions |
||
26 | |||
27 | <pre> |
||
28 | import org.slf4j.Logger; |
||
29 | import org.slf4j.LoggerFactory; |
||
30 | |||
31 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
32 | |||
33 | try{ |
||
34 | ... |
||
35 | } catch (Exception e){ |
||
36 | 2 | Tobias Wich | logger.error(ex.getMessage(), ex); |
37 | 1 | Moritz Horsch | } |
38 | </pre> |
||
39 | 2 | Tobias Wich | Or set a descriptive message: |
40 | <pre> |
||
41 | try{ |
||
42 | ... |
||
43 | } catch (Exception e){ |
||
44 | logger.error("Error while reading value X.", ex); |
||
45 | } |
||
46 | </pre> |
||
47 | 1 | Moritz Horsch | |
48 | 2 | Tobias Wich | |
49 | 1 | Moritz Horsch | h3. Messages |
50 | |||
51 | Messages including APDUs, eCard-API messages, PAOS messages. |
||
52 | |||
53 | <pre> |
||
54 | import org.slf4j.Logger; |
||
55 | import org.slf4j.LoggerFactory; |
||
56 | |||
57 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
58 | |||
59 | 2 | Tobias Wich | logger.debug("Message received:\n{}", message); |
60 | logger.debug("Message sent:\n{}", message); |
||
61 | 1 | Moritz Horsch | </pre> |
62 | |||
63 | h3. Events |
||
64 | |||
65 | E.g. GUI events. |
||
66 | |||
67 | <pre> |
||
68 | import org.slf4j.Logger; |
||
69 | import org.slf4j.LoggerFactory; |
||
70 | |||
71 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
72 | |||
73 | 2 | Tobias Wich | logger.debug("Event: {}", event); |
74 | 1 | Moritz Horsch | </pre> |
75 | |||
76 | h2. Notes |
||
77 | |||
78 | 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. |
79 | 1 | Moritz Horsch | |
80 | <pre> |
||
81 | 2 | Tobias Wich | public void foo(Bar b) { |
82 | logger.trace("Enter function foo: {}", b); |
||
83 | ... |
||
84 | 1 | Moritz Horsch | </pre> |