码迷,mamicode.com
首页 > Web开发 > 详细

Log4j – Configuring Log4j 2 - Apache Log4j 2

时间:2017-02-13 08:24:22      阅读:778      评论:0      收藏:0      [点我收藏+]

标签:比较   mos   ctc   tde   dex   gen   计划   isp   日志信息   

技术分享技术分享

 

Configuration

Inserting log requests into the application code requires a fair amount of planning and effort. Observation shows that approximately 4 percent of code is dedicated to logging. Consequently, even moderately sized applications will have thousands of logging statements embedded within their code. Given their number, it becomes imperative to manage these log statements without the need to modify them manually.(在应用程序代码中插入日志需要大量的计划和努力。观察表明,约百分之4的代码专门用于日志管理。因此,即使是中等大小的应用程序也会有成千上万的日志语句嵌入到它们的代码中。考虑到它们的数量,就必须对这些日志语句进行管理,而不需要手工修改代码。)

Configuration of Log4j 2 can be accomplished in 1 of 4 ways:(配置Log4j 2可以使用下面4个方法中的一个来完成:)

  1. Through a configuration file written in XML, JSON, YAML, or properties format.(通过一个XML, JSON, YAML, 或properties 格式的配置文件
  2. Programmatically, by creating a ConfigurationFactory and Configuration implementation.(以编程方式)
  3. Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.以编程方式
  4. Programmatically, by calling methods on the internal Logger class.以编程方式

This page focuses primarily on configuring Log4j through a configuration file. Information on programmatically configuring Log4j can be found at Extending Log4j 2 and Programmatic Log4j Configuration.(这个章节的焦点是通过配置文件来配置Log4j。通过编程方式来配置Log4j的相关信息可以在Extending Log4j 2 和Programmatic Log4j Configuration 中找到。)

Note that unlike Log4j 1.x, the public Log4j 2 API does not expose methods to add, modify or remove appenders and filters or manipulate the configuration in any way.(请注意,与log4j 1.x不同,log4j 2 的Public API没有公开用来添加、修改或删除appender和filters或以任何方式操纵配置的方法

Automatic Configuration(自动配置)

Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML.(log4j具有在初始化过程中自动配置自身的能力。当log4j启动就会找到所有的configurationfactory插件和安排他们在从最高到最低的加权进行排序。log4j包含四种ConfigurationFactory 实现:一个JSON,一个YAML,一个properties,和一个XML。)

  1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.(log4j将检查 "log4j.configurationFile" 系统属性,如果设置了这个属性,将尝试使用匹配这个文件扩展名的 ConfigurationFactory 加载配置。)
  2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.(如果"log4j.configurationFile"系统属性没有被设置,properties ConfigurationFactory  就会去查找classpath 中的 log4j2-test.properties 
  3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.如果上面的文件都没有找到YAML ConfigurationFactory  就会去查找classpath 中的log4j2-test.yaml 或log4j2-test.yml )
  4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.(如果上面的文件都没有找到,JSON ConfigurationFactory  就会去查找classpath 中的 log4j2-test.json or log4j2-test.jsn
  5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.(如果上面的文件都没有找到,XML ConfigurationFactory  就会去查找classpath 中的log4j2-test.xml
  6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.(如果找不到测试文件,properties ConfigurationFactory  就会去查找classpath 中的 log4j2.properties 
  7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.(如果找不到 properties 文件,YAML ConfigurationFactory  就会去查找classpath 中的 log4j2.yaml or log4j2.yml 
  8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.(如果找不到  YAML 文件, JSON ConfigurationFactory  就会去查找classpath 中的 log4j2.json or log4j2.jsn 
  9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.(如果找不到  JSON 文件, XML ConfigurationFactory  就会去查找classpath 中的 log4j2.xml 
  10. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.(如果找不到任何配置文件, 就会使用 DefaultConfiguration 。它会将日志输出在控制台中 

An example application named MyApp that uses log4j can be used to illustrate how this is done.(一个使用log4j 的例子,可以用来说明Log4j的自动配置是如何完成的。)

  1. import com.foo.Bar;
  2.  
  3. // Import log4j classes.
  4. import org.apache.logging.log4j.Logger;
  5. import org.apache.logging.log4j.LogManager;
  6.  
  7. publicclassMyApp{
  8.  
  9. // Define a static logger variable so that it references the
  10. // Logger instance named "MyApp".
  11. privatestaticfinalLogger logger =LogManager.getLogger(MyApp.class);
  12.  
  13. publicstaticvoid main(finalString... args){
  14.  
  15. // Set up a simple configuration that logs on the console.
  16.  
  17. logger.trace("Entering application.");
  18. Bar bar =newBar();
  19. if(!bar.doIt()){
  20. logger.error("Didn‘t do it.");
  21. }
  22. logger.trace("Exiting application.");
  23. }
  24. }

MyApp begins by importing log4j related classes. It then defines a static logger variable with the name MyApp which happens to be the fully qualified name of the class.(MyApp通过导入log4j相关的类开始。然后使用了MyApp的全限定类名定义了一个静态的日志记录器。)

MyApp uses the Bar class defined in the packagecom.foo.

  1. package com.foo;
  2. import org.apache.logging.log4j.Logger;
  3. import org.apache.logging.log4j.LogManager;
  4.  
  5. publicclassBar{
  6. staticfinalLogger logger =LogManager.getLogger(Bar.class.getName());
  7.  
  8. publicboolean doIt(){
  9. logger.entry();
  10. logger.error("Did it again!");
  11. return logger.exit(false);
  12. }
  13. }

Log4j will provide a default configuration if it cannot locate a configuration file. The default configuration, provided in the DefaultConfiguration class, will set up:(Log4j在找不到配置文件的时候,提供了一个默认的配置。默认配置由DefaultConfiguration 类提供,会建立:

  • A ConsoleAppender attached to the root logger.(为root logger添加一个ConsoleAppender 
  • A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" attached to the ConsoleAppender(为ConsoleAppender 添加一个模式为"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" 的PatternLayout 

Note that by default Log4j assigns the root logger to Level.ERROR.(注意root logger 的默认级别为 Level.ERROR

The output of MyApp would be similar to:

17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
17:13:01.540 [main] ERROR MyApp - Didn‘t do it.

As was described previously, Log4j will first attempt to configure itself from configuration files. A configuration equivalent to the default would look like:(正如上面描述的那样,Log4j首先会尝试从配置文件中读取配置。和默认配置等价的配置文件如下:)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="WARN">
  3. <Appenders>
  4. <Consolename="Console"target="SYSTEM_OUT">
  5. <PatternLayoutpattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6. </Console>
  7. </Appenders>
  8. <Loggers>
  9. <Rootlevel="error">
  10. <AppenderRefref="Console"/>
  11. </Root>
  12. </Loggers>
  13. </Configuration>

Once the file above is placed into the classpath as log4j2.xml you will get results identical to those listed above. Changing the root level to trace will result in results similar to:(一旦上面的文件被命名为log4j2.xml并放在classpath 下,你就会得到和上面的输出结果等价的结果。将  root level 修改为trace会得到类似如下的结果:)

17:13:01.540 [main] TRACE MyApp - Entering application.
17:13:01.540 [main] TRACE com.foo.Bar - entry
17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
17:13:01.540 [main] TRACE com.foo.Bar - exit with (false)
17:13:01.540 [main] ERROR MyApp - Didn‘t do it.
17:13:01.540 [main] TRACE MyApp - Exiting application.

Note that status logging is disabled when the default configuration is used.(如果提供了默认的level 则 Configuration 节点中的status 无效)

Additivity(附加性)

Perhaps it is desired to eliminate all the TRACE output from everything except com.foo.Bar. Simply changing the log level would not accomplish the task. Instead, the solution is to add a new logger definition to the configuration:(也许你想让所有的Trace级别的日志都不输出,除了com.foo.bar。仅仅更改日志级别将无法达到你的目的。相反,解决方法是添加一个新的日志记录器定义:)

  1. <Loggername="com.foo.Bar"level="TRACE"/>
  2. <Rootlevel="ERROR">
  3. <AppenderRefref="STDOUT">
  4. </Root>

With this configuration all log events from com.foo.Bar will be recorded while only error events will be recorded from all other components. (使用这个配置,所有的来自于com.foo.Bar 包下的日志事件都会被记录,然而其它包下的只有error级别的日志事件会被记录。)

In the previous example all the events from com.foo.Bar were still written to the Console. This is because the logger for com.foo.Bar did not have any appenders configured while its parent did. In fact, the following configuration(在先前的例子中,所有的来自于 com.foo.Bar 包下的事件仍然会写入到Console 。这是因为 com.foo.Bar  的日志记录器没有配置任何的appenders 然而它的父元素Root配置了。实际上,它的配置如下: )

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="WARN">
  3. <Appenders>
  4. <Consolename="Console"target="SYSTEM_OUT">
  5. <PatternLayoutpattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6. </Console>
  7. </Appenders>
  8. <Loggers>
  9. <Loggername="com.foo.Bar"level="trace">
  10. <AppenderRefref="Console"/>
  11. </Logger>
  12. <Rootlevel="error">
  13. <AppenderRefref="Console"/>
  14. </Root>
  15. </Loggers>
  16. </Configuration>

would result in

17:13:01.540 [main] TRACE com.foo.Bar - entry
17:13:01.540 [main] TRACE com.foo.Bar - entry
17:13:01.540 [main] ERROR com.foo.Bar - Did it again!
17:13:01.540 [main] TRACE com.foo.Bar - exit (false)
17:13:01.540 [main] TRACE com.foo.Bar - exit (false)
17:13:01.540 [main] ERROR MyApp - Didn‘t do it.

Notice that the trace messages from com.foo.Bar appear twice. This is because the appender associated with logger com.foo.Bar is first used, which writes the first instance to the Console. Next, the parent of com.foo.Bar, which in this case is the root logger, is referenced. The event is then passed to its appender, which is also writes to the Console, resulting in the second instance. This is known as additivity. While additivity can be quite a convenient feature (as in the first previous example where no appender reference needed to be configured), in many cases this behavior is considered undesirable and so it is possible to disable it by setting the additivity attribute on the logger to false:(注意,来自于 com.foo.Bar 包下的日志出现了两次。这是因为和com.foo.Bar 日志记录器相关的appender  被第一次使用时,它会将第一个实例写到控制台。接下来又被com.foo.Bar 的父级--root logger 引用。然后当事件被传递给root loggerappender时,它也会将信息写入到控制台,这就导致了两次输出。这就是所谓的附加性。然而additivity 可以是一个相当方便的功能(正如在先前的第一个例子中,他不需要配置 AppenderRef 而是直接使用root logger  的AppenderRef ),在许多情况下,这种行为被视为不受欢迎的,并且很可能通过设置additivity 属性值为false将它禁用 。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="WARN">
  3. <Appenders>
  4. <Consolename="Console"target="SYSTEM_OUT">
  5. <PatternLayoutpattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
  6. </Console>
  7. </Appenders>
  8. <Loggers>
  9. <Loggername="com.foo.Bar"level="trace"additivity="false">
  10. <AppenderRefref="Console"/>
  11. </Logger>
  12. <Rootlevel="error">
  13. <AppenderRefref="Console"/>
  14. </Root>
  15. </Loggers>
  16. </Configuration>

Once an event reaches a logger with its additivity set to false the event will not be passed to any of its parent loggers, regardless of their additivity setting.(一旦一个事件传递到一个additivity属性值为false的logger ,这个事件就不会再被传递到它的父loggers 

Automatic Reconfiguration(自动重新配置)

When configured from a File, Log4j has the ability to automatically detect changes to the configuration file and reconfigure itself. If the monitorInterval attribute is specified on the configuration element and is set to a non-zero value then the file will be checked the next time a log event is evaluated and/or logged and the monitorInterval has elapsed since the last check. The example below shows how to configure the attribute so that the configuration file will be checked for changes only after at least 30 seconds have elapsed. The minimum interval is 5 seconds.(当从配置文件中读取配置时,log4j具有自动检测配置文件变化和重新配置自身的能力。如果在配置元素上指定 monitorInterval 属性并且它的值为非0,则日志文件会在下一次的日志事件评估/日志记录并且经过monitorinterval设置的时间后被重新检查。下面的示例演示如何配置属性,以便在至少30秒之后检查配置文件的更改。最小间隔为5秒。)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ConfigurationmonitorInterval="30">
  3. ...
  4. </Configuration>

Chainsaw can automatically process your log files (Advertising appender configurations)(Chainsaw能够自动处理你的日志文件(通知 appender  配置))

Log4j provides the ability to ‘advertise‘ appender configuration details for all file-based appenders as well as socket-based appenders. For example, for file-based appenders, the file location and the pattern layout in the file are included in the advertisement. Chainsaw and other external systems can discover these advertisements and use that information to intelligently process the log file.(log4j提供了为所有基于文件以及基于Socket的appenders ‘宣传appender的配置细节的能力 。例如,基于文件的appender,它们的文件位置和pattern layout都被包含在”广告“中。Chainsaw 和其他外部系统可以发现这些”广告“并使用这些信息智能化地处理日志文件)

The mechanism by which an advertisement is exposed, as well as the advertisement format, is specific to each Advertiser implementation. An external system which would like to work with a specific Advertiser implementation must understand how to locate the advertised configuration as well as the format of the advertisement. For example, a ‘database‘ Advertiser may store configuration details in a database table. An external system can read that database table in order to discover the file location and the file format.针对每个广告商实现 都有其广告暴露的机制,以及这个广告的格式。要与特定的广告商实现一起工作的外部系统必须了解如何定位广告配置以及广告格式。例如,“数据库”广告商可以将配置细节存储在数据库表中。外部系统可以读取数据库表以发现文件位置和文件格式。)

Log4j provides one Advertiser implementation, a ‘multicastdns‘ Advertiser, which advertises appender configuration details via IP multicast using the http://jmdns.sourceforge.net library.(Log4j提供了一个广告商实现---‘multicastdns‘ Advertiser 它会通过使用 http://jmdns.sourceforge.net 库的IP广播对appender的配置细节进行广告)

Chainsaw automatically discovers log4j‘s multicastdns-generated advertisements and displays those discovered advertisements in Chainsaw‘s Zeroconf tab (if the jmdns library is in Chainsaw‘s classpath). To begin parsing and tailing a log file provided in an advertisement, just double-click the advertised entry in Chainsaw‘s Zeroconf tab. Currently, Chainsaw only supports FileAppender advertisements.Chainsaw 自动发现Log4j的multicastdns生成的广告并且会在 Chainsaw‘s Zeroconf tab 展示那些被发现的广告(如果jmdnslibrary  在Chainsaw的 classpath)。开始解析并且跟踪在广告中提供的日志文件,只要在Chainsaw的Zeroconf tab 中双击广告记录。目前,Chainsaw 只支持FileAppender广告。)

To advertise an appender configuration:(为了广告一个appender  的配置:)

  • Add the JmDns library from http://jmdns.sourceforge.net to the application classpath(从 http://jmdns.sourceforge.net 将JmDns库添加到应用程序的classpath中
  • Set the ‘advertiser‘ attribute of the configuration element to ‘multicastdns‘(设置configuration  元素的‘advertiser ’属性值为 ‘multicastdns‘
  • Set the ‘advertise‘ attribute on the appender element to ‘true‘设置appender 元素的‘advertiser ’属性值为 ‘true‘
  • If advertising a FileAppender-based configuration, set the ‘advertiseURI‘ attribute on the appender element to an appropriate URI(如果广告一个基于FileAppender 的配置,设置 appender  元素的‘advertiseURI’属性值为 一个合适的URI 

FileAppender-based configurations require an additional ‘advertiseURI‘ attribute to be specified on the appender. The ‘advertiseURI‘ attribute provides Chainsaw with information on how the file can be accessed. For example, the file may be remotely accessible to Chainsaw via ssh/sftp by specifying a Commons VFS (http://commons.apache.org/proper/commons-vfs/) sftp:// URI, an http:// URI may be used if the file is accessible through a web server, or a file:// URI can be specified if accessing the file from a locally-running instance of Chainsaw.(基于FileAppender 的配置需要指定一个额外的 ‘advertiseURI‘ 。‘advertiseURI‘ 将如何去访问文件的信息提供给Chainsaw。例如,文件可能要Chainsaw 通过  ssh/sftp  进行远程访问,通过指定一个通用的VFS (http://commons.apache.org/proper/commons-vfs/) sftp:// URI ,如果文件可以通过web 服务器访问可以使用 http:// URI,如果可以通过本地允许的Chainsaw 实例来访问文件则可以使用file:// URI 

Here is an example advertisement-enabled appender configuration which can be used by a locally-running Chainsaw to automatically tail the log file (notice the file:// advertiseURI):(这是一个激活了广告的appender配置,它可以使用本地允许的Chainsaw  来自动跟踪日志文件 (注意使用 file:// advertiseURI)

Please note, you must add the JmDns library from http://jmdns.sourceforge.net to your application classpath in order to advertise with the ‘multicastdns‘ advertiser.(请注意,你一定要从http://jmdns.sourceforge.net  将JmDns  库添加到你的应用程序中的classpath ,这样才能够成功使用 ‘multicastdns‘ 进行广告。 )

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationadvertiser="multicastdns">
  3. ...
  4. </Configuration>
  5. <Appenders>
  6. <Filename="File1"fileName="output.log"bufferedIO="false"advertiseURI="file://path/to/output.log"advertise="true">
  7. ...
  8. </File>
  9. </Appenders>

Configuration Syntax(配置语法)

As the previous examples have shown as well as those to follow, Log4j allows you to easily redefine logging behavior without needing to modify your application. It is possible to disable logging for certain parts of the application, log only when specific criteria are met such as the action being performed for a specific user, route output to Flume or a log reporting system, etc. Being able to do this requires understanding the syntax of the configuration files.(如前面的例子所显示的那样,Log4j允许您轻松地重新定义记录行为,而不需要修改应用程代码。可以禁用应用程序的某些部分的日志,日志只有当特定条件满足才会将日志信息输出到Flume 或日志报告系统等。要能够做到这一点需要去了解配置文件的语法。)

The configuration element in the XML file accepts several attributes:(在XML文件中的配置元素接受的几个属性:)

Attribute NameDescription
advertiser (Optional) The Advertiser plugin name which will be used to advertise individual FileAppender or SocketAppender configurations. The only Advertiser plugin provided is ‘multicastdns".((可选的)广告商插件名,他会被用来广告 FileAppender  或 SocketAppender  的配置信息。现在只支持 ‘multicastdns"广告商插件
dest Either "err", which will send output to stderr, or a file path or URL.(值为”err",a file path,URL 3者之一。如果为“err”,信息会被输出到stderr)
monitorInterval The minimum amount of time, in seconds, that must elapse before the file configuration is checked for changes.(下一次检查配置文件需要间隔的秒数)
name The name of the configuration.(配置的名称)
packages A comma separated list of package names to search for plugins. Plugins are only loaded once per classloader so changing this value may not have any effect upon reconfiguration.(查找插件的名,多个包名用逗号分隔。每个类加载器只会加载一次插件,所有改变它的值在reconfiguration 时不会有任何的效果)
schema Identifies the location for the classloader to located the XML Schema to use to validate the configuration. Only valid when strict is set to true. If not set no schema validation will take place.(类加载器加载用来校验配置文件的XML Schema文件的位置。只有当strict属性设置为true时才会校验。如果没有设置,那么schema就不会起作用。) 
shutdownHook Specifies whether or not Log4j should automatically shutdown when the JVM shuts down. The shutdown hook is enabled by default but may be disabled by setting this attribute to "disable"(指定当JVM关闭的时候是否允许Log4j自动关闭。默认的值为enable但是可以将值设置为“disable"来关闭该功能。)
shutdownTimeout Specifies how many milliseconds appenders and background tasks will get to shutdown when the JVM shuts down. Default is zero which mean that each appender uses its default timeout, and don‘t wait for background tasks. Not all appenders will honor this, it is a hint and not an absolute guarantee that the shutdown procedure will not take longer. Setting this too low increase the risk of losing outstanding log events not yet written to the final destination. See LoggerContext.stop(long, java.util.concurrent.TimeUnit). (Not used if shutdownHook is set to "disable".)(指定当JVM关闭的时候再过多少毫秒会关闭appenders和后台任务。默认值为0,意味着每个appender使用它们默认的超市,并且不会等待后台任务。并不是对所有的appenders都有效,它只是一个微量的并且不会绝对的保证关闭处理不会花更长的时间。参考 LoggerContext.stop(long, java.util.concurrent.TimeUnit) (如果shutdownHook 设置为”disable“不要使用该属性。))
status The level of internal Log4j events that should be logged to the console. Valid values for this attribute are "trace", "debug", "info", "warn", "error" and "fatal". Log4j will log details about initialization, rollover and other internal actions to the status logger. Setting status="trace" is one of the first tools available to you if you need to troubleshoot log4j.(Log4j日志事件的等级打印在控制台。校验已下的这些值 "trace", "debug", "info", "warn", "error" and "fatal" 。Log4j会将初始化时的详细信息 ,rollover和其它内部操作都记录到status日志记录器中。设置 status="trace" 这是对你来说可以用来排除log4j故障的第一个工具。
strict Enables the use of the strict XML format. Not supported in JSON configurations.(激活严格XML格式的使用。在JSON配置中不支持)
verbose Enables diagnostic information while loading plugins.(在加载插件的时候激活诊断性测试信息)

Log4j can be configured using two XML flavors; concise and strict. The concise format makes configuration very easy as the element names match the components they represent however it cannot be validated with an XML schema. For example, the ConsoleAppender is configured by declaring an XML element named Console under its parent appenders element. However, element and attribute names are are not case sensitive. In addition, attributes can either be specified as an XML attribute or as an XML element that has no attributes and has a text value. So(Log4j能够使用XML风格进行配置;简洁的或者是严格的。简洁的格式使得配置非常容易,因为元素名匹配了他们表示的组件,即使它在XML schema中校验不通过。例如,可以在父appenders下申明一个叫做Console的XML元素来配置一个 ConsoleAppender 。但是元素和属性名是不区分大小写的。此外,属性也可以指定为一个没有属性只有一个文本值得XML元素。

<PatternLayoutpattern="%m%n"/>

and

 
<PatternLayout><Pattern>%m%n</Pattern></PatternLayout>
 

are equivalent.

The file below represents the structure of an XML configuration, but note that the elements in italics below represent the concise element names that would appear in their place.(下面的文件呈现的是一个XML配置的结构,但是注意下面的斜体内容表示是简洁方式配置的。)

  1. <?xml version="1.0" encoding="UTF-8"?>;
  2. <Configuration>
  3. <Properties>
  4. <Propertyname="name1">value</property>
  5. <Propertyname="name2"value="value2"/>
  6. </Properties>
  7. <filter ... />
  8. <Appenders>
  9. <appender ... >
  10. <filter ... />
  11. </appender>
  12. ...
  13. </Appenders>
  14. <Loggers>
  15. <Loggername="name1">
  16. <filter ... />
  17. </Logger>
  18. ...
  19. <Rootlevel="level">
  20. <AppenderRefref="name"/>
  21. </Root>
  22. </Loggers>
  23. </Configuration>

See the many examples on this page for sample appender, filter and logger declarations.(查看该页面的更多其它的例子来比较sample appender, filter and logger declarations

Strict XML(严格模式)

In addition to the concise XML format above, Log4j allows configurations to be specified in a more "normal" XML manner that can be validated using an XML Schema. This is accomplished by replacing the friendly element names above with their object type as shown below. For example, instead of the ConsoleAppender being configuerd using an element named Console it is instead configured as an appender element with a type attribute containing "Console".(此外)

  1. <?xml version="1.0" encoding="UTF-8"?>;
  2. <Configuration>
  3. <Properties>
  4. <Propertyname="name1">value</property>
  5. <Propertyname="name2"value="value2"/>
  6. </Properties>
  7. <Filtertype="type" ... />
  8. <Appenders>
  9. <Appendertype="type"name="name">
  10. <Filtertype="type" ... />
  11. </Appender>
  12. ...
  13. </Appenders>
  14. <Loggers>
  15. <Loggername="name1">
  16. <Filtertype="type" ... />
  17. </Logger>
  18. ...
  19. <Rootlevel="level">
  20. <AppenderRefref="name"/>
  21. </Root>
  22. </Loggers>
  23. </Configuration>

Below is a sample configuration using the strict format.(使用严格模式的一个例子)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="debug"strict="true"name="XMLConfigTest"
  3. packages="org.apache.logging.log4j.test">
  4. <Properties>
  5. <Propertyname="filename">target/test.log</Property>
  6. </Properties>
  7. <Filtertype="ThresholdFilter"level="trace"/>
  8.  
  9. <Appenders>
  10. <Appendertype="Console"name="STDOUT">
  11. <Layouttype="PatternLayout"pattern="%m MDC%X%n"/>
  12. <Filters>
  13. <Filtertype="MarkerFilter"marker="FLOW"onMatch="DENY"onMismatch="NEUTRAL"/>
  14. <Filtertype="MarkerFilter"marker="EXCEPTION"onMatch="DENY"onMismatch="ACCEPT"/>
  15. </Filters>
  16. </Appender>
  17. <Appendertype="Console"name="FLOW">
  18. <Layouttype="PatternLayout"pattern="%C{1}.%M %m %ex%n"/><!-- class and line number -->
  19. <Filters>
  20. <Filtertype="MarkerFilter"marker="FLOW"onMatch="ACCEPT"onMismatch="NEUTRAL"/>
  21. <Filtertype="MarkerFilter"marker="EXCEPTION"onMatch="ACCEPT"onMismatch="DENY"/>
  22. </Filters>
  23. </Appender>
  24. <Appendertype="File"name="File"fileName="${filename}">
  25. <Layouttype="PatternLayout">
  26. <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
  27. </Layout>
  28. </Appender>
  29. <Appendertype="List"name="List">
  30. </Appender>
  31. </Appenders>
  32.  
  33. <Loggers>
  34. <Loggername="org.apache.logging.log4j.test1"level="debug"additivity="false">
  35. <Filtertype="ThreadContextMapFilter">
  36. <KeyValuePairkey="test"value="123"/>
  37. </Filter>
  38. <AppenderRefref="STDOUT"/>
  39. </Logger>
  40.  
  41. <Loggername="org.apache.logging.log4j.test2"level="debug"additivity="false">
  42. <AppenderRefref="File"/>
  43. </Logger>
  44.  
  45. <Rootlevel="trace">
  46. <AppenderRefref="List"/>
  47. </Root>
  48. </Loggers>
  49.  
  50. </Configuration>

Configuration with JSON

In addition to XML, Log4j can be configured using JSON. The JSON format is very similar to the concise XML format. Each key represents the name of a plugin and the key/value pairs associated with it are its attributes. Where a key contains more than a simple value it itself will be a subordinate plugin. In the example below, ThresholdFilter, Console, and PatternLayout are all plugins while the Console plugin will be assigned a value of STDOUT for its name attribute and the ThresholdFilter will be assigned a level of debug.

  1. {"configuration":{"status":"error","name":"RoutingTest",
  2. "packages":"org.apache.logging.log4j.test",
  3. "properties":{
  4. "property":{"name":"filename",
  5. "value":"target/rolling1/rollingtest-$${sd:type}.log"}
  6. },
  7. "ThresholdFilter":{"level":"debug"},
  8. "appenders":{
  9. "Console":{"name":"STDOUT",
  10. "PatternLayout":{"pattern":"%m%n"}
  11. },
  12. "List":{"name":"List",
  13. "ThresholdFilter":{"level":"debug"}
  14. },
  15. "Routing":{"name":"Routing",
  16. "Routes":{"pattern":"$${sd:type}",
  17. "Route":[
  18. {
  19. "RollingFile":{
  20. "name":"Rolling-${sd:type}","fileName":"${filename}",
  21. "filePattern":"target/rolling1/test1-${sd:type}.%i.log.gz",
  22. "PatternLayout":{"pattern":"%d %p %c{1.} [%t] %m%n"},
  23. "SizeBasedTriggeringPolicy":{"size":"500"}
  24. }
  25. },
  26. {"AppenderRef":"STDOUT","key":"Audit"},
  27. {"AppenderRef":"List","key":"Service"}
  28. ]
  29. }
  30. }
  31. },
  32. "loggers":{
  33. "logger":{"name":"EventLogger","level":"info","additivity":"false",
  34. "AppenderRef":{"ref":"Routing"}},
  35. "root":{"level":"error","AppenderRef":{"ref":"STDOUT"}}
  36. }
  37. }
  38. }

Note that in the RoutingAppender the Route element has been declared as an array. This is valid because each array element will be a Route component. This won‘t work for elements such as appenders and filters, where each element has a different name in the concise format. Appenders and filters can be defined as array elements if each appender or filter declares an attribute named "type" that contains the type of the appender. The following example illustrates this as well as how to declare multiple loggers as an array.

  1. {"configuration":{"status":"debug","name":"RoutingTest",
  2. "packages":"org.apache.logging.log4j.test",
  3. "properties":{
  4. "property":{"name":"filename",
  5. "value":"target/rolling1/rollingtest-$${sd:type}.log"}
  6. },
  7. "ThresholdFilter":{"level":"debug"},
  8. "appenders":{
  9. "appender":[
  10. {"type":"Console","name":"STDOUT","PatternLayout":{"pattern":"%m%n"}},
  11. {"type":"List","name":"List","ThresholdFilter":{"level":"debug"}},
  12. {"type":"Routing","name":"Routing",
  13. "Routes":{"pattern":"$${sd:type}",
  14. "Route":[
  15. {
  16. "RollingFile":{
  17. "name":"Rolling-${sd:type}","fileName":"${filename}",
  18. "filePattern":"target/rolling1/test1-${sd:type}.%i.log.gz",
  19. "PatternLayout":{"pattern":"%d %p %c{1.} [%t] %m%n"},
  20. "SizeBasedTriggeringPolicy":{"size":"500"}
  21. }
  22. },
  23. {"AppenderRef":"STDOUT","key":"Audit"},
  24. {"AppenderRef":"List","key":"Service"}
  25. ]
  26. }
  27. }
  28. ]
  29. },
  30. "loggers":{
  31. "logger":[
  32. {"name":"EventLogger","level":"info","additivity":"false",
  33. "AppenderRef":{"ref":"Routing"}},
  34. {"name":"com.foo.bar","level":"error","additivity":"false",
  35. "AppenderRef":{"ref":"Console"}}
  36. ],
  37. "root":{"level":"error","AppenderRef":{"ref":"STDOUT"}}
  38. }
  39. }
  40. }

The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-core</artifactId>
  4. <version>2.8.5</version>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>com.fasterxml.jackson.core</groupId>
  9. <artifactId>jackson-databind</artifactId>
  10. <version>2.8.5</version>
  11. </dependency>
  12.  
  13. <dependency>
  14. <groupId>com.fasterxml.jackson.core</groupId>
  15. <artifactId>jackson-annotations</artifactId>
  16. <version>2.8.5</version>
  17. </dependency>

Configuration with YAML

Log4j also supports using YAML for configuration files. The structure follows the same pattern as both the XML and YAML configuration formats. For example:

  1. Configuration:
  2. status: warn
  3. name:YAMLConfigTest
  4. properties:
  5. property:
  6. name: filename
  7. value: target/test-yaml.log
  8. thresholdFilter:
  9. level: debug
  10. appenders:
  11. Console:
  12. name: STDOUT
  13. PatternLayout:
  14. Pattern:"%m%n"
  15. File:
  16. name:File
  17. fileName: ${filename}
  18. bufferedIO:false
  19. PatternLayout:
  20. Pattern:"%d %p %C{1.} [%t] %m%n"
  21. List:
  22. name:List
  23. Filters:
  24. ThresholdFilter:
  25. level: error
  26.  
  27. Loggers:
  28. logger:
  29. -
  30. name: org.apache.logging.log4j.test1
  31. level: debug
  32. additivity:false
  33. ThreadContextMapFilter:
  34. KeyValuePair:
  35. key: test
  36. value:123
  37. AppenderRef:
  38. ref: STDOUT
  39. -
  40. name: org.apache.logging.log4j.test2
  41. level: debug
  42. additivity:false
  43. AppenderRef:
  44. ref:File
  45. Root:
  46. level: error
  47. AppenderRef:
  48. ref: STDOUT

In order to use YAML configuration files, the Jackson YAML data format must be included:

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.dataformat</groupId>
  3. <artifactId>jackson-dataformat-yaml</artifactId>
  4. <version>2.8.5</version>
  5. </dependency>

Configuring loggers

An understanding of how loggers work in Log4j is critical before trying to configure them. Please reference the Log4j architecture if more information is required. Trying to configure Log4j without understanding those concepts will lead to frustration.在尝试配置日志记录器 前, 理解它们是如何工作的是非常重要的。如果需要更多的信息请参考Log4j architecture  。在没有理解这些概念前尝试配置Log4j将会导致失败。)

A LoggerConfig is configured using the logger element. The logger element must have a name attribute specified, will usually have a level attribute specified and may also have an additivity attribute specified. The level may be configured with one of TRACE, DEBUG, INFO, WARN, ERROR, ALL or OFF. If no level is specified it will default to ERROR. The additivity attribute may be assigned a value of true or false. If the attribute is omitted the default value of false will be used.(使用logger 元素来配置日志记录器。 logger  元素一定要指定一个name属性,通常还会指定level和additivity属性。level属性值可以为 TRACE, DEBUG, INFO, WARN, ERROR, ALL or OFF之一。如果没有指定level属性,则默认的为error。additivity属性值可以为true或false,它的默认值为false。

A LoggerConfig (including the root LoggerConfig) can be configured with properties that will be added to the properties copied from the ThreadContextMap. These properties can be referenced from Appenders, Filters, Layouts, etc just as if they were part of the ThreadContext Map. The properties can contain variables that will be resolved either when the configuration is parsed or dynamically when each event is logged. See Property Substitution for more information on using variables.(LoggerConfig(包括root LogerConfig)都可以使用从ThreadContextMap 拷贝过来的属性进行配置。这些属性可以被 Appenders, Filters, Layouts 等引用,就好像它们是ThreadContext Map 的一部分。这些属性中可以包含变量,它们会在分析配置文件的时候被解析或者当日志被记录的时候动态解析。如何使用变量,查看 Property Substitution

The LoggerConfig may also be configured with one or more AppenderRef elements. Each appender referenced will become associated with the specified LoggerConfig. If multiple appenders are configured on the LoggerConfig each of them be called when processing logging events.(LoggerConfig也可以使用一个或多个AppenderRef引用元素进行配置。如果在一个LoggerConfig中配置多个appenders,当处理日志事件的时候,每个appender都会被调用)

Every configuration must have a root logger. If one is not configured the default root LoggerConfig, which has a level of ERROR and has a Console appender attached, will be used. The main differences between the root logger and other loggers are(每一个配置都必须有一个root logger。如果没有配置,一个默认的root LoggerConfig就会被使用,它的级别为ERROR并且有一个Console的appender。root logger和其它的logger的主要区别为:)

  1. The root logger does not have a name attribute.(root logger没有name属性)
  2. The root logger does not support the additivity attribute since it has no parent.(root logger不会支持additivity属性,因为它没有父元素)

Configuring Appenders(配置Appender元素)

An appender is configured either using the specific appender plugin‘s name or with an appender element and the type attibute containing the appender plugin‘s name. In addition each appender must have a name attribute specified with a value that is unique within the set of appenders. The name will be used by loggers to reference the appender as described in the previous section.(appender要么是通过使用一个指定的appender插件的名称进行配置,要么是通过一个appender元素并且type属性的值为appender插件的名称进行配置。此外每一个appender一定要指定一个在appenders集合中独一无二的name属性。这个name属性会被logger的AppenderRef引用)

Most appenders also support a layout to be configured which again may be specified either using the specific Layout plugin‘s name as the element or with "layout" as the element name along with a type attribute that contains the layout plugin‘s name. The various appenders will contain other attributes or elements that are required for them to function properly.(大多数的appenders也支持配置layout,layout可以通过指定Layout插件的名称进行配置,也可以通过使用“layout”作为元素名并为其指定type属性(type属性的值为layout插件的名称)。各种各样的appender将包含其他属性或是他们的正常功能所需的元素 )

Configuring Filters(配置过滤器元素)

Log4j allows a filter to be specified in any of 4 places:(Log4j允许在4个位置配置过滤器元素)

  1. At the same level as the appenders, loggers and properties elements. These filters can accept or reject events before they have been passed to a LoggerConfig.(和appenders,loggers,properties元素在同一个级别。这些过滤器在日志事件传递给LoggerConfig之前对日志事件进行接受或者是拒绝)
  2. In a logger element. These filters can accept or reject events for specific loggers.(在一个logger元素中。为指定的logger接受和拒绝日志事件)
  3. In an appender element. These filters can prevent or cause events to be processed by the appender.(在一个appender中。阻止或者导致日志事件被appender处理)
  4. In an appender reference element. These filters are used to determine if a Logger should route the event to an appender.(在一个AppenderRef元素中。用来判断一个日志记录器是有应该将日志事件传递到appender中)

Although only a single filter element can be configured, that element may be the filters element which represents the CompositeFilter. The filters element allows any number of filter elements to be configured within it. The following example shows how multiple filters can be configured on the ConsoleAppender.(即使只有一个过滤器可以配置,这个元素可能是filters 元素,它表示过滤元素的复合。可以在filters  元素中配置任意个 filter 元素。下面的例子显示在ConsoleAppender中配置多个filter  )

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="debug"name="XMLConfigTest"packages="org.apache.logging.log4j.test">
  3. <Properties>
  4. <Propertyname="filename">target/test.log</Property>
  5. </Properties>
  6. <ThresholdFilterlevel="trace"/>
  7.  
  8. <Appenders>
  9. <Consolename="STDOUT">
  10. <PatternLayoutpattern="%m MDC%X%n"/>
  11. </Console>
  12. <Consolename="FLOW">
  13. <!-- this pattern outputs class name and line number -->
  14. <PatternLayoutpattern="%C{1}.%M %m %ex%n"/>
  15. <filters>
  16. <MarkerFiltermarker="FLOW"onMatch="ACCEPT"onMismatch="NEUTRAL"/>
  17. <MarkerFiltermarker="EXCEPTION"onMatch="ACCEPT"onMismatch="DENY"/>
  18. </filters>
  19. </Console>
  20. <Filename="File"fileName="${filename}">
  21. <PatternLayout>
  22. <pattern>%d %p %C{1.} [%t] %m%n</pattern>
  23. </PatternLayout>
  24. </File>
  25. <Listname="List">
  26. </List>
  27. </Appenders>
  28.  
  29. <Loggers>
  30. <Loggername="org.apache.logging.log4j.test1"level="debug"additivity="false">
  31. <ThreadContextMapFilter>
  32. <KeyValuePairkey="test"value="123"/>
  33. </ThreadContextMapFilter>
  34. <AppenderRefref="STDOUT"/>
  35. </Logger>
  36.  
  37. <Loggername="org.apache.logging.log4j.test2"level="debug"additivity="false">
  38. <Propertyname="user">${sys:user.name}</Property>
  39. <AppenderRefref="File">
  40. <ThreadContextMapFilter>
  41. <KeyValuePairkey="test"value="123"/>
  42. </ThreadContextMapFilter>
  43. </AppenderRef>
  44. <AppenderRefref="STDOUT"level="error"/>
  45. </Logger>
  46.  
  47. <Rootlevel="trace">
  48. <AppenderRefref="List"/>
  49. </Root>
  50. </Loggers>
  51.  
  52. </Configuration>

Configuration with Properties

As of version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1. Like the XML and JSON configurations, properties configurations define the configuration in terms of plugins and attributes to the plugins.

Prior to version 2.6, the properties configuration requires that you list the identifiers of the appenders, filters and loggers, in a comma separated list in properties with those names. Each of those components will then be expected to be defined in sets of properties that begin with component.<.identifier>.. The identifier does not have to match the name of the component being defined but must uniquely identify all the attributes and subcomponents that are part of the component. If the list of identifiers is not present the identier must not contain a ‘.‘. Each individual component MUST have a "type" attribute specified that identifies the component‘s Plugin type.

As of version 2.6, this list of identifiers is no longer required as names are inferred upon first usage, however if you wish to use more complex identifies you must still use the list. If the list is present it will be used.

Unlike the base components, when creating subcomponents you cannot specify an element containing a list of identifiers. Instead, you must define the wrapper element with its type as is shown in the policies definition in the rolling file appender below. You then define each of the subcomponents below that wrapper element, as the TimeBasedTriggeringPolicy and SizeBasedTriggeringPolicy are defined below.

Properties configuration files support the advertiser, monitorInterval, name, packages, shutdownHook, shutdownTimeout, status, verbose, and dest attrbutes. See Configuration Syntax for the definitions of these attributes.

  1. status = error
  2. dest = err
  3. name =PropertiesConfig
  4.  
  5. property.filename = target/rolling/rollingtest.log
  6.  
  7. filter.threshold.type =ThresholdFilter
  8. filter.threshold.level = debug
  9.  
  10. appender.console.type =Console
  11. appender.console.name = STDOUT
  12. appender.console.layout.type =PatternLayout
  13. appender.console.layout.pattern =%m%n
  14.  
  15. appender.rolling.type =RollingFile
  16. appender.rolling.name =RollingFile
  17. appender.rolling.fileName = ${filename}
  18. appender.rolling.filePattern = target/rolling2/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
  19. appender.rolling.layout.type =PatternLayout
  20. appender.rolling.layout.pattern =%d %p %C{1.}[%t]%m%n
  21. appender.rolling.policies.type =Policies
  22. appender.rolling.policies.time.type =TimeBasedTriggeringPolicy
  23. appender.rolling.policies.time.interval =2
  24. appender.rolling.policies.time.modulate =true
  25. appender.rolling.policies.size.type =SizeBasedTriggeringPolicy
  26. appender.rolling.policies.size.size=100MB
  27. appender.rolling.strategy.type =DefaultRolloverStrategy
  28. appender.rolling.strategy.max =5
  29.  
  30. appender.list.type =List
  31. appender.list.name =List
  32. appender.list.filter.threshold.type =ThresholdFilter
  33. appender.list.filter.threshold.level = error
  34.  
  35. logger.rolling.name = com.example.my.app
  36. logger.rolling.level = debug
  37. logger.rolling.additivity =false
  38. logger.rolling.appenderRef.rolling.ref=RollingFile
  39.  
  40. rootLogger.level = info
  41. rootLogger.appenderRef.stdout.ref= STDOUT

Property Substitution(属性替换)

Log4j 2 supports the ability to specify tokens in the configuration as references to properties defined elsewhere. Some of these properties will be resolved when the configuration file is interpreted while others may be passed to components where they will be evaluated at runtime. To accomplish this, Log4j uses variations of Apache Commons Lang‘s StrSubstitutor and StrLookup classes. In a manner similar to Ant or Maven, this allows variables declared as ${name} to be resolved using properties declared in the configuration itself. For example, the following example shows the filename for the rolling file appender being declared as a property.(Log4j 2支持指定token,这些token可以被其它地方的属性进行引用。当配置文件被解释时,一些属性将被解决,而另一些属性将传递给组件,这些组件将在运行时对它们进行评估。为了实现这一目标,log4j的使用 Apache Commons Lang‘s StrSubstitutor 和StrLookup类的变量。在某种程度上类似于Ant或Maven,这允许变量声明为 ${name}   ,它们会被解析成该配置文件中的name属性定义的值。例如,下面的示例显示rolling file appender的文件名被作为一个属性声明。 )

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="debug"name="RoutingTest"packages="org.apache.logging.log4j.test">
  3. <Properties>
  4. <Propertyname="filename">target/rolling1/rollingtest-$${sd:type}.log</Property>
  5. </Properties>
  6. <ThresholdFilterlevel="debug"/>
  7.  
  8. <Appenders>
  9. <Consolename="STDOUT">
  10. <PatternLayoutpattern="%m%n"/>
  11. </Console>
  12. <Listname="List">
  13. <ThresholdFilterlevel="debug"/>
  14. </List>
  15. <Routingname="Routing">
  16. <Routespattern="$${sd:type}">
  17. <Route>
  18. <RollingFilename="Rolling-${sd:type}"fileName="${filename}"
  19. filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
  20. <PatternLayout>
  21. <pattern>%d %p %c{1.} [%t] %m%n</pattern>
  22. </PatternLayout>
  23. <SizeBasedTriggeringPolicysize="500"/>
  24. </RollingFile>
  25. </Route>
  26. <Routeref="STDOUT"key="Audit"/>
  27. <Routeref="List"key="Service"/>
  28. </Routes>
  29. </Routing>
  30. </Appenders>
  31.  
  32. <Loggers>
  33. <Loggername="EventLogger"level="info"additivity="false">
  34. <AppenderRefref="Routing"/>
  35. </Logger>
  36.  
  37. <Rootlevel="error">
  38. <AppenderRefref="STDOUT"/>
  39. </Root>
  40. </Loggers>
  41.  
  42. </Configuration>

While this is useful, there are many more places properties can originate from. To accommodate this, Log4j also supports the syntax ${prefix:name} where the prefix identifies tells Log4j that variable name should be evaluated in a specific context. See the Lookups manual page for more details. The contexts that are built in to Logj4 are:(这是很有用的,但是引用的属性会来自于很多的地方。为了适应这种情况,log4j也支持语法 ${prefix:name} 在前缀标识告诉log4j,变量的名字应该在一个特定的上下文进行评估。看到Lookups 手册页来了解更多细节。建立在logj4的上下文包括:)

PrefixContext
bundle Resource bundle. The format is ${bundle:BundleName:BundleKey}. The bundle name follows package naming conventions, for example: ${bundle:com.domain.Messages:MyKey}.
ctx Thread Context Map (MDC)
date Inserts the current date and/or time using the specified format
env System environment variables
jndi A value set in the default JNDI Context.
jvmrunargs A JVM input argument accessed through JMX, but not a main argument; see RuntimeMXBean.getInputArguments(). Not available on Android.
log4j Log4j configuration properties. The expressions ${log4j:configLocation} and ${log4j:configParentLocation} respectively provide the absolute path to the log4j configuration file and its parent folder.
main A value set with MapLookup.setMainArguments(String[])
map A value from a MapMessage
sd A value from a StructuredDataMessage. The key "id" will return the name of the StructuredDataId without the enterprise number. The key "type" will return the message type. Other keys will retrieve individual elements from the Map.
sys System properties

A default property map can be declared in the configuration file. If the value cannot be located in the specified lookup the value in the default property map will be used. The default map is pre-populated with a value for "hostName" that is the current system‘s host name or IP address and the "contextName" with is the value of the current logging context.(默认属性映射可以在配置文件中声明。如果该值不能位于指定的查找中,则将使用默认属性映射中的值。默认映射预填充的值为“主机名”是当前系统的主机名或IP地址和“contextname”是当前测试上下文的值。 )

Lookup Variables with Multiple Leading ‘$‘ Characters(查找多个前导"$"字符的变量)

An interesting feature of StrLookup processing is that when a variable reference is declared with multiple leading ‘$‘ characters each time the variable is resolved the leading ‘$‘ is simply removed. In the previous example the "Routes" element is capable of resolving the variable at runtime. To allow this the prefix value is specified as a variable with two leading ‘$‘ characters. When the configuration file is first processed the first variable is simply removed. Thus, when the Routes element is evaluated at runtime it is the variable declaration "${sd:type}" which causes the event to be inspected for a StructuredDataMessage and if one is present the value of its type attribute to be used as the routing key. Not all elements support resolving variables at runtime. Components that do will specifically call that out in their documentation.StrLookup 处理的一个有趣的特点是,当一个变量引用声明了多个前导‘$‘字符,在解析时只是简单的将‘$‘删除。在前面的示例中,“Routes”元素能够在运行时解决变量。为了允许这个,前缀值被指定为带有两个前导$字符的变量。当配置文件被第一次处理时,第一个变量会被删除。因此,当线路元件在运行时计算它的变量声明 "${sd:type}" ,这会引起事件来检查一个StructuredDataMessage 和如果类型属性的值存在则作为 routing key。不是所有元素都支持在运行时解析变量。组件将在其文档中明确调用该文件。 )

If no value is found for the key in the Lookup associated with the prefix then the value associated with the key in the properties declaration in the configuration file will be used. If no value is found the variable declaration will be returned as the value. Default values may be declared in the configuration by doing:(如果与前缀相关联的查找中的键没有找到值,则将使用配置文件中的属性声明中的键关联的值。如果没有找到值,变量声明将被作为值返回。默认值可以在配置中声明 ,如下:)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configuration>
  3. <Properties>
  4. <Propertyname="type">Audit</property>
  5. </Properties>
  6. ...
  7. </Configuration>

As a footnote, it is worth pointing out that the variables in the RollingFile appender declaration will also not be evaluated when the configuration is processed. This is simply because the resolution of the whole RollingFile element is deferred until a match occurs. See RoutingAppender for more information.(作为一个注脚,值得指出的是,当配置文件被处理的时候,在rollingfile appender声明中的变量也不会被评估时。这是因为整个rollingfile元素的解析被推迟到匹配发生的时候。更多信息见RoutingAppender。 )

Scripts(脚本)

Log4j provides support for JSR 223 scripting languages to be used in some of its components. Any language that provides support for the JSR 223 scripting engine may be used. A list of the languages and bindings for them can be found at the Scripting Engine web site. However, some of the languages listed there, such as JavaScript, Groovy and Beanshell, directly support the JSR 223 scripting framework and only require that the jars for that language be installed.

The components that support using scripts do so by allowing a <script>, <scriptFile>, or <scriptRef> element to be configured on them. The script element contains a name for the script, the language of the script, and the script text. The scriptFile element contains the name of the script, its location, its language, its charset, and whether the file should be watched for changes. The scriptRef element contains the name of the script that is defined in the <scripts> configuration element. The name of the script is used to store the script, along with its ScriptEngine, so it can quickly be located each time the script needs to be run. While the name is not required, providing it will help in debugging problems when the script is running. The language must be provided on the script element and must specify one of the language names that appear in the Configuration status log as described in the next section. If the language is not specified on the scriptFile element the language will be determined by the file extension of the script path. If file monitoring is requested it will only be enabled if a non-zero monitorInterval is specified on the configuration element. That interval will be used to check for changes in the file.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Configurationstatus="debug"name="RoutingTest">
  3. <Scripts>
  4. <Scriptname="selector"language="javascript"><![CDATA[
  5. var result;
  6. if(logEvent.getLoggerName().equals("JavascriptNoLocation")){
  7. result ="NoLocation";
  8. }elseif(logEvent.getMarker()!=null&& logEvent.getMarker().isInstanceOf("FLOW")){
  9. result ="Flow";
  10. }
  11. result;
  12. ]]></Script>
  13. <ScriptFilename="groovy.filter"path="scripts/filter.groovy"/>
  14. </Scripts>
  15.  
  16. <Appenders>
  17. <Consolename="STDOUT">
  18. <ScriptPatternSelectordefaultPattern="%d %p %m%n">
  19. <ScriptRefref="selector"/>
  20. <PatternMatchkey="NoLocation"pattern="[%-5level] %c{1.} %msg%n"/>
  21. <PatternMatchkey="Flow"pattern="[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"/>
  22. </ScriptPatternSelector>
  23. <PatternLayoutpattern="%m%n"/>
  24. </Console>
  25. </Appenders>
  26.  
  27. <Loggers>
  28. <Loggername="EventLogger"level="info"additivity="false">
  29. <ScriptFilteronMatch="ACCEPT"onMisMatch="DENY">
  30. <Scriptname="GroovyFilter"language="groovy"><![CDATA[
  31. if(logEvent.getMarker()!=null&& logEvent.getMarker().isInstanceOf("FLOW")){
  32. returntrue;
  33. }elseif(logEvent.getContextMap().containsKey("UserId")){
  34. returntrue;
  35. }
  36. returnfalse;
  37. ]]>
  38. </Script>
  39. </ScriptFilter>
  40. <AppenderRefref="STDOUT"/>
  41. </Logger>
  42.  
  43. <Rootlevel="error">
  44. <ScriptFilteronMatch="ACCEPT"onMisMatch="DENY">
  45. <ScriptRefref="groovy.filter"/>
  46. </ScriptFilter>
  47. <AppenderRefref="STDOUT"/>
  48. </Root>
  49. </Loggers>
  50.  
  51. </Configuration>

If the status attribute on the Configuration element is set to DEBUG the list of script engines currently installed and their attributes will be listed. Although some engines may say they are not thread safe, Log4j takes steps to insure that the scripts will run in a thread-safe manner if the engine advertises that it is not thread safe.

2015-09-27 16:13:22,925 main DEBUG Installed script engines
2015-09-27 16:13:22,963 main DEBUG AppleScriptEngine Version: 1.1, Language: AppleScript, Threading: Not Thread Safe,
            Compile: false, Names: {AppleScriptEngine, AppleScript, OSA}
2015-09-27 16:13:22,983 main DEBUG Groovy Scripting Engine Version: 2.0, Language: Groovy, Threading: MULTITHREADED,
            Compile: true, Names: {groovy, Groovy}
2015-09-27 16:13:23,030 main DEBUG BeanShell Engine Version: 1.0, Language: BeanShell, Threading: MULTITHREADED,
            Compile: true, Names: {beanshell, bsh, java}
2015-09-27 16:13:23,039 main DEBUG Mozilla Rhino Version: 1.7 release 3 PRERELEASE, Language: ECMAScript, Threading: MULTITHREADED,
            Compile: true, Names: {js, rhino, JavaScript, javascript, ECMAScript, ecmascript}
          

When the scripts are executed they will be provided with a set of variables that should allow them to accomplish whatever task they are expected to perform. See the documentation for the individual components for the list of variables that are available to the script.

The components that support scripting expect a return value to be passed back to the calling Java code. This is not a problem for several of the scripting languages, but Javascript does not allow a return statement unless it is within a function. However, Javascript will return the value of the last statement executed in the script. As a consequence, code such as that shown below will result in the desired behavior.

            var result;
            if (logEvent.getLoggerName().equals("JavascriptNoLocation")) {
                result = "NoLocation";
            } else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf("FLOW")) {
                result = "Flow";
            }
            result;
          

A special note on Beanshell

JSR 223 scripting engines are supposed to identify that they support the Compilable interface if they support compiling their scripts. Beanshell does this. However, whenever the compile method is called it throws an Error (not an Exception). Log4j catches this but will log the warning shown below for each Beanshell script when it tries to compile them. All Beanshell scripts will then be interpreted on each execution.

2015-09-27 16:13:23,095 main DEBUG Script BeanShellSelector is compilable
2015-09-27 16:13:23,096 main WARN Error compiling script java.lang.Error: unimplemented
            at bsh.engine.BshScriptEngine.compile(BshScriptEngine.java:175)
            at bsh.engine.BshScriptEngine.compile(BshScriptEngine.java:154)
            at org.apache.logging.log4j.core.script.ScriptManager$MainScriptRunner.<init>(ScriptManager.java:125)
            at org.apache.logging.log4j.core.script.ScriptManager.addScript(ScriptManager.java:94)
          

XInclude

XML configuration files can include other files with XInclude. Here is an example log4j2.xml file that includes two other files:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configurationxmlns:xi="http://www.w3.org/2001/XInclude"
  3. status="warn"name="XIncludeDemo">
  4. <properties>
  5. <propertyname="filename">xinclude-demo.log</property>
  6. </properties>
  7. <ThresholdFilterlevel="debug"/>
  8. <xi:includehref="log4j-xinclude-appenders.xml"/>
  9. <xi:includehref="log4j-xinclude-loggers.xml"/>
  10. </configuration>

log4j-xinclude-appenders.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <appenders>
  3. <Consolename="STDOUT">
  4. <PatternLayoutpattern="%m%n"/>
  5. </Console>
  6. <Filename="File"fileName="${filename}"bufferedIO="true"immediateFlush="true">
  7. <PatternLayout>
  8. <pattern>%d %p %C{1.} [%t] %m%n</pattern>
  9. </PatternLayout>
  10. </File>
  11. </appenders>

log4j-xinclude-loggers.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <loggers>
  3. <loggername="org.apache.logging.log4j.test1"level="debug"additivity="false">
  4. <ThreadContextMapFilter>
  5. <KeyValuePairkey="test"value="123"/>
  6. </ThreadContextMapFilter>
  7. <AppenderRefref="STDOUT"/>
  8. </logger>
  9.  
  10. <loggername="org.apache.logging.log4j.test2"level="debug"additivity="false">
  11. <AppenderRefref="File"/>
  12. </logger>
  13.  
  14. <rootlevel="error">
  15. <AppenderRefref="STDOUT"/>
  16. </root>
  17. </loggers>

Composite Configuration

Log4j allows multiple configuration files to be used by specifying them as a list of comma separated file paths on log4j.configurationFile. The merge logic can be controlled by specifying a class that implements the MergeStrategy interface on the log4j.mergeStrategy property. The default merge strategy will merge the files using the following rules:

  1. The global configuration attributes are aggregated with those in later configurations replacing those in previous configurations, with the exception that the highest status level and the lowest monitorInterval greater than 0 will be used.
  2. Properties from all configurations are aggregated. Duplicate properties replace those in previous configurations.
  3. Filters are aggregated under a CompositeFilter if more than one Filter is defined. Since Filters are not named duplicates may be present.
  4. Scripts and ScriptFile references are aggregated. Duplicate definiations replace those in previous configurations.
  5. Appenders are aggregated. Appenders with the same name are replaced by those in later configurations, including all of the Appender‘s subcomponents.
  6. Loggers are all aggregated. Logger attributes are individually merged with duplicates being replaced by those in later configurations. Appender references on a Logger are aggregated with duplicates being replaced by those in later configurations. Filters on a Logger are aggregated under a CompositeFilter if more than one Filter is defined. Since Filters are not named duplicates may be present. Filters under Appender references included or discarded depending on whether their parent Appender reference is kept or discarded.

Status Messages

Troubleshooting tip for the impatient:
  • Before a configuration is found, status logger level can be controlled with system property org.apache.logging.log4j.simplelog.StatusLogger.level.
  • After a configuration is found, status logger level can be controlled in the configuration file with the "status" attribute, for example: <Configuration status="trace">.

Just as it is desirable to be able to diagnose problems in applications, it is frequently necessary to be able to diagnose problems in the logging configuration or in the configured components. Since logging has not been configured, "normal" logging cannot be used during initialization. In addition, normal logging within appenders could create infinite recursion which Log4j will detect and cause the recursive events to be ignored. To accomodate this need, the Log4j 2 API includes a StatusLogger. Components declare an instance of the StatusLogger similar to:

protectedfinalstaticLogger logger =StatusLogger.getLogger();

Since StatusLogger implements the Log4j 2 API‘s Logger interface, all the normal Logger methods may be used.

When configuring Log4j it is sometimes necessary to view the generated status events. This can be accomplished by adding the status attribute to the configuration element or a default value can be provided by setting the "Log4jDefaultStatusLevel" system property. Valid values of the status attribute are "trace", "debug", "info", "warn", "error" and "fatal". The following configuration has the status attribute set to debug.

  1. <?xml version="1.0" encoding="UTF-8"?>;
  2. <Configurationstatus="debug"name="RoutingTest">
  3. <Properties>
  4. <Propertyname="filename">target/rolling1/rollingtest-$${sd:type}.log</Property>
  5. </Properties>
  6. <ThresholdFilterlevel="debug"/>
  7.  
  8. <Appenders>
  9. <Consolename="STDOUT">
  10. <PatternLayoutpattern="%m%n"/>
  11. </Console>
  12. <Listname="List">
  13. <ThresholdFilterlevel="debug"/>
  14. </List>
  15. <Routingname="Routing">
  16. <Routespattern="$${sd:type}">
  17. <Route>
  18. <RollingFilename="Rolling-${sd:type}"fileName="${filename}"
  19. filePattern="target/rolling1/test1-${sd:type}.%i.log.gz">
  20. <PatternLayout>
  21. <pattern>%d %p %c{1.} [%t] %m%n</pattern>
  22. </PatternLayout>
  23. <SizeBasedTriggeringPolicysize="500"/>
  24. </RollingFile>
  25. </Route>
  26. <Routeref="STDOUT"key="Audit"/>
  27. <Routeref="List"key="Service"/>
  28. </Routes>
  29. </Routing>
  30. </Appenders>
  31.  
  32. <Loggers>
  33. <Loggername="EventLogger"level="info"additivity="false">
  34. <AppenderRefref="Routing"/>
  35. </Logger>
  36.  
  37. <Rootlevel="error">
  38. <AppenderRefref="STDOUT"/>
  39. </Root>
  40. </Loggers>
  41.  
  42. </Configuration>

During startup this configuration produces:

2011-11-23 17:08:00,769 DEBUG Generated plugins in 0.003374000 seconds
2011-11-23 17:08:00,789 DEBUG Calling createProperty on class org.apache.logging.log4j.core.config.Property for element property with params(name="filename", value="target/rolling1/rollingtest-${sd:type}.log")
2011-11-23 17:08:00,792 DEBUG Calling configureSubstitutor on class org.apache.logging.log4j.core.config.PropertiesPlugin for element properties with params(properties={filename=target/rolling1/rollingtest-${sd:type}.log})
2011-11-23 17:08:00,794 DEBUG Generated plugins in 0.001362000 seconds
2011-11-23 17:08:00,797 DEBUG Calling createFilter on class org.apache.logging.log4j.core.filter.ThresholdFilter for element ThresholdFilter with params(level="debug", onMatch="null", onMismatch="null")
2011-11-23 17:08:00,800 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%m%n", Configuration(RoutingTest), null, charset="null")
2011-11-23 17:08:00,802 DEBUG Generated plugins in 0.001349000 seconds
2011-11-23 17:08:00,804 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console with params(PatternLayout(%m%n), null, target="null", name="STDOUT", ignoreExceptions="null")
2011-11-23 17:08:00,804 DEBUG Calling createFilter on class org.apache.logging.log4j.core.filter.ThresholdFilter for element ThresholdFilter with params(level="debug", onMatch="null", onMismatch="null")
2011-11-23 17:08:00,806 DEBUG Calling createAppender on class org.apache.logging.log4j.test.appender.ListAppender for element List with params(name="List", entryPerNewLine="null", raw="null", null, ThresholdFilter(DEBUG))
2011-11-23 17:08:00,813 DEBUG Calling createRoute on class org.apache.logging.log4j.core.appender.routing.Route for element Route with params(AppenderRef="null", key="null", Node=Route)
2011-11-23 17:08:00,823 DEBUG Calling createRoute on class org.apache.logging.log4j.core.appender.routing.Route for element Route with params(AppenderRef="STDOUT", key="Audit", Node=Route)
2011-11-23 17:08:00,824 DEBUG Calling createRoute on class org.apache.logging.log4j.core.appender.routing.Route for element Route with params(AppenderRef="List", key="Service", Node=Route)
2011-11-23 17:08:00,825 DEBUG Calling createRoutes on class org.apache.logging.log4j.core.appender.routing.Routes for element Routes with params(pattern="${sd:type}", routes={Route(type=dynamic default), Route(type=static Reference=STDOUT key=‘Audit‘), Route(type=static Reference=List key=‘Service‘)})
2011-11-23 17:08:00,827 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.routing.RoutingAppender for element Routing with params(name="Routing", ignoreExceptions="null", Routes({Route(type=dynamic default),Route(type=static Reference=STDOUT key=‘Audit‘),Route(type=static Reference=List key=‘Service‘)}), Configuration(RoutingTest), null, null)
2011-11-23 17:08:00,827 DEBUG Calling createAppenders on class org.apache.logging.log4j.core.config.AppendersPlugin for element appenders with params(appenders={STDOUT, List, Routing})
2011-11-23 17:08:00,828 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.plugins.AppenderRefPlugin for element AppenderRef with params(ref="Routing")
2011-11-23 17:08:00,829 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig for element logger with params(additivity="false", level="info", name="EventLogger", AppenderRef={Routing}, null)
2011-11-23 17:08:00,830 DEBUG Calling createAppenderRef on class org.apache.logging.log4j.core.config.plugins.AppenderRefPlugin for element AppenderRef with params(ref="STDOUT")
2011-11-23 17:08:00,831 DEBUG Calling createLogger on class org.apache.logging.log4j.core.config.LoggerConfig$RootLogger for element root with params(additivity="null", level="error", AppenderRef={STDOUT}, null)
2011-11-23 17:08:00,833 DEBUG Calling createLoggers on class org.apache.logging.log4j.core.config.LoggersPlugin for element loggers with params(loggers={EventLogger, root})
2011-11-23 17:08:00,834 DEBUG Reconfiguration completed
2011-11-23 17:08:00,846 DEBUG Calling createLayout on class org.apache.logging.log4j.core.layout.PatternLayout for element PatternLayout with params(pattern="%d %p %c{1.} [%t] %m%n", Configuration(RoutingTest), null, charset="null")
2011-11-23 17:08:00,849 DEBUG Calling createPolicy on class org.apache.logging.log4j.core.appender.rolling.SizeBasedTriggeringPolicy for element SizeBasedTriggeringPolicy with params(size="500")
2011-11-23 17:08:00,851 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile with params(fileName="target/rolling1/rollingtest-Unknown.log", filePattern="target/rolling1/test1-Unknown.%i.log.gz", append="null", name="Rolling-Unknown", bufferedIO="null", immediateFlush="null", SizeBasedTriggeringPolicy(SizeBasedTriggeringPolicy(size=500)), null, PatternLayout(%d %p %c{1.} [%t] %m%n), null, ignoreExceptions="null")
2011-11-23 17:08:00,858 DEBUG Generated plugins in 0.002014000 seconds
2011-11-23 17:08:00,889 DEBUG Reconfiguration started for context sun.misc.Launcher$AppClassLoader@37b90b39
2011-11-23 17:08:00,890 DEBUG Generated plugins in 0.001355000 seconds
2011-11-23 17:08:00,959 DEBUG Generated plugins in 0.001239000 seconds
2011-11-23 17:08:00,961 DEBUG Generated plugins in 0.001197000 seconds
2011-11-23 17:08:00,965 WARN No Loggers were configured, using default
2011-11-23 17:08:00,976 DEBUG Reconfiguration completed

If the status attribute is set to error than only error messages will be written to the console. This makes troubleshooting configuration errors possible. As an example, if the configuration above is changed to have the status set to error and the logger declaration is:

  1. <loggername="EventLogger"level="info"additivity="false">
  2. <AppenderRefref="Routng"/>
  3. </logger>

the following error message will be produced.

2011-11-24 23:21:25,517 ERROR Unable to locate appender Routng for logger EventLogger

Applications may wish to direct the status output to some other destination. This can be accomplished by setting the dest attribute to either "err" to send the output to stderr or to a file location or URL. This can also be done by insuring the configured status is set to OFF and then configuring the application programmatically such as:

  1. StatusConsoleListener listener =newStatusConsoleListener(Level.ERROR);
  2. StatusLogger.getLogger().registerListener(listener);

Testing in Maven

Maven can run unit and functional tests during the build cycle. By default, any files placed in src/test/resources are automatically copied to target/test-classes and are included in the classpath during execution of any tests. As such, placing a log4j2-test.xml into this directory will cause it to be used instead of a log4j2.xml or log4j2.json that might be present. Thus a different log configuration can be used during testing than what is used in production.

A second approach, which is extensively used by Log4j 2, is to set the log4j.configurationFile property in the method annotated with @BeforeClass in the junit test class. This will allow an arbitrarily named file to be used during the test.

A third approach, also used extensively by Log4j 2, is to use the LoggerContextRule JUnit test rule which provides additional convenience methods for testing. This requires adding the log4j-core test-jar dependency to your test scope dependencies. For example:

  1. publicclassAwesomeTest{
  2. @Rule
  3. publicLoggerContextRule init =newLoggerContextRule("MyTestConfig.xml");
  4.  
  5. @Test
  6. publicvoid testSomeAwesomeFeature(){
  7. finalLoggerContext ctx = init.getContext();
  8. finalLogger logger = init.getLogger("org.apache.logging.log4j.my.awesome.test.logger");
  9. finalConfiguration cfg = init.getConfiguration();
  10. finalListAppender app = init.getListAppender("List");
  11. logger.warn("Test message");
  12. finalList<LogEvent> events = app.getEvents();
  13. // etc.
  14. }
  15. }

System Properties

The Log4j documentation references a number of System Properties that can be used to control various aspects of Log4j 2 behavior. The table below lists these properties along with their default value and a description of what they control. Any spaces present in the property name are for visual flow and should be removed.

The properties listed below can also be specified by creating a file named log4j2.component.properties, adding the desired keys and value to the file, and then including the file in the classpath of the application.

Log4j 2 System Properties
System PropertyDefault ValueDescription
log4j.configurationFile   Path to an XML or JSON Log4j 2 configuration file. May also contain a comma separated list of configuration file names.
log4j.mergeFactory   The name of the class that implements the MergeStrategy interface. If not specified DefaultMergeStrategy will be used when creating a CompositeConfiguration..
Log4jContextSelector ClassLoaderContextSelector Creates the LoggerContexts. An application can have one or more active LoggerContexts depending on the circumstances. See Log Separation for more details. Available context selector implementation classes:
org.apache.logging.log4j.core.async .AsyncLoggerContextSelector - makes all loggers asynchronous.
org.apache.logging.log4j.core.selector .BasicContextSelector - creates a single shared LoggerContext.
org.apache.logging.log4j.core.selector .ClassLoaderContextSelector - separate LoggerContexts for each web application.
org.apache.logging.log4j.core.selector .JndiContextSelector - use JNDI to locate each web application‘s LoggerContext.
org.apache.logging.log4j.core.osgi .BundleContextSelector - separate LoggerContexts for each OSGi bundle.
Log4jLogEventFactory org.apache.logging.log4j.core.impl .DefaultLogEventFactory Factory class used by LoggerConfig to create LogEvent instances. (Ignored when the AsyncLoggerContextSelector is used.)
log4j2.loggerContextFactory org.apache.logging.log4j.simple .SimpleLoggerContextFactory Factory class used by LogManager to bootstrap the logging implementation. The core jar provides org.apache.logging.log4j.core.impl.Log4jContextFactory.
log4j.configurationFactory   Fully specified class name of a class extending org.apache.logging.log4j.core.config.ConfigurationFactory. If specified, an instance of this class is added to the list of configuration factories.
log4j.shutdownHookEnabled true Overrides the global flag for whether or not a shutdown hook should be used to stop a LoggerContext. By default, this is enabled and can be disabled on a per-configuration basis. When running with the log4j-web module, this is automatically disabled.
log4j.shutdownCallbackRegistry org.apache.logging.log4j.core.util .DefaultShutdownCallbackRegistry Fully specified class name of a class implementing ShutdownCallbackRegistry. If specified, an instance of this class is used instead of DefaultShutdownCallbackRegistry. The specified class must have a default constructor.
log4j.Clock SystemClock Implementation of the org.apache.logging.log4j.core.util.Clock interface that is used for timestamping the log events.
By default, System.currentTimeMillis is called on every log event.
You can also specify a fully qualified class name of a custom class that implements the Clock interface.
org.apache.logging.log4j.level ERROR Log level of the default configuration. The default configuration is used if the ConfigurationFactory could not successfully create a configuration (e.g. no log4j2.xml file was found).
disableThreadContext false If true, the ThreadContext stack and map are disabled. (May be ignored if a custom ThreadContext map is specified.)
disableThreadContextStack false If true, the ThreadContext stack is disabled.
disableThreadContextMap false If true, the ThreadContext map is disabled. (May be ignored if a custom ThreadContext map is specified.)
log4j2.threadContextMap   Fully specified class name of a custom ThreadContextMap implementation class.
isThreadContextMapInheritable false If true use a InheritableThreadLocal to implement the ThreadContext map. Otherwise, use a plain ThreadLocal. (May be ignored if a custom ThreadContext map is specified.)
log4j2.ContextDataInjector   Fully specified class name of a custom ContextDataInjector implementation class.
log4j2.garbagefree.threadContextMap false Specify "true" to make the ThreadContext map garbage-free.
log4j2.disable.jmx false If true, Log4j configuration objects like LoggerContexts, Appenders, Loggers, etc. will not be instrumented with MBeans and cannot be remotely monitored and managed.
log4j2.jmx.notify.async false for web apps, true otherwise If true, log4j‘s JMX notifications are sent from a separate background thread, otherwise they are sent from the caller thread. If system property log4j2.is.webapp is true or the javax.servlet.Servlet class is on the classpath, the default behaviour is to use the caller thread to send JMX notifications.
log4j.skipJansi false If true, the ConsoleAppender will not try to use the Jansi output stream on Windows.
log4j.ignoreTCL false If true, classes are only loaded with the default class loader. Otherwise, an attempt is made to load classes with the current thread‘s context class loader before falling back to the default class loader.
org.apache.logging.log4j.uuidSequence 0 System property that may be used to seed the UUID generation with an integer value.
org.apache.logging.log4j.simplelog .showContextMap false If true, the full ThreadContext map is included in each SimpleLogger log message.
org.apache.logging.log4j.simplelog .showlogname false If true, the logger name is included in each SimpleLogger log message.
org.apache.logging.log4j.simplelog .showShortLogname true If true, only the last component of a logger name is included in SimpleLogger log messages. (E.g., if the logger name is "mycompany.myproject.mycomponent", only "mycomponent" is logged.
org.apache.logging.log4j.simplelog .showdatetime false If true, SimpleLogger log messages contain timestamp information.
org.apache.logging.log4j.simplelog .dateTimeFormat "yyyy/MM/dd HH:mm:ss:SSS zzz" Date-time format to use. Ignored if org.apache.logging.log4j.simplelog.showdatetime is false.
org.apache.logging.logj.simplelog .logFile system.err "system.err" (case-insensitive) logs to System.err, "system.out" (case-insensitive) logs to System.out, any other value is interpreted as a file name to save SimpleLogger messages to.
org.apache.logging.log4j.simplelog .level ERROR Default level for new SimpleLogger instances.
org.apache.logging.log4j.simplelog.<loggerName>level SimpleLogger default log level Log level for a the SimpleLogger instance with the specified name.
org.apache.logging.log4j.simplelog .StatusLogger.level ERROR This property is used to control the initial StatusLogger level, and can be overridden in code by calling StatusLogger.getLogger().setLevel(someLevel). Note that the StatusLogger level is only used to determine the status log output level until a listener is registered. In practice, a listener is registered when a configuration is found, and from that point onwards, status messages are only sent to the listeners (depending on their statusLevel).
Log4jDefaultStatusLevel ERROR

The StatusLogger logs events that occur in the logging system to the console. During configuration, AbstractConfiguration registers a StatusConsoleListener with the StatusLogger that may redirect status log events from the default console output to a file. The listener also supports fine-grained filtering. This system property specifies the default status log level for the listener to use if the configuration does not specify a status level.

Note: this property is used by the log4j-core implementation only after a configuration file has been found.

log4j2.StatusLogger.level WARN

The initial "listenersLevel" of the StatusLogger. If StatusLogger listeners are added, the "listenerLevel" is changed to that of the most verbose listener. If any listeners are registered, the listenerLevel is used to quickly determine if an interested listener exists.

By default, StatusLogger listeners are added when a configuration is found and by the JMX StatusLoggerAdmin MBean. For example, if a configuration contains <Configuration status="trace">, a listener with statusLevel TRACE is registered and the StatusLogger listenerLevel is set to TRACE, resulting in verbose status messages displayed on the console.

If no listeners are registered, the listenersLevel is not used, and the StatusLogger output level is determined by StatusLogger.getLogger().getLevel() (see property org.apache.logging.log4j.simplelog .StatusLogger.level).

log4j2.status.entries 200 Number of StatusLogger events that are kept in a buffer and can be retrieved with StatusLogger.getStatusData().
AsyncLogger.ExceptionHandler default handler See Async Logger System Properties for details.
AsyncLogger.RingBufferSize 256 * 1024 See Async Logger System Properties for details.
AsyncLogger.WaitStrategy Timeout See Async Logger System Properties for details.
AsyncLogger.ThreadNameStrategy CACHED See Async Logger System Properties for details.
AsyncLoggerConfig.ExceptionHandler default handler See Mixed Async/Synchronous Logger System Properties for details.
AsyncLoggerConfig.RingBufferSize 256 * 1024 See Mixed Async/Synchronous Logger System Properties for details.
AsyncLoggerConfig.WaitStrategy Timeout See Mixed Async/Synchronous Logger System Properties for details.
log4j.jul.LoggerAdapter org.apache.logging.log4j.jul .ApiLoggerAdapter Default LoggerAdapter to use in the JUL adapter. By default, if log4j-core is available, then the class org.apache.logging.log4j.jul .CoreLoggerAdapter will be used. Otherwise, the ApiLogggerAdapter will be used. Custom implementations must provide a public default constructor.
log4j.format.msg.async false If false (the default), Log4j will make sure the message is formatted in the caller thread, to ensure the value at the time of the call to the logger is the value that is logged.
log4j2.AsyncQueueFullPolicy  

Used by Async Loggers and the AsyncAppender to maintain application throughput even when the underlying appender cannot keep up with the logging rate and the queue is filling up.

If no value is specified (the default) events are never discarded. If the queue is full, the logger call blocks until the event can be added to the queue.

Specify Discard to drop events whose level is equal or less than the threshold level (INFO by default) when the queue is full.

log4j2.DiscardThreshold INFO Used by the DiscardingAsyncQueueFullPolicy to determine which events to drop when the queue becomes full. By default, INFO, DEBUG and TRACE level events are discarded when the queue is full. This property only has effect if Discard is specified as the log4j2.AsyncQueueFullPolicy.
log4j2.messageFactory org.apache.logging.log4j.message. ParameterizedMessageFactory or org.apache.logging.log4j.message. ReusableMessageFactory in garbage-free mode Default message factory used by Loggers if no factory was specified.
log4j2.flowMessageFactory org.apache.logging.log4j.message. DefaultFlowMessageFactory Default flow message factory used by Loggers.
log4j2.is.webapp true if Servlet class on class path This system property can be used to force Log4j 2 to behave as if it is part of a web application (when true) or as if it is not part of a web application (when false).
log4j2.enable.threadlocals true This system property can be used to switch off the use of threadlocals, which will partly disable Log4j‘s garbage-free behaviour: to be fully garbage-free, Log4j stores objects in ThreadLocal fields to reuse them, otherwise new objects are created for each log event. Note that this property is not effective when Log4j detects it is running in a web application.
log4j2.enable.direct.encoders true This property can be used to force garbage-aware Layouts and Appenders to revert to the pre-2.6 behaviour where converting log events to text generates temporary objects like Strings and char[] arrays, and converting this text to bytes generates temporary byte[] arrays. By default, this property is true and garbage-aware Layouts and Appenders that convert log events to text will convert this text to bytes without creating temporary objects.
log4j.initialReusableMsgSize 128 In GC-free mode, this property determines the initial size of the reusable StringBuilders where the message text is formatted and potentially passed to background threads.
log4j.maxReusableMsgSize 518 In GC-free mode, this property determines the maximum size of the reusable StringBuilders where the message text is formatted and potentially passed to background threads.
log4j.layoutStringBuilder.maxSize 2048 This property determines the maximum size of the thread-local reusable StringBuilders used to format the log event to text by Layouts that extend AbstractStringLayout.
log4j.unbox.ringbuffer.size 32 The org.apache.logging.log4j.util.Unbox utility manages a small thread-local ring buffer of StringBuilders. Each time one of the box() methods is called, the next slot in the ring buffer is used, until the ring buffer is full and the first slot is reused. By default the Unbox ring buffer has 32 slots, so user code can have up to 32 boxed primitives in a single logger call.

If more slots are required, set system property log4j.unbox.ringbuffer.size to the desired ring buffer size. Note that the specified number will be rounded up to the nearest power of 2.

log4j.LoggerContext.stacktrace.on.start false Prints a stacktrace to the status logger at DEBUG level when the LoggerContext is started. For debug purposes.





Log4j – Configuring Log4j 2 - Apache Log4j 2

标签:比较   mos   ctc   tde   dex   gen   计划   isp   日志信息   

原文地址:http://www.cnblogs.com/jimboi/p/6392323.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!