3

我是 log4j 和日志记录活动的新手。

我有 log4.properties 文件。

**log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %40.40c:%4L - %m%n**

请建议我如何配置它以将消息记录到我指定的文件中,就像我希望它记录在某处(即 c:\abc.log);

如果您需要更多信息,请告诉我

4

3 回答 3

1

作为一个快速的答案:

log4j.appender.FileAppender= org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.File= C:\abc.log
log4j.appender.FileAppender.MaxFileSize= 10MB
log4j.appender.FileAppender.MaxBackupIndex= 10
log4j.appender.FileAppender.layout= org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern= \#\#\#\#<%d{MMM d, yyyy h:mm:ss aa zzz}> <%p> <%c> <%X{machine}> <%X{wlsDomain}> <%t> <%X{userid}> <%X{object}> <%X{messageCode}> <%X{reqId}> <%X{reqURL}> <%m>%n
log4j.appender.FileAppender.Threshold= DEBUG

您可能还需要更新

log4j.rootCategory=DEBUG, FileAppender

或者

log4j.rootCategory=DEBUG, stdout, FileAppender

我强烈建议您检查文档。您似乎正在使用log4j 1.x。如果您使用的是 log4j 2.x,您可以在此处查看

于 2013-08-22T07:42:37.800 回答
1

这很简单。尝试这个

log4j.rootLogger=ERROR, ROOT
# Direct log messages to a log file
#log4j.appender.ROOTLOGGER=org.apache.log4j.ConsoleAppender
log4j.appender.ROOT=org.apache.log4j.RollingFileAppender
# appends the log level to file name abc.log
log4j.appender.ROOT.File=abc.log 
log4j.appender.ROOT.MaxFileSize=1000KB
#Keep 5 old files around.
log4j.appender.ROOT.MaxBackupIndex=5
log4j.appender.ROOT.layout=org.apache.log4j.PatternLayout
#Format almost same as WebSphere's common log format.
# d date, t ist der Name des Threads, M ist Methodenname, C full qualified Classenname 
#log4j.appender.ROOT.layout.ConversionPattern=[%d] %t %C:%l %-4p - %M%n
log4j.appender.ROOT.layout.ConversionPattern=%5p [%d] [%t] %l : %m%n
于 2013-08-22T07:43:01.723 回答
0
# Root logger option
log4j.rootLogger=error, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logfile.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Struts2
log4j.logger.freemarker=ERROR
log4j.logger.com.opensymphony=ERROR
log4j.logger.com.opensymphony.xwork2.ognl=ERROR
log4j.logger.org.apache.struts2=ERROR
log4j.logger.org.apache.struts2.components=ERROR
log4j.logger.org.apache.struts2.dispatcher=ERROR
log4j.logger.org.apache.struts2.convention=ERROR

# Alfresco
log4j.logger.org.alfresco=ERROR
log4j.logger.org.apache.axis=ERROR
log4j.logger.org.apache.ws=ERROR
于 2015-03-25T15:39:35.363 回答