-
Notifications
You must be signed in to change notification settings - Fork 0
Logging
The first thing you need to do is to get the Logger for your class:
private static final Logger LOGGER = LogManager.getLogger(YourClass.class);
It is important to pass the actual class object into the logger, so make sure to replace YourClass
with the name of the class you're working on.
Afterwards you can log messages by simply doing this:
LOGGER.debug("I'm a log message!");
Instead of debug there are other levels of log messages. The levels are in ascending order:
- trace for fine-grained debug messages
- debug for general debug messages
- info for informational messages that may be interesting to the user
- warn in case something went wrong but the problem may be solved by the application itself
- error in case something went wrong
- fatal in case something went wrong and the application won't be able to continue working
It is a bad idea to use String concatenation ("Working on object" + someObject
) as in that case the result of the concatenation will always be calculated taking up CPU time even if the log message will be discarded. Instead the objectes can be passed as additional parameters:
LOGGER.trace("Working on object {}", someObject);
The {}
will be replaced with the value of toString()
on each of the additional parameters in order. If you need to log something that has to be calculated specifically for the log message, wrap the call to the logger in a matching if statement:
if (LOGGER.isInfoEnabled()) {
LOGGER.info("{} of {} jobs finished", getFinishedJobCount(), allJobs.size());
}
That way the calculations will only be done if the message will be logged.
If you need to follow the flow of methods there are special methods:
public double someMethod(int foo, String bar) {
LOGGER.entry(foo, bar);
// code
LOGGER.exit(result);
return result;
}
entry()
is used to mark the entry point of a method and should be the first statement of the method. Pass all of the method's parameters to entry()
so that they get logged too. exit()
should be called before each return statement and the end of the method. If your method has a return value, pass it to exit()
to log it. Statements like return someComputation();
should be transformed to
final ResultType result = someComputation();
LOGGER.exit(result);
return result;
to avoid doing expensive calculations multiple times.
Which messages get logged is set in log4j2.xml
. The default is to log all messages of level INFO or higher for the package edu.teco.dnd
and WARN or higher for all other packages. If you want to increase or decrease the verbosity for a package or a class add a logger block:
<logger name="edu.teco.dnd" level="INFO" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="edu.teco.dnd.mypackage.MyClass" level="ALL" additivity="false">
<appender-ref ref="Console"/>
</logger>
<root level="WARN">
<appender-ref ref="Console"/>
</root>
Instead of edu.teco.dnd.mypackage.MyClass
put the full name of your class or of a packge. Replace the level with the level of logging you want for the class/package. ALL is a special level that can only be used in the configuration, not for sending a log message. Besides of ALL the levels listed above can be used.
If you want to see trace messages but not the entry()
/exit()
messages you can add a filter:
<logger name="edu.teco.dnd.mypackage.MyClass" level="ALL" additivity="false">
<MarkerFilter marker="FLOW" onMatch="DENY" onMismatch="NEUTRAL"/>
<appender-ref ref="Console"/>
</logger>