6

我是登录新手。我正在尝试登录文件。我logback.xml的是:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
            </Pattern>
        </encoder>
    </appender>

    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>d:/walknshine.log</File>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
                %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>d:/walknshine.%i.log.zip
            </FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

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

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

日志记录发生在控制台中,但在给定路径上没有生成文件。我的 Maven 依赖项是

  <!-- LOGGING DEPENDENCIES - LOG4J -->
    <dependency>
        <groupId>logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.6</version>
    </dependency>
    <dependency>
        <groupId>logback</groupId>
        <artifactId>sl4j</artifactId>
        <version>1.6.0</version>
    </dependency>

我将记录器用作:

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

logger.info("Logged Message");

请帮帮我......我错过了什么?

4

1 回答 1

15

您确定您的 logback.xml 文件位于正确的位置并且正在被检测到吗?尝试故意在配置文件中输入错误,并查看是否在控制台中收到消息(Logback 会自动执行此操作)。此外,您可以要求 Logback打印出其内部状态信息

public static void main(String[] args) {
    // assume SLF4J is bound to logback in the current environment
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    // print logback's internal status
    StatusPrinter.print(lc);
    ...
}
于 2012-07-17T04:47:50.623 回答