Logging guidelines » History » Version 3
Tobias Wich, 09/14/2012 04:17 PM
| 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 | h3. SLF4J with java.util.logging backend |
||
| 24 | |||
| 25 | 2 | Tobias Wich | *TODO:* rewrite for logback-classic |
| 26 | 1 | Moritz Horsch | |
| 27 | 2 | Tobias Wich | This code SHOULD only be using in TestNG. |
| 28 | |||
| 29 | 1 | Moritz Horsch | <pre> |
| 30 | // Reset configuration to avoid multiple log entries. |
||
| 31 | java.util.logging.LogManager.getLogManager().reset(); |
||
| 32 | // Add new console handler |
||
| 33 | ConsoleHandler ch = new ConsoleHandler(); |
||
| 34 | ch.setLevel(Level.ALL); |
||
| 35 | // Set logging level |
||
| 36 | LogManager.getLogger(MYCLASS.class.getName()).setLevel(Level.FINE); |
||
| 37 | |||
| 38 | // Log message as jdk logging level INFO |
||
| 39 | 2 | Tobias Wich | logger.info("Log my message: {}", message); |
| 40 | 1 | Moritz Horsch | // Log message as jdk logging level FINE |
| 41 | 2 | Tobias Wich | logger.debug("Log my message: {}", message); |
| 42 | 1 | Moritz Horsch | // Log message as jdk logging level WARNING |
| 43 | 2 | Tobias Wich | logger.warn("Log my message: {}", message); |
| 44 | 1 | Moritz Horsch | // Log message as jdk logging level SEVERE |
| 45 | 2 | Tobias Wich | logger.error("Log my message: {}", message); |
| 46 | 1 | Moritz Horsch | </pre> |
| 47 | |||
| 48 | |||
| 49 | |||
| 50 | h2. Levels |
||
| 51 | |||
| 52 | h3. Exceptions |
||
| 53 | |||
| 54 | <pre> |
||
| 55 | import org.slf4j.Logger; |
||
| 56 | import org.slf4j.LoggerFactory; |
||
| 57 | |||
| 58 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
| 59 | |||
| 60 | try{ |
||
| 61 | ... |
||
| 62 | } catch (Exception e){ |
||
| 63 | 2 | Tobias Wich | logger.error(ex.getMessage(), ex); |
| 64 | 1 | Moritz Horsch | } |
| 65 | </pre> |
||
| 66 | 2 | Tobias Wich | Or set a descriptive message: |
| 67 | <pre> |
||
| 68 | try{ |
||
| 69 | ... |
||
| 70 | } catch (Exception e){ |
||
| 71 | logger.error("Error while reading value X.", ex); |
||
| 72 | } |
||
| 73 | </pre> |
||
| 74 | 1 | Moritz Horsch | |
| 75 | 2 | Tobias Wich | |
| 76 | 1 | Moritz Horsch | h3. Messages |
| 77 | |||
| 78 | Messages including APDUs, eCard-API messages, PAOS messages. |
||
| 79 | |||
| 80 | <pre> |
||
| 81 | import org.slf4j.Logger; |
||
| 82 | import org.slf4j.LoggerFactory; |
||
| 83 | |||
| 84 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
| 85 | |||
| 86 | 2 | Tobias Wich | logger.debug("Message received:\n{}", message); |
| 87 | logger.debug("Message sent:\n{}", message); |
||
| 88 | 1 | Moritz Horsch | </pre> |
| 89 | |||
| 90 | h3. Events |
||
| 91 | |||
| 92 | E.g. GUI events. |
||
| 93 | |||
| 94 | <pre> |
||
| 95 | import org.slf4j.Logger; |
||
| 96 | import org.slf4j.LoggerFactory; |
||
| 97 | |||
| 98 | private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); |
||
| 99 | |||
| 100 | 2 | Tobias Wich | logger.debug("Event: {}", event); |
| 101 | 1 | Moritz Horsch | </pre> |
| 102 | |||
| 103 | h2. Notes |
||
| 104 | |||
| 105 | 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. |
| 106 | 1 | Moritz Horsch | |
| 107 | <pre> |
||
| 108 | 2 | Tobias Wich | public void foo(Bar b) { |
| 109 | logger.trace("Enter function foo: {}", b); |
||
| 110 | ... |
||
| 111 | 1 | Moritz Horsch | </pre> |