创建新的日志文件,每个在logback中运行? [英] Create new log file each run in logback?

查看:203
本文介绍了创建新的日志文件,每个在logback中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在每次运行应用程序时创建新的日志文件?

How to create new log file each time application runs?

我想以任何方式保留以前的日志.例如,我希望按创建时间和日期来命名每个新日志文件.否则,我同意将旧的日志文件备份到日期和时间文件名中.

I would like to keep previous logs in any way. For example, I would prefer to name each new log file by time and date it was created. Otherwise, I agree to backup old log file into date and time filename.

很遗憾,我在这里看不到适当的策略和/或触发器: http://logback .qos.ch/manual/appenders.html

Unfortunately, I can't see appropriate policies and/or triggers here: http://logback.qos.ch/manual/appenders.html

更新

我做了大致与重复"中所述的内容

I made approximately as said in "duplicate"

    <appender name="ROUTINEAPPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/routine.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/routine%d{yyyyMMdd}%d{HHmmss,aux}.log</fileNamePattern>
            <TimeBasedFileNamingAndTriggeringPolicy class="com.inthemoon.toolkit.StartupTimeBasedTriggeringPolicy" />
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} - %C{0} - %msg%n</pattern>
        </encoder>
    </appender>

,但永远不会调用我的班级com.inthemoon.toolkit.StartupTimeBasedTriggeringPolicy.我在start()方法中添加了断点,但从未提出.

but my class com.inthemoon.toolkit.StartupTimeBasedTriggeringPolicy is never called. I put breakpoint in start() method but it never raised.

此外,不发生滚动.日志文件已创建,但始终具有名称routine.log.

Also, no rolling occurs. Log file is created, but it always has the name routine.log.

我也不明白参数filefilenamePattern应该如何共存.

Also I don't understand, how parameters file and filenamePattern should coexist.

更新2

我有固定的UPDATE 1类参考,但仍然没有我需要的东西.在给定的解决方案中,将日期时间插入OLD日志文件名中.例如,如果我在2013年运行程序,它将创建routine.log.然后我等了一年并在2014年运行程序.将创建新的日志文件并命名为routine.log,而将OLD 2013日志放入routine2014XXXXXXXXXX.log中,这是绝对无关的.

I have fixed UPDATE 1 class reference, but still don't have what I need. In given solution, the date time is inserted into OLD log filename. For example, if I ran program in 2013, it created routine.log. Then I waited a year and run program in 2014. New log file will be created and have name routine.log, while OLD 2013 log will be put into routine2014XXXXXXXXXX.log, which is absolutely irrelevant.

每次程序启动时我都需要创建一个新文件,并用日期时间戳标记该文件.

I need create new file each time program starts and have this namely file marked with date-time stamp.

推荐答案

以下配置将在每次运行程序时创建一个新的日志文件和控制台输出,请注意,它不使用RollingFileAppender.有关更多信息,请参阅logback文档 https://logback.qos.ch/manual/configuration.html

The following configuration will create a new logfile and console output every time the program is run, please note that it does not use RollingFileAppender. For more information please refer to logback documentation https://logback.qos.ch/manual/configuration.html

<configuration>
    <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logfile-${bySecond}.txt</file>
        <append>true</append>
        <encoder>
            <Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </encoder>
    </appender>
    <root level="info" additivity="false">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"></appender-ref>
    </root>
</configuration>

这篇关于创建新的日志文件,每个在logback中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆