Actions
Logging guidelines » History » Revision 3
« Previous |
Revision 3/4
(diff)
| Next »
Tobias Wich, 09/14/2012 04:17 PM
Logging guidelines¶
Framework¶
SLF4J (Simple Logging Facade for Java)¶
Maven¶
No dependency SHOULD be added to the module POM. The slf4j dependency is already defined in the ecard client root POM.
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.
Preamble¶
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class);
SLF4J with java.util.logging backend¶
TODO: rewrite for logback-classic
This code SHOULD only be using in TestNG.
// Reset configuration to avoid multiple log entries. java.util.logging.LogManager.getLogManager().reset(); // Add new console handler ConsoleHandler ch = new ConsoleHandler(); ch.setLevel(Level.ALL); // Set logging level LogManager.getLogger(MYCLASS.class.getName()).setLevel(Level.FINE); // Log message as jdk logging level INFO logger.info("Log my message: {}", message); // Log message as jdk logging level FINE logger.debug("Log my message: {}", message); // Log message as jdk logging level WARNING logger.warn("Log my message: {}", message); // Log message as jdk logging level SEVERE logger.error("Log my message: {}", message);
Levels¶
Exceptions¶
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); try{ ... } catch (Exception e){ logger.error(ex.getMessage(), ex); }
Or set a descriptive message:
try{ ... } catch (Exception e){ logger.error("Error while reading value X.", ex); }
Messages¶
Messages including APDUs, eCard-API messages, PAOS messages.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); logger.debug("Message received:\n{}", message); logger.debug("Message sent:\n{}", message);
Events¶
E.g. GUI events.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(MYCLASS.class); logger.debug("Event: {}", event);
Notes¶
Trace statements as the following example shows are omitted, because there is a slf4j agent which can automate this task.¶
public void foo(Bar b) { logger.trace("Enter function foo: {}", b); ...
Updated by Tobias Wich over 12 years ago · 3 revisions