Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback-test.xml configuration is producing two log files instead of one?

When I stop running my spring-boot application there are two log files produced rather than one (one is expected).

What is wrong in my Logback-test.xml file below that may be causing this?

logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>

    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <logger name="org.springframework.web" level="INFO"/>

    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS}  - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <file>C:\path\to\my\file\myLog-${myTimestamp}.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>myLog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>2MB</MaxFileSize>
        </triggeringPolicy>

    </appender>

    <logger name="com.my.package" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

The two files being created are e.g.:

myLog-2016-04-22_15-47-30.126.log

and

myLog-2016-04-22_15-47-30.922.log
like image 896
java123999 Avatar asked Apr 22 '16 14:04

java123999


People also ask

What is use of Logback-test xml?

If you are using Maven and if you place the logback-test. xml under the src/test/resources folder, Maven will ensure that it won't be included in the artifact produced. Thus, you can use a different configuration file, namely logback-test. xml during testing, and another file, namely, logback.

How do I setup a Logback XML file?

In a Logback. xml file, all the configuration options are enclosed within the <configuration> root element. In the root element, you can set the debug=true attribute to inspect Logback's internal status. You can also configure auto scanning of the configuration file by setting the scan=true attribute.

Which of the following is the configuration file for Logback?

As mentioned earlier, logback will try to configure itself using the files logback-test. xml or logback. xml if found on the class path.


2 Answers

The timestamp is generated when the configuration is parsed. Since Spring Boot reinitializes logback once during startup, two different timestamps are generated and therefore the two files.

You can use a timeReferene="contextBirth" in your config to get a constant timestamp. Since the LoggerContext isn't destroyed, just resetted this should work:

<timestamp key="myTimestamp" timeReference="contextBirth" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
like image 99
joshiste Avatar answered Oct 25 '22 00:10

joshiste


How does your logging.config property look? You problem may be the pathing (or the property missing from your properties file). E.g. should look something like this:

logging.config=classpath:logback-test.xml

in application-test.properties.

The other possibility might be that you have two active profiles. E.g. your test profile and the default one. In that case, you can get around it with conditional configuration in logback.

See the answer for more details. Also, see the last comment on that answer. spring boot 1.3 includes some new features around handling multiple profiles with logback:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-logback-extensions

like image 31
leeor Avatar answered Oct 25 '22 01:10

leeor