3

I've got a pair of custom loggers defined for LogBack in a Spring Boot application. The idea is to send all of that logging to a file, instead of the standard console output provided by Spring Boot by default. What I'm seeing is that the logging goes to both the custom file AND the console, and I cannot find any reason for that to happen. All logging to the two custom appenders is at DEBUG and/or TRACE level. I have set logging.level.root=INFO, thinking that would prevent this logging from showing up, but that didn't help.

I do have the standard Spring Boot logging configured for use in the application. So logging.path and logging.file are both set.

Any help appreciated.

Here is my custom logback-spring.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!--
    LogBack configuration used by all services to create separate files for TRACE and SOAP logs. 
-->
<configuration>
    <!-- Include the basic Spring-Boot logback configuration. -->
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <!-- An appender and logger for TRACE logging of method calls -->
    <appender name="TraceLoggingAppender" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_PATH}/logs/trace.log</file>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="TraceLogging" additivity="false" level="trace">
        <appender-ref ref="TraceLoggingAppender"/>
    </logger>

    <!-- An appender and logger for SOAP envelope logging -->
    <appender name="SoapLoggingAppender" class="ch.qos.logback.core.FileAppender">
        <file>${LOG_PATH}/logs/soap.log</file>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="SoapLogging" additivity="false" level="debug">
        <appender-ref ref="SoapLoggingAppender"/>
    </logger>
</configuration>

And an example usage:

private static final Log LOG = LogFactory.getLog("SoapLogging");
...
LOG.debug(soapMessage);
Dave Hicks
  • 31
  • 1
  • 7

2 Answers2

2

If you want to disable console logging and write output only to a file, you need a custom logback-spring.xml that imports file-appender.xml but not console-appender.xml, as shown in the following example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

You also need to add logging.file.name to your application.properties, as shown in the following example:

logging.file.name=myapplication.log

Courtesy: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-configure-logback-for-logging-fileonly

raikumardipak
  • 1,461
  • 2
  • 29
  • 49
0

Instead of including base.xml include defaults.xml as mentioned here

<include resource="org/springframework/boot/logging/logback/defaults.xml" />
yausername
  • 750
  • 5
  • 15
  • That did not help me. I'm still seeing logging from the SoapLogging appender on the CONSOLE. I only want those statements to go to the defined file. – Dave Hicks Apr 07 '18 at 22:34
  • Sorry, but that did not work, either. The net effect was to shut down logging entirely, or, at least, sent the files somewhere that I can't find them. – Dave Hicks Apr 09 '18 at 14:04