gpt4 book ai didi

java - Logback,根本没有日志文件(RollingFileAppender 和 SizeBasedTriggeringPolicy

转载 作者:行者123 更新时间:2023-11-28 22:24:08 24 4
gpt4 key购买 nike

logback 有问题。我尝试记录我的操作,想将所有日志写入文件,但在我的应用程序启动后根本没有日志

logback.xml

<configuration>
<appender name="FILE-LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<fileNamePattern>log/application-%d{yyyy-MM-dd}.log%i</fileNamePattern>
<maxFileSize>5MB</maxFileSize>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.test.application" level="info">
<appender-ref ref="FILE-LOGGER"/>
</logger>
<root level="debug">
<appender-ref ref="FILE-LOGGER"/>
</root>
</configuration>

服务.java

 static final Logger LOG = LoggerFactory.getLogger(TestClass.class);
public List<Items> getAll(){
LOG.info("hi, info!!!!!");
LOG.error("hi, error!!!!!");
}

pom.xml

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-version}</version>
</dependency>

期望日志会在日志文件中

最佳答案

无意中发现了一个问题我从 logback.xml 中删除文件并开始在控制台中接收日志并找到下一个有趣的行:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:org/slf4j/slf4j-simple/1.7.26/slf4j-simple-1.7.26.jar!/org/slf4j/impl/StaticLoggerBinder.class]

ERROR in ch.qos.logback.core.joran.util.PropertySetter@100454a1 - A "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" object is not assignable to a "ch.qos.logback.core.rolling.RollingPolicy" variable.

要解决此问题,您需要执行后续步骤:

  • 删除slf4j依赖
  • SizeBasedTriggeringPolicy 重命名为SizeAndTimeBasedRollingPolicy

关于java - Logback,根本没有日志文件(RollingFileAppender 和 SizeBasedTriggeringPolicy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com