Spring Boot log files in standalone tomcat are sav

2019-08-29 09:33发布

So I've been wondering why tomcat in standalone mode saves log files in XML format. This did not happen when I deployed the Spring boot application in Tomcat embedded. I'm using logback-spring.xml for configuring logs. Even if I disable it, Tomcat still saves it in XML format. I even tried to override tomcats logging.properties file located in $CATALINA_HOME/conf.

This is the XML output I get:

<record>
   <date>2018-07-08T22:35:25</date>
   <millis>1531089325236</millis>
   <sequence>325</sequence>
   <logger>org.apache.catalina.startup.Catalina</logger>
   <level>INFO</level>
   <class>org.apache.catalina.startup.Catalina</class>
   <method>start</method>
   <thread>1</thread>
   <message>Server startup in 11151 ms</message>
</record>

This is my logging configuration inside application.properties:

#logging.config=classpath:/logback-spring.xml
logging.file=/tmp/app.log
logging.file-name=app
logging.archive.path=/var/tmp/app
logging.file.max-size=10MB
logging.file.max-history=30
logging.archive.compressor=gz
logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n

logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

And this is my logback-spring.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>
            ${FILE_LOG_PATTERN}
        </pattern>
    </encoder>
</appender>

<springProperty  name="LOG_ARCHIVE_PATH" source="logging.archive.path" />
<springProperty  name="LOG_FILE_NAME" source="logging.file-name" />
<springProperty  name="LOG_ARCHIVE_COMPRESSOR" source="logging.archive.compressor" />
<springProperty  name="PATTERN" source="logging.pattern.file" />

<appender name="LOGROTATE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_FILE}</file>

    <encoder>
        <pattern>${PATTERN}</pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <cleanHistoryOnStart>true</cleanHistoryOnStart>
        <fileNamePattern>${LOG_ARCHIVE_PATH}/${LOG_FILE_NAME}.%d{dd-MM-yyyy}_%i.log.${LOG_ARCHIVE_COMPRESSOR}</fileNamePattern>

        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>

        <maxHistory>${LOG_FILE_MAX_HISTORY}</maxHistory>

    </rollingPolicy>

</appender>


<springProfile name="dev">
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>
</springProfile>

<springProfile name="prod" >

    <root level="WARN">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="LOGROTATE" />
    </root>

</springProfile>

I am very certain, that the configurations on the spring side are Okay, since it worked in the embedded mode. Tomcat seems to be the culprit, but I am clueless on how to solve this issue.

1条回答
疯言疯语
2楼-- · 2019-08-29 10:05

After reading the tomcat documentation I found out, that tomcat falls back unto Java's default logging configuration, if it cannot resolve the tomcat logging.properties file location or certain properties. The default java logging configuration is located in $JAVA_HOME/lib/logging.properties and uses XMLFormatter for file logs. Spring seems unable to handle logging configuration in Tomcat's standalone mode. I could not find a way to use logback, but instead I overrode tomcat logging file. Below is an example:

handlers =java.util.logging.FileHandler,java.util.logging.ConsoleHandler
.level = INFO

# File Logging
java.util.logging.FileHandler.pattern = %h/logfilename%u.log
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.limit = 10485760
java.util.logging.FileHandler.count = 10

#java.util.logging.ConsoleHandler.formatter = org.springframework.boot.logging.java.SimpleFormatter
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level = INFO

org.hibernate.validator.internal.util.Version.level = WARNING
org.apache.coyote.http11.Http11NioProtocol.level = WARNING
org.apache.tomcat.util.net.NioSelectorPool.level = WARNING
org.apache.catalina.startup.DigesterFactory.level = SEVERE
org.apache.catalina.util.LifecycleBase.level = SEVERE

#java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
java.util.logging.SimpleFormatter.format=%d{dd-MM-yyyy HH:mm:ss.SSS} %magenta([%thread]) %highlight(%-5level) %logger{36}.%M - %msg%n

Another option would be to add configurations for file logging into tomcats existing logging.properties file located in $CATALINA_HOME/conf/logging.properties:

java.util.logging.FileHandler= java.util.logging.SimpleFormatter
查看更多
登录 后发表回答