【问题标题】:Adding timestamp to log file in Spring Boot?在 Spring Boot 中向日志文件添加时间戳?
【发布时间】:2016-08-04 09:55:18
【问题描述】:

我目前在我的应用程序中使用Spring Boot,并且我知道它使用Logback 作为其默认日志记录实现。

目前在我的applications.properties 文件中,我有以下内容:

#some other properties

#logging details
logging.path= path/to/my/log/folder

这当前记录到我的输出文件夹中的一个文件:spring.log

如何更改此文件以使其包含创建时间的时间戳和日期

例如 - “my-application-log-DATE-TIME.log”

【问题讨论】:

    标签: java spring properties timestamp logback


    【解决方案1】:

    在 appender 中,尝试添加:

    <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>
                    %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
                </Pattern>      </layout>
    

    【讨论】:

    • 我不确定您所说的附加程序是什么意思?谢谢
    【解决方案2】:

    你做对了。使用 Spring Boot 的 property support 自定义您的 application.properties 文件。

        // application.properties file
    app.name=appname
    logging.path=logpath
    logging.file=${appname}-{timestamp}.log
    

    另一方面,在您的支持类中,您可以创建一个 url 助手来获取属性值:

        /**
     * Helper class to get configured urls.
     */
    @Component
    public class UrlHelper {
    
        @Inject
        private Environment env;
    
        private static final String LOGGING_TIMESTAMP = "{timestamp}";
    
    private static String loggingFileUrlPattern;
    
    /**
         * Initializes properties.
         */
        @PostConstruct
        public void initProperties() {
    
            loggingFileUrlPattern = env.getRequiredProperty("logging.file");
        }
    
    /**
         * Formats the loggin Url pattern application property with the current
         * timestamp
         * 
         * @param timestamp
         *            current timestamp.
         * @return The complete logging file url.
         */
    
        public static String buildLoggingFileUrl(String timestamp) {
            return loggingFileUrlPattern.replace(LOGGING_FILE_URL, timestamp);
        }
    }
    

    希望对你有帮助!

    【讨论】:

    • 结果为@​​987654324@
    【解决方案3】:

    删除 .properties 文件样式配置并使用 logback 的正确配置 - logback.xml。 Spring Boot 已经为此做好了充分的准备!这是一个例子:

    <property name="logPattern" value="%d %-5level %logger{35} - %msg%n"/>
    <property name="logEncoding" value="UTF-8"/>
    <property name="logDirectory" value="logs"/>
    
    <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logDirectory}/myapplication.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logDirectory}/myapplication_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>30MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <charset>${logEncoding}</charset>
            <pattern>${logPattern}</pattern>
        </encoder>
    </appender>
    
    <logger name="org.springframework" level="warn"/>
    
    <root level="INFO">
        <appender-ref ref="fileAppender"/>
    </root>
    

    这不仅会为日志文件添加时间戳,还会帮助您进行日志轮换(例如,它会保留旧日志直到它们达到一定大小等)。

    【讨论】:

    • 谢谢,我应该把它放在我的项目哪里?
    • 假设您有一个基于 maven 的项目,只需创建一个名为 logback.xml 的文件并将其放在 src/main/resources 下,然后将内容复制到该文件中。另外,我犯了一个错误,没有在此处复制整个文件...以 开始文件并以 结束以使其成为正确的 XML。
    • 查看此链接以获取 logback 配置的几个完整示例。 mkyong.com/logging/logback-xml-example
    • 我实现了相同的代码,但我无法获得定义的最大大小的文件,而且滚动文件随后每个文件的大小都翻了一番,我是否遗漏了什么。
    猜你喜欢
    • 2016-08-16
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2017-04-07
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多