You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Dmitri Pissarenko <dm...@gmail.com> on 2011/05/06 17:56:08 UTC

Logging with slf4j in Tomcat 7

Hello!

In my application I use slf4j for logging.

The logging messages appear in the console output like

17:05:31.985 ["http-bio-8080"-exec-1] DEBUG a.s.p.i.p.DefaultPersistence -
013.008: Daily plans (start)
17:05:31.985 ["http-bio-8080"-exec-1] DEBUG a.s.p.i.p.DefaultPersistence -
session: null
17:05:31.986 ["http-bio-8080"-exec-1] ERROR a.s.p.i.p.DefaultPersistence -
013.009

How can I configure Tomcat so that these log messages appear not only in
console output, but also in a log file?

Thanks in advance

Dmitri

Re: Logging with slf4j in Tomcat 7

Posted by chris derham <ch...@derham.me.uk>.
> In my application I use slf4j for logging.
>
> The logging messages appear in the console output like
>
> 17:05:31.985 ["http-bio-8080"-exec-1] DEBUG a.s.p.i.p.DefaultPersistence -
> 013.008: Daily plans (start)
> 17:05:31.985 ["http-bio-8080"-exec-1] DEBUG a.s.p.i.p.DefaultPersistence -
> session: null
> 17:05:31.986 ["http-bio-8080"-exec-1] ERROR a.s.p.i.p.DefaultPersistence -
> 013.009
>
> How can I configure Tomcat so that these log messages appear not only in
> console output, but also in a log file?
>
> This isn't a tomcat question - more a question on slf4j. You need to have
say slf4j-log4j12-X.Y.Z.jar packaged up in your. This will send slf4j
messages to log4j. Then configure log4j to send log messages to a file e.g.
create a file log4j.xml with something like this

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="FILE" class="org.apache.log4j.FileAppender">
        <param name="Threshold" value="DEBUG" />
        <param name="File" value="${catalina.base}/logs/yourApplication.log"
/>
        <param name="Append" value="false" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %X{user} %-5p [%c]
%m%n" />
        </layout>
    </appender>

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%p %X{user} -
%C{1}.%M(%L) | %m%n"/>
        </layout>
    </appender>

    <logger name="your.company.package">
        <level value="DEBUG"/>
    </logger>

    <root>
        <level value="DEBUG"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </root>
</log4j:configuration>

HTH

Chris