2

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

Patrick Aquilone
  • 584
  • 2
  • 11
  • 28
  • Use logback for that. Use a specialized appender which writes the to what you want. That way in your code you can use a single logger and behind the scenes you can configure what you want. – M. Deinum Oct 01 '18 at 18:42
  • @M.Deinum ok. Can I pass runtime information to the custom appender? Like a log number or exception thrown when logging an error? – Patrick Aquilone Oct 01 '18 at 18:53
  • The exception is already part of the logging API in SLF4J (assuming that you are using that as the API. Everything else you could place in the MDC from SLF4J. Depending on what/how you need to log you might be able to use one of the existing appends to talk to your monitoring (if it is JMX or something like that). – M. Deinum Oct 01 '18 at 18:56
  • @M.Deinum Ok, so in my custom appender I can read out of the MDC. The exception is in the eventObject? – Patrick Aquilone Oct 01 '18 at 19:27

1 Answers1

0

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.

df778899
  • 10,703
  • 1
  • 24
  • 36