使用 slf4j+logback 找不到我的 .log 文件

Can't find my .log file using slf4j+logback

首先你必须知道我是 logback 的初学者所以也许我犯了一个非常愚蠢的错误并且没有注意到它。那么也许问题可能已经在其他地方解决了但只要我搜索过我发现什么都没有。

但是当我尝试配置我的日志文件时遇到了问题。 我想在我的项目的根目录上有一个名为 logs 的存储库,其中包含按时间(和限制大小)排序的所有日志。所以我看到 logback 正在使用我想要的 SizeAndTimeBasedRollingPolicy.

所以在对 google 进行一些研究之后,我的配置文件 ( logback.xml ) 是:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="ROLLING"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>SG.log</file>
    <rollingPolicy
        class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
        <!-- each file should be at most 100MB, keep 60 days worth of history, 
            but at most 20GB -->
        <maxFileSize>100MB</maxFileSize>
        <maxHistory>60</maxHistory>
        <totalSizeCap>20GB</totalSizeCap>
    </rollingPolicy>
    <encoder>
        <pattern>%msg%n</pattern>
    </encoder>
</appender>
<root level="DEBUG">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
    <appender-ref ref="ROLLING" />
</root>
</configuration>

我的 pom.xml 也是这样的:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testLog</groupId>
<artifactId>testLog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.3.0-alpha4</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.3.0-alpha4</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.8.0-beta1</version>
    </dependency>
</dependencies>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

当然我会调用记录器:

package testLog;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
   private static Logger logger = LoggerFactory.getLogger(Main.class);

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       logger.debug("toto");
       logger.info("ela ela");
       logger.warn("ce je ne sais quoi ");
       logger.error("ce dont du ciel qui la rend belle");
   }
}

现在我可以告诉您我的问题:没有生成日志文件。 您知道问题出在哪里吗?

感谢您的宝贵时间。

我认为它应该在您项目的根目录下,以确保生成它,而不是:

<file>SG.log</file>

尽量给绝对路径,比如

<file>/Users/username/logs/SG.log</file>

确保目录arborescence 已创建。 这样您就知道在哪里可以找到该文件,并且可以确保它确实已创建。

如果生成良好则执行:

<file>logs/SG.log</file>

它应该进入你的日志目录(手动创建目录以确保,我不认为 logback 会创建它)

编辑

你能把你的根记录器改成

<root level="DEBUG">
    <appender-ref ref="ROLLING" />
</root>

可能其他 STDOUTFILE 中的一个正在劫持日志

EDIT2

我最后想到的与我的配置不同的是这些:

  • 我的 logback.xml 文件是这样开始的

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration>
    <configuration debug="true">
    
  • 我为编码器设置了class

    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>%msg%n</Pattern>
    </encoder>
    

还有"ce don du ciel qui la rend belle" :)

所以我找到了我的问题的解决方案(出现了另一个问题,但很快就会解决)

我不知道这是 Bentaye 给出的版本还是我在我的类路径中添加了资源,现在我有回应了!

神奇!

谢谢大家,如果你尝试过:)