0
votes

I have a Tomcat (8.5.23) server, to use uit with logback and slf4j, I have

  • installed logback (1.2.3)
  • installed slf4j (1.7.25)
  • added a logback.xml in {catalina.base}/conf
  • replaced the tomcat-juli.jar with th eone provided by logback
  • defined -Djuli-logback.configurationFile to point to {catalina.base}/conf/logback.xml.

The Tomcat code indeed logs through logback, and obeys to the configuration file.

My servlet uses slf4j. I do see the log output, but it doesn't match any formatting pattern defined in the logback.xml file (it seems to be a default) and does not comply to the level setting... In fact it behaves like the tomcat logging before I added the -Djuli-logback.configurationFile.

The config has:

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

<configuration debug="true">

    <property name="max.retention.days" value="60" />

    <appender name="CONSOLE" class="org.apache.juli.logging.ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level {%thread} [%logger{20}] : %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE-CATALINA" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/catalina.log</file>
        <append>true</append>
        <encoder>
            <charset>utf-8</charset>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level {%thread} [%logger{40}] : %msg%n</pattern>
        </encoder>
        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/archive/catalina-%d{yyyyMMdd}-%i.log.zip</fileNamePattern> 
            <maxHistory>${max.retention.days}</maxHistory>
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
            <maxFileSize>20MB</maxFileSize>
        </rollingPolicy>
    </appender>

    <appender name="FILE-SERVLET" class="org.apache.juli.logging.ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/servlet.log</file>
        <append>true</append>
        <encoder>
            <charset>utf-8</charset>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %logger{40} {%thread} %level : %msg%n</pattern>
        </encoder>
        <rollingPolicy class="org.apache.juli.logging.ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/archive/servlet-%d{yyyyMMdd}-%i.log.zip</fileNamePattern> 
            <maxHistory>${max.retention.days}</maxHistory>
            <maxFileSize>20MB</maxFileSize>
        </rollingPolicy>
    </appender>


    <logger name="com.mycompany" level="INFO" additivity="false">
        <appender-ref ref="FILE-SERVLET" />
    </logger>

    <logger name="org.apache.catalina" level="INFO" additivity="false">
        <appender-ref ref="FILE-CATALINA" />
    </logger>

    <logger name="org.apache.catalina.core.ContainerBase.[Catalina]" level="INFO" additivity="false">
        <appender-ref ref="FILE-CATALINA" />
    </logger>

    <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[/manager]" level="INFO"
        additivity="false">
        <appender-ref ref="FILE-CATALINA" />
    </logger>

    <logger name="org.apache.catalina.core.ContainerBase.[Catalina].[/host-manager]" level="INFO"
        additivity="false">
        <appender-ref ref="FILE-CATALINA" />
    </logger>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>

</configuration>

The console in Eclipse has:

2017-11-22 18:28:31.616 INFO  {main} [o.a.c.h.Http11NioProtocol] : Starting ProtocolHandler ["http-nio-8080"]
2017-11-22 18:28:31.621 INFO  {main} [o.a.c.a.AjpNioProtocol] : Starting ProtocolHandler ["ajp-nio-8009"]
18:28:37.591 [http-nio-8080-exec-2] DEBUG com.mycompany.otmxfer.OTMXferServlet - Entering doPost(/OTMXferServlet/OTMXfer): text/xml
18:28:37.599 [http-nio-8080-exec-2] DEBUG com.mycompany.otmxfer.OTMXferServlet - Received 1 lines

The first two lines are from Tomcat and correspond to the defined pattern. The next ones are from slf4j calls in the servlet.

The servlet.log file is created, but remains empty.

I tried to set -Dlogback.configurationFile but when I do so I get errors (ch.qos.logback.core.util.IncompatibleClassException).

I must be missing something (wrong place for file, missing file, other?)

1

1 Answers

0
votes

Partial answer:

The logback.xml above uses classes (org.apache.juli.logging.ch.qos.logback...) that are in tomcat-juli and not in logback-core. Using the logback-core classes (ch.qos.logback...) for the appender my code uses resolves the problem.

But then it's the appenders for the general Tomcat code that cannot be created. Using two distinct logback.xml files for servlet and server removes the error message but I don't get any server log...