Search code examples
springlogbackspring-logback

Can I Use Logback.xml To Speak to Two Loggers


So we have a somewhat unique situation in that we have a company logger that gets instantiated with the LoggerFactory.getLogger and the class name and then is passed a logging number, throwable exception and the message. This goes to the company admin console for monitoring logs.

As a team, we are using the logback.xml currently to write any and all log messages to a file so that we have extra information. Here is our logback.xml

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

    <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  

    <!-- dev/log/BOSApplication_${byDate}.log -->
    <timestamp key="byDate" datePattern="yyyy-MM-dd" />




    <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- <filter class="com.tdameritrade.commons.log.LogSuppressionFilter" /> -->
        <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender -->
        <File>../logs/BOSApplication_${byDate}.log</File>

        <encoder>
            <pattern>%date %-5level [%logger{36}:%F.%M:%L - %thread] - %msg%n</pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <!-- rollover daily -->
            <FileNamePattern>../logs/BOSApplication_%d{yyyy-MM-dd}_%i.log </FileNamePattern>

            <!-- keep 5 days' worth of history -->
            <MaxHistory>5</MaxHistory>

            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>100MB</MaxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>

        </rollingPolicy>

    </appender>

    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %-5level [%logger{36}:%F.%M:%L - %thread] - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="dbAppender" class="com.tdameritrade.commons.log.ApplicationMessagesAppender" />

    <root level="INFO">
        <appender-ref ref="stdout" />
        <appender-ref ref="rollingFile" />
        <appender-ref ref="dbAppender" />
    </root>

    <logger name="org.springframework" level="WARN" />
    <logger name="org.springframework.web.servlet.DispatcherServlet" level="INFO" />
    <logger name="org.apache.jasper" level="WARN" />
    <logger name="springfox.documentation.spring" level="WARN" />
    <logger name="org.apache.tomcat.util.net" level="WARN" />
    <logger name="org.apache.coyote.http11" level="WARN" />



    <logger name="mmAchFulfillmentLogger" level="INFO" additivity="false">
        <appender-ref ref="rollingFile" />
        <appender-ref ref="stdout" />
    </logger>



</configuration>

Currently we have to instantiate both loggers and then call one depending on type of message.

private static final LogManager coreLogManager = new LogManager(AchFulfillmentLoggerUtil.class, "MM_ACH_FULFILLMENT");

private static final Logger mmAchFulfillmentLogger = LoggerFactory.getLogger("mmAchFulfillmentLogger");

Is there any way that we can combine these two together in the logback.xml so that we can instantiate one log manager in a class and then do the following.

  • If the level is ERROR then call the core company logger and our file logger
  • If the level is anything else just call our file logger.

I have been searching but our problem seems somewhat unique to find an answer


Solution

  • A ThresholdFilter should be fine for this:

    <appender name="dbAppender" class="com.tdameritrade.commons.log.ApplicationMessagesAppender" >
      <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>error</level>
      </filter>
    </appender>
    

    Then clearly attach that under the appropriate logger(s) as well as the file logger.