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.
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 usesXMLFormatter
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:Another option would be to add configurations for file logging into tomcats existing
logging.properties
file located in$CATALINA_HOME/conf/logging.properties
: