Java loggers levels

Here is a quick summary about Logback (and logging).
In your project, you’ll use slf4j, which is a facade, behind which you’ll plug a logging implementation.

Apache Jakarta Commons Logging (JCL) has memory leaks, so don’t use it.
Log4J has been deprecated by Logback, so don’t use it in new projects.
Logback is the logging API you should use.

The info which you (especially I) should learn by heart is the list of the different logging levels:

The set of possible levels, from highest to lowest:

TRACE,
DEBUG,
INFO,
WARN,
ERROR

if you put the level to INFO, everything from INFO down to ERROR will be displayed
if you put the level to DEBUG, everything from DEBUG down to ERROR will be displayed
etc.

When setting the logback.xml config file, just set the root logger on the warn level, or you’ll get tons of logs lines, too many.
Instead, override the root level only for your packages, say you have your code under package : com.mycompany

you use something like

  <logger name="com.mycompany" level="debug"/>

Here is a simple logback.xml config to get you started

<configuration>

<appender name="STDOUT">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.mycompany" level="debug"/>
<logger name="org.something.else" level="info"/>

<root level="warn">
<appender-ref ref="STDOUT" />
</root>

</configuration>
About these ads

One thought on “Java loggers levels

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s