这样,就可以关闭Appender的遗传效应了。
具体解释如下 :
Appender Additivity The output of a log statement of Logger L will go to all the Appenders in the LoggerConfig associated with L and the ancestors of that LoggerConfig. This is the meaning of the term \However, if an ancestor of the LoggerConfig associated with Logger L, say P, has the additivity flag set to false, then L's output will be directed to all the appenders in L's LoggerConfig and it's ancestors up to and including P but not the Appenders in any of the ancestors of P. Loggers have their additivity flag set to true by default.
上面例子的详细表格解释如下: Logger Name root Added Appenders A1 Additivity Flag not applicable true Output Targets A1 Comment The root logger has no parent so additivity does not apply to it. Appenders of \x A-x1, A-x2 A1, A-x1, A-x2 A1, A-x1, A-x2 x.y none true Appenders of \typical to configure a Logger with no Appenders. Appenders in \x.y.z A-xyz1 true A1, A-x1, A-x2, A-xyz1 A-sec security A-sec false No appender accumulation since the additivity flag is set to false. security.access none true A-sec Only appenders of \additivity flag in \false.
9. Layout
通常,用户不止希望能定义log输出的位置,还希望可以定义输出的格式。这就可以通过将Appender与一个layout相关联来实现。Log4j中的 一个标准定义PatternLayout,就允许用户使用一种类似C语言printf函数的打印格式, 如\格式在真实环境下会打印类似如下的信息: 176 [main] INFO org.foo.Bar - Located nearest gas station.
第一个字段是启动的毫秒数,第二个字段是日志请求的线程号, 第三个字段是日志请求级别,第四个是与日志请求相关联的日志名称, 在“-” 之后的内容就是日志内容。
Log4j有各种不同的 Layouts, 例如 : JSON, XML, HTML, 和Syslog (including the new RFC 5424 version). 其他一些appenders例如database connectors会使用指定的字段替代特定的文本布局。
同样重要的是,要使得log4j的日志消息内容符合用户指定的标准。,例如:在你的工程中,你需要一个Oranges类型的日志对象,你就可以创建一个OrangeMessage,可以接受Orange实例并且传递到log4j,Orange对象就会被格式化为一个适当的字节数组。
10. StrSubstitutor and StrLookup
StrSubstitutor 类与 StrLookup接口是来自Apache Commons Lang, 并且被修改为支持 LogEvents 的运算。
此外 来自Apache Commons Configuration的 Interpolator类允许StrSubstitutor 运算来着多个StrLookups 的变量,它也被修改支持LogEvents的运算。
Log4j2提供了一种机制,可以使得configuration引用 来自System Properties, the configuration file, the ThreadContext Map, StructuredData中的变量。
当 configuration被加载或者 每个LogEvent 运行时,这个变量将被解析。如果这个日志组件可以处理这个变量。
三.Log4j2 API
Log4j2API提供了应用程序使用的接口,也提供了创建一个日志实现所需的适配器组件, Log4j2解耦了API及其实现之间的关系。这样做的目的是不允许有多个实现 。
一种可能的情况是 一个Log4j2的API对应多个实现,所以在应用中要使用 Log4j2 API中的类和方法, 而不要使用具体的实现内容
(1)Hello World!
下面是一个helloworld的例子, 有一个从LogManager中获取的 名称为 HelloWorld的Logger,接着,这个Logger打印了 \的日志消息。 当然,只有在configured 中做了相应的配置,这些内容才能打印出来。
1. import org.apache.logging.log4j.LogManager; 2. import org.apache.logging.log4j.Logger; 3.
4. publicclassHelloWorld{
5. privatestaticfinalLogger logger =LogManager.getLogger(\); 6. publicstaticvoid main(String[] args){ 7. logger.info(\); 8. }
9. }
调用方法 logger.info()的输出会有所不同,主要取决于 configuration的配置
(2) Substituting Parameters
日志记录的目的是要提供系统中发生的信息,这就要打印一些对象信息,在 Log4j1.x中, 可以通过这样来打印 :
1. if(logger.isDebugEnabled()){
2. logger.debug(\+ user.getName()+\
\+user.getBirthdayCalendar()); 3. }
这样的代码感觉很繁琐,而且会检查2次日志级别,一次是 logger.isDebugEnabled,另一次是logger.debug。
在log4j2中更好的实现方式是 :
logger.debug(\in user {} with birthday {}\, user.getName(), user.getBirthdayCalendar()); 代码简洁了很多, 而且日志只有在debug 打印的时候才会构建日志内容中的字符串。
(3) Formatting Parameters
类似于java的Formatter, Formatter Loggers 可以提供简易的字符串格式化方式 : 例如:
1. publicstaticLogger logger =LogManager.getFormatterLogger(\); 2.
3. logger.debug(\, user.getName(),
user.getBirthdayCalendar());
4. logger.debug(\, user.getName(),
user.getBirthdayCalendar());
5. logger.debug(\,Integer.MAX_VALUE); 6. logger.debug(\,Long.MAX_VALUE);
要使用格式化日志,必须调用 LogManager getFormatterLogger的方法。 下面展示了例子中的结果内容 :
1. 2012-12-1211:56:19,633[main] DEBUG:UserJohnSmithwith birthday
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\,offset=-18000000,dstSavings=3600000
,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=1995,MONTH=4,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=23,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]
2. 2012-12-1211:56:19,643[main] DEBUG:UserJohnSmithwith birthday 0523,1995 3. 2012-12-1211:56:19,643[main] DEBUG:Integer.MAX_VALUE =2,147,483,647
4. 2012-12-1211:56:19,643[main] DEBUG:Long.MAX_VALUE =9,223,372,036,854,775,807
(4) Mixing Loggers with Formatter Loggers
Formatter loggers是用来控制细粒度的输出格式的。但是缺点是必须指定正确的类型,例如给 %d 格式化参数传递了 Integer以外的类型,就会抛错。
如果你主要使用 {} 风格的参数, 但是偶尔要精确控制输出格式,可以使用 printf方法:
1. publicstaticLogger logger =LogManager.getLogger(\); 2.
3. logger.debug(\, someDataSource);
4. logger.printf(Level.INFO,\,
user.getName(), user.getBirthdayCalendar());
(5) Java 8 lambda support for lazy logging
在2.4版本以后,Logger 支持了lambda表达式,在请求日志级别启动的时候,可以懒加载的打印这些信息而不用去显式检查日志是否要打印。 例如, 在以前版本中可能这样写 :
1. // pre-Java 8 style optimization: explicitly check the log level
2. // to make sure the expensiveOperation() method is only called if necessary 3. if(logger.isTraceEnabled()){
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库log4j2中文手册(3)在线全文阅读。
相关推荐: