java 使用 Logback-test.xml 向日志文件添加时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36795792/
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-11-03 01:52:39  来源:igfitidea点击:

Adding timestamp to a log file using Logback-test.xml

javaxmlloggingtimestamplogback

提问by java123999

Currently my Spring-boot application logs to a file named: myLog.log, this is working as intended, however I would like the log file to have a timestampat the end of it and create a new fileeach time it is ran.

目前我的 Spring-boot 应用程序记录到一个名为: 的文件myLog.log,这按预期工作,但是我希望日志文件timestamp在它的末尾有一个,并在每次运行时创建一个新文件

I have tried to implement this in my logback-test.xmlfile shown below, but it is just giving me the filename: myLog.log withouta timestamp.

我试图在我的logback-test.xml文件中实现这个,如下所示,但它只是给了我文件名:myLog.log没有时间戳。

How can I fix this?

我怎样才能解决这个问题?

Logback-test.xml:

Logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <logger name="org.springframework.web" level="INFO"/>

    <!-- Send debug messages to System.out -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS}  - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>path/to/my/file/mylog.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>mylog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>10</MaxIndex>
        </rollingPolicy>

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

    <logger name="com.my.package" level="INFO" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </logger>

    <!-- By default, the level of the root level is set to DEBUG -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

回答by nyname00

You can define a variable like this:

您可以像这样定义一个变量:

<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>

(note: don't use colons in datePattern)

(注意:不要在 中使用冒号datePattern

Then use it directly in your appender's fileelement:

然后直接在你appenderfile元素中使用它:

 <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>path/to/my/file/mylog-${myTimestamp}.log</file>
 ...
 </appender>

Or in a simple FileAppender:

或者在一个简单的FileAppender

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>path/to/my/file/mylog-${myTimestamp}.log</file>
    <encoder>
        <Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
    </encoder>
</appender>