Java 使用 log4j 创建带日期的日志文件

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

Create log file with date using log4j

javalogginglog4j

提问by Vicky Thakor

I'm writing my log file using below code but it stores file as QueryLog.log. Am i missing something? Check my code of log4j.propertiesfile

我正在使用以下代码编写日志文件,但它将文件存储为QueryLog.log. 我错过了什么吗?检查我的log4j.properties文件代码

log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
log4j.logger.org.hibernate.tool.hbm2ddl=warn
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug
log4j.rootLogger = DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd-a
log4j.appender.FILE.File=log4j/QueryLog.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern= %d{HH:mm:ss} %-5p %c - %m%n

Links i used:

我使用的链接:

http://www.tutorialspoint.com/log4j/log4j_logging_files.htm

http://www.tutorialspoint.com/log4j/log4j_logging_files.htm

http://www.codejava.net/coding/configure-log4j-for-creating-daily-rolling-log-files

http://www.codejava.net/coding/configure-log4j-for-creating-daily-rolling-log-files

采纳答案by crush

As is mentioned in this StackOverflow Q&A, the purpose of a RollingFileAppenderis to automatically create a new log file at some defined interval. In the case of the DailyRollingFileAppender, that interval is 12:00 AM of each day.

正如StackOverflow Q&A 中提到的,a的目的RollingFileAppender是在某个定义的时间间隔自动创建一个新的日志文件。在 的情况下DailyRollingFileAppender,该间隔是每天的凌晨 12:00。

What this means is that the first file created by log4j will have the file name you specified here:

这意味着 log4j 创建的第一个文件将具有您在此处指定的文件名:

log4j.appender.FILE.File=log4j/QueryLog.log

And, from then forward, each day a new log file will be created with the date appended to it.

并且,从那时起,每天都会创建一个新的日志文件,并附加日期。

To always name the file with the date appended, you could use DatedFileAppenderby Geoff Mottram

要始终使用附加日期命名文件,您可以使用DatedFileAppenderGeoff Mottram

回答by jandresrodriguez

This line sets the log file name, in your log4j properties you have: log4j.appender.FILE.File=log4j/QueryLog.log

这一行设置日志文件名,在您的 log4j 属性中:log4j.appender.FILE.File=log4j/QueryLog.log

You can see the answer here Setting a log file name to include current date in Log4j

您可以在此处看到答案 设置日志文件名以在 Log4j 中包含当前日期

回答by kisna

The solution to log directly to a file with current active date/time such as XYZ.log.20150101.log instead of XYZ.log could be done by simply removing ActiveFileName property when using the rolling package org.apache.log4j.rolling.RollingFileAppender in the apache-log4j-extras 1.1 with log4j 1.2.x.

在使用滚动包 org.apache.log4j.rolling.RollingFileAppender 时,可以通过简单地删除 ActiveFileName 属性来完成直接登录到具有当前活动日期/时间的文件(例如 XYZ.log.20150101.log 而不是 XYZ.log)的解决方案在 apache-log4j-extras 1.1 和 log4j 1.2.x 中。

<appender name="defaultFileAppender" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="append" value="true" />
    <param name="Threshold" value="INFO" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern"
            value="${catalina.base}/logs/application/custom-application-logger.%d{yyyy-MM-dd_HH_mm}" />
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %-10t [%-40.40c] %x - %m%n" />
    </layout>
</appender>