Logging guidelines » History » Version 1
Moritz Horsch, 06/13/2012 11:20 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 | No dependency SHOULD be added to the module POM. The dependencies are already defined in the ecard client POM. |
||
14 | |||
15 | h3. Preamble |
||
16 | |||
17 | <pre> |
||
18 | import org.slf4j.Logger; |
||
19 | import org.slf4j.LoggerFactory; |
||
20 | |||
21 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
22 | </pre> |
||
23 | |||
24 | h3. SLF4J with java.util.logging backend |
||
25 | |||
26 | This code SHOULD only be using in JUnit tests. |
||
27 | |||
28 | <pre> |
||
29 | // Reset configuration to avoid multiple log entries. |
||
30 | java.util.logging.LogManager.getLogManager().reset(); |
||
31 | // Add new console handler |
||
32 | ConsoleHandler ch = new ConsoleHandler(); |
||
33 | ch.setLevel(Level.ALL); |
||
34 | // Set logging level |
||
35 | LogManager.getLogger(MYCLASS.class.getName()).setLevel(Level.FINE); |
||
36 | |||
37 | // Log message as jdk logging level INFO |
||
38 | logger.info(LoggingConstants.FINE, "Log my message: {}", message); |
||
39 | // Log message as jdk logging level FINE |
||
40 | logger.debug(LoggingConstants.FINE, "Log my message: {}", message); |
||
41 | // Log message as jdk logging level WARNING |
||
42 | logger.warn(LoggingConstants.FINE, "Log my message: {}", message); |
||
43 | // Log message as jdk logging level SEVERE |
||
44 | logger.error(LoggingConstants.FINE, "Log my message: {}", message); |
||
45 | </pre> |
||
46 | |||
47 | |||
48 | |||
49 | h2. Levels |
||
50 | |||
51 | h3. Overview |
||
52 | |||
53 | Marker are specified in @org.openecard.client.common.logging.LoggingConstants@ |
||
54 | |||
55 | * THROWING: [[Logging guidelines#Exceptions|Exceptions]] |
||
56 | * SEVERE: |
||
57 | * INFO: |
||
58 | * CONFIG: |
||
59 | * FINE: [[Logging guidelines#Exceptions|Messages]] |
||
60 | * FINER: [[Logging guidelines#Exceptions|Events]] |
||
61 | * FINEST: |
||
62 | |||
63 | h3. Exceptions |
||
64 | |||
65 | <pre> |
||
66 | import org.openecard.client.common.logging.LoggingConstants; |
||
67 | import org.slf4j.Logger; |
||
68 | import org.slf4j.LoggerFactory; |
||
69 | |||
70 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
71 | |||
72 | try{ |
||
73 | ... |
||
74 | } catch (Exception e){ |
||
75 | // <editor-fold defaultstate="collapsed" desc="log exception"> |
||
76 | logger.error(LoggingConstants.THROWING, "Exception", e); |
||
77 | // </editor-fold> |
||
78 | } |
||
79 | </pre> |
||
80 | |||
81 | h3. Messages |
||
82 | |||
83 | Messages including APDUs, eCard-API messages, PAOS messages. |
||
84 | |||
85 | <pre> |
||
86 | import org.openecard.client.common.logging.LoggingConstants; |
||
87 | import org.slf4j.Logger; |
||
88 | import org.slf4j.LoggerFactory; |
||
89 | |||
90 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
91 | |||
92 | // <editor-fold defaultstate="collapsed" desc="log message"> |
||
93 | logger.debug(LoggingConstants.FINE, "Message received:\n{}", message); |
||
94 | // </editor-fold> |
||
95 | |||
96 | // <editor-fold defaultstate="collapsed" desc="log message"> |
||
97 | logger.debug(LoggingConstants.FINE, "Message sent:\n{}", message); |
||
98 | // </editor-fold> |
||
99 | </pre> |
||
100 | |||
101 | h3. Events |
||
102 | |||
103 | E.g. GUI events. |
||
104 | |||
105 | <pre> |
||
106 | import org.openecard.client.common.logging.LoggingConstants; |
||
107 | import org.slf4j.Logger; |
||
108 | import org.slf4j.LoggerFactory; |
||
109 | |||
110 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
111 | |||
112 | // <editor-fold defaultstate="collapsed" desc="log event"> |
||
113 | logger.debug(LoggingConstants.FINER, "Event: {}", event); |
||
114 | // </editor-fold> |
||
115 | </pre> |
||
116 | |||
117 | h2. Notes |
||
118 | |||
119 | h3. Trace statements are omitted |
||
120 | |||
121 | <pre> |
||
122 | logger.trace("Trace a message: {}", message); |
||
123 | </pre> |
||
124 | |||
125 | Will be replaced by "slf4j extension":http://www.slf4j.org/extensions.html#javaagent. |
||
126 | |||
127 | h2. Examples |
||
128 | |||
129 | h3. APDU logging |
||
130 | |||
131 | <pre> |
||
132 | import org.openecard.client.common.ECardConstants; |
||
133 | import org.openecard.client.common.logging.LogManager; |
||
134 | |||
135 | java.util.logging.LogManager.getLogManager().reset(); |
||
136 | ConsoleHandler ch = new ConsoleHandler(); |
||
137 | ch.setLevel(Level.ALL); |
||
138 | LogManager.getLogger("org.openecard.client.ifd.scio.wrapper").addHandler(ch); |
||
139 | LogManager.getLogger("org.openecard.client.ifd.scio.wrapper").setLevel(Level.FINE); |
||
140 | </pre> |