log4j.rootCategory
是对根类的设置,如不说明,以下的子类都要继承这些设置
log4j.category.*
是对自定义类的设置,可以对类、包和工程单独设置
Category的家族关系是通过“.”来说明的,比如x是x.y的父类。
默认情况下,子类要继承父类的全部设置,比如:
log4j.rootCategory=INFO, dest1
log4j.category.MyCategory=DEBUG
dest1会输出INFO以上的消息
对于Category logger = Category.getInstance("MyCategory")会输出DEBUG以上的消息,因为MyCategory继承了根类的设置,输出到dest1
如果使用下面的设置,每条消息会在dest1中输出两次:
log4j.rootCategory = INFO, dest1
log4j.category.MyCategory = DEBUG, dest1
记住,MyCategory继承了根类的设置。
可以指定子类不继承父类的设置,方法是指定log4j.additivity.*=false,如上例中:
log4j.rootCategory = INFO, dest1
log4j.additivity.MyCategory = false
log4j.category.MyCategory = DEBUG, dest1
2,http://wangjc-opal.iteye.com/blog/309924
log4j的强大功能无可置疑,但实际应用中免不了遇到某个功能需要输出独立的日志文件的情况,怎样才能把所需的内容从原有日志中分离,形成单独的日志文件呢?其实只要在现有的log4j基础上稍加配置即可轻松实现这一功能。
先看一个常见的log4j.properties文件,它是在控制台和myweb.log文件中记录日志:
log4j.rootLogger=DEBUG, stdout, logfile
log4j.category.org.springframework=ERROR
log4j.category.org.apache=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${myweb.root}/WEB-INF/log/myweb.log
log4j.appender.logfile.MaxFileSize=512KB
log4j.appender.logfile.MaxBackupIndex=5
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
================= 不同的类输出不同的文件:
如果想对不同的类输出不同的文件(以cn.com.Test为例),先要在Test.java中定义:
private static Log logger = LogFactory.getLog(Test.class);
然后在log4j.properties中加入:
log4j.logger.cn.com.Test= DEBUG, test
log4j.appender.test=org.apache.log4j.FileAppender
log4j.appender.test.File=${myweb.root}/WEB-INF/log/test.log
log4j.appender.test.layout=org.apache.log4j.PatternLayout
log4j.appender.test.layout.ConversionPattern=%d %p [%c] - %m%n
也就是让cn.com.Test中的logger使用log4j.appender.test所做的配置。
================= 同一个类输出多个日志文件:
但是,如果在同一类中需要输出多个日志文件呢?其实道理是一样的,先在Test.java中定义:
private static Log logger1 = LogFactory.getLog("myTest1");
private static Log logger2 = LogFactory.getLog("myTest2");
然后在log4j.properties中加入:
log4j.logger.myTest1= DEBUG, test1
log4j.appender.test1=org.apache.log4j.FileAppender
log4j.appender.test1.File=${myweb.root}/WEB-INF/log/test1.log
log4j.appender.test1.layout=org.apache.log4j.PatternLayout
log4j.appender.test1.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.myTest2= DEBUG, test2
log4j.appender.test2=org.apache.log4j.FileAppender
log4j.appender.test2.File=${myweb.root}/WEB-INF/log/test2.log
log4j.appender.test2.layout=org.apache.log4j.PatternLayout
log4j.appender.test2.layout.ConversionPattern=%d %p [%c] - %m%n
也就是在用logger时给它一个自定义的名字(如这里的"myTest1"),然后在log4j.properties中做出相应配置即可。别忘了不同日志要使用不同的logger(如输出到test1.log的要用logger1.info("abc"))。
==================== 继承性
还有一个问题,就是这些自定义的日志默认是同时输出到log4j.rootLogger所配置的日志中的,如何能只让它们输出到自己指定的日志中呢?别急,这里有个开关:
log4j.additivity.myTest1 = false
它用来设置是否同时输出到log4j.rootLogger所配置的日志中,设为false就不会输出到其它地方啦!注意这里的"myTest1"是你在程序中给logger起的那个自定义的名字!
如果你说,我只是不想同时输出这个日志到log4j.rootLogger所配置的logfile中,stdout里我还想同时输出呢!那也好办,把你的log4j.logger.myTest1 = DEBUG, test1改为下式就OK啦!
log4j.logger.myTest1=DEBUG, test1
===================== Threshold 修改appender的输出级别
### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存异常信息到单独文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## 异常日志文件名
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
============================================================================================
example:
#ident "%W"
#it will log every thing
log4j.rootCategory=INFO
log4j.logger.actions=INFO, R
log4j.logger.dto=INFO, R
log4j.logger.util=INFO, R
log4j.logger.delegators=INFO, R
log4j.logger.ejbcore=INFO, R
log4j.logger.javacore=INFO, R
log4j.logger.xxx=INFO, R
log4j.logger.loanclient=INFO, R
log4j.logger.loan.position=INFO, R
log4j.logger.org.springframework=INFO, R
#log4j.debug=true
#log4j.disable=INFO
#log4j.disable=DEBUG
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=@LOAN_ROOT@/log/xxx.log
log4j.appender.R.MaxFileSize=50000KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=3
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout=org.apache.log4j.TTCCLayout
log4j.appender.R.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss, SSS} [%t] [%5p] %c{1}:[%L] - %m%n
# =============== POST TRADE PROPERTIES =====================
log4j.logger.loan.tesgateway=INFO, POSTTRADE
log4j.logger.loan.posttrade=INFO, POSTTRADE
log4j.logger.loan.clearpar=INFO, POSTTRADE
log4j.logger.loan.liveupdate=INFO, POSTTRADE
log4j.logger.org.springframework=INFO, POSTTRADE
log4j.appender.POSTTRADE=org.apache.log4j.RollingFileAppender
log4j.appender.POSTTRADE.File=@LOAN_ROOT@/log/post_trade.log
log4j.appender.POSTTRADE.MaxFileSize=50000KB
# Keep one backup file
log4j.appender.POSTTRADE.MaxBackupIndex=3
log4j.appender.POSTTRADE.layout=org.apache.log4j.PatternLayout
#log4j.appender.POSTTRADE.layout=org.apache.log4j.TTCCLayout
log4j.appender.POSTTRADE.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss, SSS} [%t] [%5p] %c{1}:[%L] - %m%n
# =============== VAILDATION SERVICE PROPERTIES =====================
log4j.appender.VAILDATION=org.apache.log4j.RollingFileAppender
log4j.appender.VAILDATION.File=@LOAN_ROOT@/log/valcalc_service.log
log4j.appender.VAILDATION.MaxFileSize=50000KB
# Keep one backup file
log4j.appender.VAILDATION.MaxBackupIndex=1
log4j.appender.VAILDATION.layout=org.apache.log4j.PatternLayout
#log4j.appender.VAILDATION.layout=org.apache.log4j.TTCCLayout
log4j.appender.VAILDATION.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss, SSS} [%t] [%5p] %c{1}:[%L] - %m%n
log4j.logger.loan.calculator=INFO, VAILDATION
log4j.logger.loan.common=INFO, VAILDATION
log4j.logger.loan.framework=INFO, VAILDATION
log4j.logger.loan.validator=INFO, VAILDATION
log4j.logger.loan.propertiescache=INFO, VAILDATION
log4j.logger.loan.account=INFO, VAILDATION
private Category cat = Category.getInstance(loan.posttrade.common.publisher.GFITradePublisher.class.getName());
是对根类的设置,如不说明,以下的子类都要继承这些设置
log4j.category.*
是对自定义类的设置,可以对类、包和工程单独设置
Category的家族关系是通过“.”来说明的,比如x是x.y的父类。
默认情况下,子类要继承父类的全部设置,比如:
log4j.rootCategory=INFO, dest1
log4j.category.MyCategory=DEBUG
dest1会输出INFO以上的消息
对于Category logger = Category.getInstance("MyCategory")会输出DEBUG以上的消息,因为MyCategory继承了根类的设置,输出到dest1
如果使用下面的设置,每条消息会在dest1中输出两次:
log4j.rootCategory = INFO, dest1
log4j.category.MyCategory = DEBUG, dest1
记住,MyCategory继承了根类的设置。
可以指定子类不继承父类的设置,方法是指定log4j.additivity.*=false,如上例中:
log4j.rootCategory = INFO, dest1
log4j.additivity.MyCategory = false
log4j.category.MyCategory = DEBUG, dest1
2,http://wangjc-opal.iteye.com/blog/309924
log4j的强大功能无可置疑,但实际应用中免不了遇到某个功能需要输出独立的日志文件的情况,怎样才能把所需的内容从原有日志中分离,形成单独的日志文件呢?其实只要在现有的log4j基础上稍加配置即可轻松实现这一功能。
先看一个常见的log4j.properties文件,它是在控制台和myweb.log文件中记录日志:
log4j.rootLogger=DEBUG, stdout, logfile
log4j.category.org.springframework=ERROR
log4j.category.org.apache=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${myweb.root}/WEB-INF/log/myweb.log
log4j.appender.logfile.MaxFileSize=512KB
log4j.appender.logfile.MaxBackupIndex=5
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
================= 不同的类输出不同的文件:
如果想对不同的类输出不同的文件(以cn.com.Test为例),先要在Test.java中定义:
private static Log logger = LogFactory.getLog(Test.class);
然后在log4j.properties中加入:
log4j.logger.cn.com.Test= DEBUG, test
log4j.appender.test=org.apache.log4j.FileAppender
log4j.appender.test.File=${myweb.root}/WEB-INF/log/test.log
log4j.appender.test.layout=org.apache.log4j.PatternLayout
log4j.appender.test.layout.ConversionPattern=%d %p [%c] - %m%n
也就是让cn.com.Test中的logger使用log4j.appender.test所做的配置。
================= 同一个类输出多个日志文件:
但是,如果在同一类中需要输出多个日志文件呢?其实道理是一样的,先在Test.java中定义:
private static Log logger1 = LogFactory.getLog("myTest1");
private static Log logger2 = LogFactory.getLog("myTest2");
然后在log4j.properties中加入:
log4j.logger.myTest1= DEBUG, test1
log4j.appender.test1=org.apache.log4j.FileAppender
log4j.appender.test1.File=${myweb.root}/WEB-INF/log/test1.log
log4j.appender.test1.layout=org.apache.log4j.PatternLayout
log4j.appender.test1.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.myTest2= DEBUG, test2
log4j.appender.test2=org.apache.log4j.FileAppender
log4j.appender.test2.File=${myweb.root}/WEB-INF/log/test2.log
log4j.appender.test2.layout=org.apache.log4j.PatternLayout
log4j.appender.test2.layout.ConversionPattern=%d %p [%c] - %m%n
也就是在用logger时给它一个自定义的名字(如这里的"myTest1"),然后在log4j.properties中做出相应配置即可。别忘了不同日志要使用不同的logger(如输出到test1.log的要用logger1.info("abc"))。
==================== 继承性
还有一个问题,就是这些自定义的日志默认是同时输出到log4j.rootLogger所配置的日志中的,如何能只让它们输出到自己指定的日志中呢?别急,这里有个开关:
log4j.additivity.myTest1 = false
它用来设置是否同时输出到log4j.rootLogger所配置的日志中,设为false就不会输出到其它地方啦!注意这里的"myTest1"是你在程序中给logger起的那个自定义的名字!
如果你说,我只是不想同时输出这个日志到log4j.rootLogger所配置的logfile中,stdout里我还想同时输出呢!那也好办,把你的log4j.logger.myTest1 = DEBUG, test1改为下式就OK啦!
log4j.logger.myTest1=DEBUG, test1
===================== Threshold 修改appender的输出级别
### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG ## 输出DEBUG级别以上的日志
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存异常信息到单独文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = logs/error.log ## 异常日志文件名
log4j.appender.D.Append = true
log4j.appender.D.Threshold = ERROR ## 只输出ERROR级别以上的日志!!!
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
============================================================================================
example:
#ident "%W"
#it will log every thing
log4j.rootCategory=INFO
log4j.logger.actions=INFO, R
log4j.logger.dto=INFO, R
log4j.logger.util=INFO, R
log4j.logger.delegators=INFO, R
log4j.logger.ejbcore=INFO, R
log4j.logger.javacore=INFO, R
log4j.logger.xxx=INFO, R
log4j.logger.loanclient=INFO, R
log4j.logger.loan.position=INFO, R
log4j.logger.org.springframework=INFO, R
#log4j.debug=true
#log4j.disable=INFO
#log4j.disable=DEBUG
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=@LOAN_ROOT@/log/xxx.log
log4j.appender.R.MaxFileSize=50000KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=3
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout=org.apache.log4j.TTCCLayout
log4j.appender.R.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss, SSS} [%t] [%5p] %c{1}:[%L] - %m%n
# =============== POST TRADE PROPERTIES =====================
log4j.logger.loan.tesgateway=INFO, POSTTRADE
log4j.logger.loan.posttrade=INFO, POSTTRADE
log4j.logger.loan.clearpar=INFO, POSTTRADE
log4j.logger.loan.liveupdate=INFO, POSTTRADE
log4j.logger.org.springframework=INFO, POSTTRADE
log4j.appender.POSTTRADE=org.apache.log4j.RollingFileAppender
log4j.appender.POSTTRADE.File=@LOAN_ROOT@/log/post_trade.log
log4j.appender.POSTTRADE.MaxFileSize=50000KB
# Keep one backup file
log4j.appender.POSTTRADE.MaxBackupIndex=3
log4j.appender.POSTTRADE.layout=org.apache.log4j.PatternLayout
#log4j.appender.POSTTRADE.layout=org.apache.log4j.TTCCLayout
log4j.appender.POSTTRADE.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss, SSS} [%t] [%5p] %c{1}:[%L] - %m%n
# =============== VAILDATION SERVICE PROPERTIES =====================
log4j.appender.VAILDATION=org.apache.log4j.RollingFileAppender
log4j.appender.VAILDATION.File=@LOAN_ROOT@/log/valcalc_service.log
log4j.appender.VAILDATION.MaxFileSize=50000KB
# Keep one backup file
log4j.appender.VAILDATION.MaxBackupIndex=1
log4j.appender.VAILDATION.layout=org.apache.log4j.PatternLayout
#log4j.appender.VAILDATION.layout=org.apache.log4j.TTCCLayout
log4j.appender.VAILDATION.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss, SSS} [%t] [%5p] %c{1}:[%L] - %m%n
log4j.logger.loan.calculator=INFO, VAILDATION
log4j.logger.loan.common=INFO, VAILDATION
log4j.logger.loan.framework=INFO, VAILDATION
log4j.logger.loan.validator=INFO, VAILDATION
log4j.logger.loan.propertiescache=INFO, VAILDATION
log4j.logger.loan.account=INFO, VAILDATION
private Category cat = Category.getInstance(loan.posttrade.common.publisher.GFITradePublisher.class.getName());
this will be logged in post_trade.log
原文:http://blog.csdn.net/caolaosanahnu/article/details/6771075