如何创建滚动日志文件

时间:2015-12-09 22:55:12

标签: tomcat7 slf4j logback

我似乎无法获得滚动日志文件。

我的logback.xml配置包含以下内容:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%date %level %thread %X{jobid} %logger{20}.%method\(%line\) %msg%n</pattern>
    </encoder>
</appender>

<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator>
        <key>logFileName</key>
        <defaultValue>unknown</defaultValue>
    </discriminator>
    <sift>
        <appender name="FILE-${logFileName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>${catalina.base}/logs/${logFileName}.log</file>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${catalina.base}/logs/${logFileName}_%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%date %level %thread %X{jobid} %logger{20}.%method\(%line\) %msg%n</pattern>
            </encoder>
        </appender>
    </sift>
</appender>


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

一旦Tomcat启动并加载应用程序,我就可以看到日志文件&#34; unknown.log&#34;。那是因为我还没有在代码中设置logFileName。

但是,我没有看到滚动文件。即使等了好几分钟。

根据logback documentation模式&#34;%d {yyyy-MM-dd_HH-mm}&#34;应该制定每分钟的滚动时间政策。

我没有看到创建新的日志文件。

更新

我添加了STDOUT appender。现在我在catalina.out日志中看到了。

有趣。 &#34; unknown.log&#34;中的条目来自SIFT的文件在分钟停止时停止。之后没有任何内容写入SIFT文件,但我一直在catalina.out中获取条目:

2015-12-09 16:31:22,269 INFO Thread1 RlQti5b0QYybZvRuHi/m c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
2015-12-09 16:31:49,654 INFO Thread1 mY0E7sV7QZyTRZAw9UwV c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
2015-12-09 16:31:51,812 INFO Thread1 WWjuyzH+RmCa35JcxRJy c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
2015-12-09 16:31:58,977 INFO Thread1 jUdqB1DaQ8KxajDFk6jN c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.

3 个答案:

答案 0 :(得分:0)

只是猜测但是 - 可能是因为你没有记录任何东西。他们像那样“懒惰”。

答案 1 :(得分:0)

从fileNamePattern中删除%i似乎可以解决问题。我想索引(%i)只允许用于FixedWindowRollingPolicy。

答案 2 :(得分:0)

试试这个

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <statusListener class="ch.qos.logback.core.status.NopStatusListener" />
    <property name="USER_HOME" value="\logs" />

    <appender name="FILE-THREAD" class="ch.qos.logback.classic.sift.SiftingAppender">

        <!-- This is MDC value -->
        <!-- We will assign a value to 'logFileName' via Java code -->
        <discriminator>
            <key>logFileName</key>
            <defaultValue>head0</defaultValue>
        </discriminator>

        <sift>

            <!-- A standard RollingFileAppender, the log file is based on 'logFileName' 
                at runtime -->
            <appender name="FILE-${logFileName}"
                class="ch.qos.logback.core.rolling.RollingFileAppender">
                <file>${USER_HOME}/${logFileName}.log</file>

                <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                    <Pattern>
                        %d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35}
                        - %msg%n
                    </Pattern>
                </encoder>

                <rollingPolicy
                    class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                    <FileNamePattern>${USER_HOME}/${logFileName}.%i.log.zip
                    </FileNamePattern>
                    <MinIndex>1</MinIndex>
                    <MaxIndex>10</MaxIndex>
                </rollingPolicy>

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

            </appender>

        </sift>
    </appender>