Home » Java » Enterprise Java » Forcing Tomcat to log through SLF4J/Logback

About Tomasz Nurkiewicz

Tomasz Nurkiewicz
Java EE developer, Scala enthusiast. Enjoying data analysis and visualization. Strongly believes in the power of testing and automation.

Forcing Tomcat to log through SLF4J/Logback

So you have your executable web application in JAR with bundled Tomcat (make sure to read that one first). However there are these annoying Tomcat logs at the beginning, independent from our application logs and not customizable:

Nov 24, 2012 11:44:02 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 24, 2012 11:44:02 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Nov 24, 2012 11:44:02 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.30
Nov 24, 2012 11:44:05 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

I would really like to quite them down, or even better save them somewhere since they sometimes reveal important failures. But I definitely don’t want to have a separate java.util.logging configuration. Did you wonder after reading the previous article how did I knew that runnable Tomcat JAR supports –httpPort parameter and few others? Well, I checked the sources, but it’s simpler to just ask for help:

$ java -jar target/standalone.jar -help
usage: java -jar [path to your exec war jar]
-ajpPort <ajpPort> ajp port to use
-clientAuth enable client authentication for
-D <arg> key=value
-extractDirectory <extractDirectory> path to extract war content,
default value: .extract
-h,--help help
-httpPort <httpPort> http port to use
-httpProtocol <httpProtocol> http protocol to use: HTTP/1.1 or
-httpsPort <httpsPort> https port to use
-keyAlias <keyAlias> alias from keystore for ssl
-loggerName <loggerName> logger to use: slf4j to use slf4j
bridge on top of jul
-obfuscate <password> obfuscate the password and exit
-resetExtract clean previous extract directory
-serverXmlPath <serverXmlPath> server.xml to use, optional
-uriEncoding <uriEncoding> connector uriEncoding default
-X,--debug debug

The -loggerName parameter looks quite promising. First try:

$ java -jar target/standalone.jar -loggerName slf4j
WARNING: issue configuring slf4j jul bridge, skip it

No good. Quick look at the source code again and it turns out that SLF4J library is missing. Since this parameter is interpreted during Tomcat bootstrapping (way before web application is deployed), slf4j-api.jar inside my web application is not enough, it has to be available for root class loader (equivalent to /lib directory in packaged Tomcat). Luckily plugin exposes <extraDependencies/> configuration parameter:


Running Tomcat and… success!

00:01:27.110 [main] INFO o.a.coyote.http11.Http11Protocol - Initializing ProtocolHandler ["http-bio-8080"]
00:01:27.127 [main] INFO o.a.catalina.core.StandardService - Starting service Tomcat
00:01:27.128 [main] INFO o.a.catalina.core.StandardEngine - Starting Servlet Engine: Apache Tomcat/7.0.33
00:01:29.645 [main] INFO o.a.coyote.http11.Http11Protocol - Starting ProtocolHandler ["http-bio-8080"]

Well, not quite. If you use Logback on a daily basis you are familiar with default console logging pattern. We are not picking up any logback.xml. From my experience it seems that placing logback.xml externally somewhere in your file system is superior to putting it inside your binary, especially with auto refreshing feature turned on:

<configuration scan="true" scanPeriod="5 seconds">
<!-- ... -->

Put some fallback logback.xml file in the root of your CLASSPATH in case no other file was specified like below and voilà:

$ java -jar standalone.jar -httpPort=8081 -loggerName=slf4j \

Finally, clean and consistent logging, most likely to a single file.

Reference: Forcing Tomcat to log through SLF4J/Logback from our JCG partner Tomasz Nurkiewicz at the Java and neighbourhood blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
Tom Wgalica Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

Is there any way to force tomcat to create separate log file for each wepapp?

Tom W

Cool, good tip! While I don’t have a standalone Tomcat product at the moment, this is definitely useful info for the future. Getting logging right is nice :)