Logback滚动文件appender无法正常工作

时间:2017-04-05 10:45:01

标签: java logging logback

以下是我的回溯配置

<appender name="TIME_BASED_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>C:\\Users\\ADMIN\\Documents\\NetBeansProjects\\timelog.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">      
        <!-- daily rollover -->
        <fileNamePattern>C:\\Users\\ADMIN\\Documents\\NetBeansProjects\\timelog.%d{yyyy-MM-dd-HH-mm}.log</fileNamePattern>
        <maxHistory>30</maxHistory>
        <maxFileSize>1M</maxFileSize>
        <totalSizeCap>10M</totalSizeCap>

    </rollingPolicy>
    <encoder>
        <pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender> 

  <logger name="timeBased" level="INFO">
    <appender-ref ref="TIME_BASED_FILE"/>
    <appender-ref ref="consoleAppender"/>
  </logger> 

代码:

public class Test {
    public static void main(String [] args) {
        Logger logger = (Logger)LoggerFactory.getLogger("timeBased");
        logger.info("info");
    }
}

此代码仅在控制台上打印,在指定目录中不创建任何文件。文件夹具有所有写入权限。出了什么问题? Logback也不会显示任何错误。请帮忙。我正在使用windows7。

1 个答案:

答案 0 :(得分:1)

您需要将M替换为MB并更正fileName模式 以下示例适用于我:

<appender name="TIME_BASED_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>C:\\Users\\ADMIN\\Documents\\NetBeansProjects\\timelog.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>C:\\Users\\admin\\Documents\\NetBeansProjects\\timelog.%d{yyyy-MM-dd-HH-mm}.%i.log</fileNamePattern>
        <maxHistory>30</maxHistory>
        <maxFileSize>1MB</maxFileSize>
        <totalSizeCap>10MB</totalSizeCap>

    </rollingPolicy>
    <encoder >
        <pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>