简体   繁体   English

Logback 创建日志文件但不向其中写入任何内容

[英]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.我一直在尝试设置一个通用的 logback 配置:一个附加到控制台,另一个附加到文件。 The console appender seems to be working ok but the file appender is not.控制台 appender 似乎工作正常,但文件 appender 不是。 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.我开始设置一个 Rolling File Appender,然后让它变得更简单到一个基本的 File Appender 并且具有相同的行为。 I use maven to build the application and the application uses spring (not boot).我使用 maven 构建应用程序,应用程序使用 spring(不是引导)。 So I'm not sure if I'm missing a configuration...所以我不确定我是否缺少配置...

What logback outputs when starting (with debug set to true):启动时的 logback 输出(调试设置为 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 logback.xml 文件

<?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我的 pom.xml 文件中的一些相关依赖项

    <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.我一直在查看关于 stackoverflow 的其他 logback 问题,大多数似乎指出 logback.xml 文件中缺少一些配置,但这里似乎并非如此。

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.像这样修改编码器应该在每一行中添加 DEBUG 词,但我没有看到应用了这样的更改。 So, it seems like the BasicConfiguration is still being used (despite finding the config as shown in log).因此,似乎仍在使用 BasicConfiguration(尽管找到了日志中显示的配置)。

EDIT 2: After Spring application context is loaded, the logging config is set back to null.编辑 2:加载 Spring 应用程序上下文后,日志配置设置回 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尝试使用以下参考 logback 配置

<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像这样,您可以定义要使用的 appender

<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.好的,我刚刚发现作为我无法触及的库的一部分,有一个部分从特定文件加载了 logback 上下文。 Since it wasn't found, it removed the one set before the spring application context was loaded.由于没有找到,它在加载 spring 应用程序上下文之前删除了一组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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