configuring log4j.properties file | separating logs
I am using hibernate in my webpoject.I have my log4j开发者_运维问答.propertes under WEB-INF/classes.I want seperate log files for hibernate logs and my application log.In shot am not interested in hibernate logs but am interested only in my application log.
I have following in my log4j.properties
# Define the root logger with appender file
logDir = ${catalina.home}/logs/akp.log
log4j.rootLogger = DEBUG, FILE
#Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File= ${catalina.home}/logs/${fileName}.log
And within my app i am getting a instance of my logger as
private Logger logger = Logger.getLogger(MyClass.class.getName());
But in logs folder i can see only one file with exmpt name : .log (not xyz.log).Also withing this name i have all mixed logs.I want seperate log file for each of my applation class as i am using
logger = Logger.getLogger(MyClass.class.getName());
your log4j properties file is declaring variables but not using them. You've also got a couple of other issues and have asked about three separate questions.
Here you define the variable 'logDir':
logDir = ${catalina.home}/logs/akp.log (you are mixing up file name and directory here)
Here you use the variable 'fileName' and are not referring to logDir at all
log4j.appender.FILE.File= ${catalina.home}/logs/${fileName}.log
You need to change a fair bit to achieve what you've described (there are also a vast range of other options and implications) but here's a start - declare your directory and file name separately:
logDir = ${catalina.home}/logs fileName = myAppLog.log
specify the root category appender (using a better naming convention than 'FILE'). Personally I'd never use DEBUG at the root unless it was a very small app:
log4j.rootLogger = DEBUG, myAppAppender
Create the appender using the log directory and filename. Use append to add to decide whether to add to existing file or overwrite:
log4j.appender.myAppAppender=org.apache.log4j.FileAppender log4j.appender.myAppAppender.File= ${logDir}/${fileName} log4j.appender.myAppAppender.Append=true
You can then specify additional appenders (and files) for different application categories. Parameterise the filename as needed/convenient, use additivity=false to keep the entry only in this specific log:
log4j.category.com.company.package.specialpackagename=INFO, specialAppender log4j.appender.specialAppender=org.apache.log4j.FileAppender log4j.appender.specialAppender.File=${logDir}/specialPackageLog.log log4j.appender.specialAppender.Append=true log4j.appender.specialAppender.Additivity=false
That should get you started (if you've not got it already :)
cheers
Rob
Did you replace "filename" in ${fileName}? Is it creating the log files under catalina.home folder, if not check if your catalina.home is declared?
I had a similar problem where I was using log4j.properties and the purpose was to push logs from one package hierarchy into one file and the logs from a different package hierarchy in second file.
Although there is an answer accepted for this question, still I am just writing this to help people who are not able to configure this.
We need to configure appenders for each logger and define two loggers as below:
# Direct log messages to a log file
log4j.appender.metering_app=org.apache.log4j.RollingFileAppender
log4j.appender.metering_app.File=C:/logs/Metering_INFO.log
log4j.appender.metering_app.MaxFileSize=10MB
log4j.appender.metering_app.MaxBackupIndex=50
log4j.appender.metering_app.layout=org.apache.log4j.PatternLayout
log4j.appender.metering_app.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{2}:%L - %m%n
log4j.appender.metering_app.filter.ID=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.metering_app.filter.ID.LevelMax=FATAL
log4j.appender.metering_app.filter.ID.LevelMin=INFO
log4j.appender.metering_app.filter.ID.AcceptOnMatch=true
# Direct log messages to a log file
log4j.appender.notification_app=org.apache.log4j.RollingFileAppender
log4j.appender.notification_app.File=C:/logs/Notification_INFO.log
log4j.appender.notification_app.MaxFileSize=10MB
log4j.appender.notification_app.MaxBackupIndex=50
log4j.appender.notification_app.layout=org.apache.log4j.PatternLayout
log4j.appender.notification_app.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{2}:%L - %m%n
log4j.appender.notification_app.filter.ID=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.notification_app.filter.ID.LevelMax=FATAL
log4j.appender.notification_app.filter.ID.LevelMin=INFO
log4j.appender.notification_app.filter.ID.AcceptOnMatch=true
#root logger
log4j.logger.com.test.metering=INFO, metering_app
log4j.additivity.com.test.metering=false
log4j.logger.com.test.notification=INFO, notification_app
log4j.additivity.com.test.notification=false
精彩评论