简体   繁体   中英

Logback creates log file but doesn't write anything to it

community!

I've been trying to set up a common logback config: one appender to console and another one to a file. The console appender seems to be working ok but the file appender is not. Only an empty log file is created at start up, but that's all. I started setting up a Rolling File Appender, then made it simpler to a basic File Appender and has the same behavior. I use maven to build the application and the application uses spring (not boot). So I'm not sure if I'm missing a configuration...

What logback outputs when starting (with debug set to true):

20:10:26,380 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
20:10:26,505 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
20:10:26,520 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
20:10:26,653 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
20:10:26,663 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
20:10:26,666 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
20:10:26,668 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [./logs/mylog.log]
20:10:26,674 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
20:10:26,675 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
20:10:26,676 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
20:10:26,676 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
20:10:26,678 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@17550481 - Registering current configuration as safe fallback point

The logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration debug="true">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
    </encoder>
  </appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>./logs/mylog.log</file>
  <immediateFlush>true</immediateFlush>
  <encoder>
    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
  </encoder>
</appender>

  <!-- My first sad attempt with a file appender -->
  <!--<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>./logs/mylog.log</file>
    <immediateFlush>true</immediateFlush>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <fileNamePattern>./logs/mylog-%d.%i.zip</fileNamePattern>
      <maxFileSize>200MB</maxFileSize>
      <maxHistory>60</maxHistory>
      <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>

    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
    </encoder>
  </appender>  -->

  <root level="INFO">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>

</configuration>

Some related dependencies in my pom.xml file

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.7.RELEASE</version>
        <exclusions>
          <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
        <exclusions>
          <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.4.3.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
        <version>1.2.0.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.7.RELEASE</version>
      </dependency>
<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.25</version>
      </dependency>
      <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
      </dependency>

I've been looking at other logback questions here on stackoverflow, and most seem to point out some missing config in the logback.xml file, but it doesn't seem to be the case here.

EDIT: I tried to modify the console appender, just to try to isolate the problem, as:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} DEBUG [%thread] %-5level %logger{36} [%file:%line] - %msg%n</pattern>
    </encoder>
  </appender>

Modifying the encoder like that should add the DEBUG word in every line, but I don't see such change being applied. So, it seems like the BasicConfiguration is still being used (despite finding the config as shown in log).

EDIT 2: After Spring application context is loaded, the logging config is set back to null.

15:12:24.590 [main] INFO  com.my.package.SomeClass [SomeCodeFile.java:83] - Logback used 'null' as the configuration file.
15:12:24.591 [main] INFO  com.my.package.SomeClass [SomeCodeFile.java:85] - Root logger level is: INFO

Any ideas would be greatly appreciated! Thank you!

Try to use below reference logback configuration

<appender name="DebugFileAppender"
                class="ch.qos.logback.core.FileAppender">
    <file>${LOG_PATH}/log.debug</file>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>DEBUG</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <Pattern> %d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
    </Pattern>
  </encoder>
</appender>

Like this you can define your appender to use

<logger name="debugAppender" level="debug" additivity="false">
        <appender-ref ref="DebugFileAppender" />
</logger>

Ok, I just found that as part of a library that I can't touch, there was a section that loaded a logback context from an specific file. Since it wasn't found, it removed the one set before the spring application context was loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM