问题
I am new to log4j and logging activities.
I have log4.properties file.
**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**
please suggest me how to configure it to log message into my specified file like i want it to log somewhere(i.e c:\abc.log);
let me know if you require some more inforamtion
回答1:
As a quick answer:
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
You could also need to update
log4j.rootCategory=DEBUG, FileAppender
or
log4j.rootCategory=DEBUG, stdout, FileAppender
I'd strongly suggest you to check the documentation. You seem to be using log4j 1.x . If you are using log4j 2.x, you can check here .
回答2:
This is quite simple. Try this
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
回答3:
# 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
来源:https://stackoverflow.com/questions/18374223/how-to-configure-log4j-properties-file-for-file-appenders-in-netbeans-6-8