简体   繁体   中英

How to separate web application logging in Tomcat

I have my webapp some-app.war running under Tomcat 8.5. I use Slf4j and Logback for logging within the webapp. When I log using log.info() or similar, the output gets written to catalina.out , the same log file as Tomcat uses for its internal logging. I want the logs from my application to be written to a separate log file. But I do NOT want to use logback's FileAppender to do it. I'd prefer to leave Logback using ConsoleAppender as I may deliver the WAR to others who won't have the same logging needs. I see the location of the log file as an aspect of the deployment environment, not the code.

Is there a way that I can manage per-application log files from the configuration of the Tomcat server itself -- while still using Slf4j and Logback in my code?

You can't know about others' logging needs, so there's not a silver bullet solution for this. However I'm sure the best option is not "I'm gonna leave it configured really badly because someone might log differently".

You can configure it how you wish for yourself, using FileAppenders and document the necessary steps to reconfigure it (eg edit the war).

You can also use more advanced configurations such as making use of syslog. Now other users have a standardized configuration option, just as long as they use syslog.

There are other options as well, but the common thread here is that you can't know what others may want, so don't waste time trying to guess it. Just do what works well for you at least.

If you placed logging.properties at [CATALINA_HOME/conf/logging.properties] then it should use it as your root logging properties file. Put you logging.properties or log.xml file in your application at [MyApp/WEB_INF/classes/logging.properties] and configure it like blow.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
  xmlns:log4j='http://jakarta.apache.org/log4j/'>

    <appender name="stderr" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
        <param name="Target" value="System.err"/> 
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
        <param name="Target" value="System.out"/> 
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>


    <appender name="file" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true" />
        <param name="maxFileSize" value="2MB" />
        <param name="maxBackupIndex" value="100" />
        <param name="file" value="${catalina.base}/logs/MyApp/MyApp.log" />
        <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
        </layout>
    </appender>

    <root>
        <level value="DEBUG" />
        <appender-ref ref="stdout" />
        <appender-ref ref="stderr" />
        <appender-ref ref="file" />
    </root>

</log4j:configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM