java logback 不生成文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11510227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:23:08  来源:igfitidea点击:

logback not generating file

javaslf4jlogback

提问by user746458

I am new to logback. I am trying to log in a file. My logback.xmlis:

我是 logback 的新手。我正在尝试登录一个文件。我的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>

Logging is happening in console but no file is being generated at the given path. My Maven dependencies are

日志记录在控制台中进行,但没有在给定路径生成文件。我的 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>

I am using the logger as:

我将记录器用作:

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

logger.info("Logged Message");

Please help me out here... what I am missing?

请帮我在这里...我错过了什么?

回答by wolfcastle

Are you sure your logback.xml file is in the correct location and is being detected? Try intentionally putting in errors in the config file, and seeing if you get messages in the console (Logback will do this automatically). Additionally, you can ask Logback to print out its internal status information.

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