规则-如果类P的名字是另一个类C的名字的前缀,且P与C之间以“.”号连接起来,那么称P为祖先层次;如果层次A与其子层次之间没有任何父层次,则认为层次A为父层次。
|
规则-设当前logger为X,从X开始往X的父类方向开始算(包括X本身),直到名为root的logger,第一个不为null的级别值就是X的级别值。
|
规则-若当前请方式级别为P,而当前的logger的级别为Q,当且仅当在P>=Q的情况下,日志信息才能显示。
|
DEBUG < INFO < WARN < ERROR < FATAL
。
关于此规则的说明,有以下代码为实例:
// get a logger instance named "com.foo"
Logger logger = Logger.getLogger("com.foo");
// Now set its level. Normally you do not need to set the
// level of a logger programmatically. This is usually done
// in configuration files.
logger.setLevel(Level.INFO);
Logger barlogger = Logger.getLogger("com.foo.Bar");
// This request is enabled, because WARN >= INFO.
logger.warn("Low fuel level.");
// This request is disabled, because DEBUG < INFO.
logger.debug("Starting search for nearest gas station.");
// The logger instance barlogger, named "com.foo.Bar",
// will inherit its level from the logger named
// "com.foo" Thus, the following request is enabled
// because INFO >= INFO.
barlogger.info("Located nearest gas station.");
// This request is disabled, because DEBUG < INFO.
barlogger.debug("Exiting gas station search");
|
以下表格可以清晰说明此规则:
对于layout,也正如前面所述,是用来定义日志信息的输出格式,它的与C语言中Logger
Name Added
Appenders Additivity
Flag Output Targets Comment root A1 not applicable A1 The root logger is anonymous but can be accessed with the Logger.getRootLogger() method. There is no default appender attached to root. x A-x1, A-x2 true A1, A-x1, A-x2 Appenders of "x" and root. x.y none true A1, A-x1, A-x2 Appenders of "x" and root. x.y.z A-xyz1 true A1, A-x1, A-x2, A-xyz1 Appenders in "x.y.z", "x" and root. security A-sec false A-sec No appender accumulation since the additivity flag is set to false
. security.access none true A-sec Only appenders of "security" because the additivity flag in "security" is set to false
. printf
函数的格式定义基本相似,本人对printf中的格式参数不太熟悉,总感觉有些复杂,一般是平时看到自己认为有用的格式就记一下,到时直接搬过来。如
log4j.appender.R.layout.ConversionPattern=--->%-d{yyyy-MM-dd HH:mm:ss} [%5p]%l - %m%n
时,日志信息格式为:
--->2008-07-11 01:13:40 [ INFO]com.test.Log4jTest.main(Log4jTest.java:27) - Exiting application.
好啦,现在log4j的理论很肤浅地扯了一下,现在可以开始配置了。
配置:
Log4j的配置可以通过加载Java的properties配置文件或者XML文件来完成。
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=--->%-d{yyyy-MM-dd HH:mm:ss} [%5p]%l - %m%n
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.File=myapp.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=1024KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=10
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern==[slf5s.start]%d{DATE}[slf5s.DATE]%n/
%p[slf5s.PRIORITY]%n%x[slf5s.NDC]%n%t[slf5s.THREAD]%n/
%c[slf5s.CATEGORY]%n%l[slf5s.LOCATION]%n%m[slf5s.MESSAGE]%n%n
|