如何部署和搭建测试log4j 2
Apache Log4j 2 is an upgrade to Log4j that provides significant improvements over its predecessor, Log4j 1.x, and provides many of the improvements available in Logback while fixing some inherent problems in Logbacks architecture.Apache Log4j 2是一个Log4j的升级,提供了比他的前身Log4j 1.x更重大的改进,并且它提供了在Logback中可用的许多改进,同时,修复了很多在Logback的结构中的一些内部问题。Log4j 2 APIOverviewThe Log4j 2 API provides the interface that applications should code to and provides the adapter components required for implementers to create a logging implemenTAtion. Although Log4j 2 is broken up between an API and an implementation, the primary purpose of doing so was not to allow multiple implementations, although that is certainly possible, but to clearly define what classes and methods are safe to use in "normal" application code.Log4j 2 API提供应用程序可以编写代码的接口并且为实施者提供了创建一个日志实现所需的适配器组件。尽管log4j 2是打破了一个API和一个实现这种关系,这样做的主要目的是不允许多个接口的实现,尽管这当然是可能的,但要清楚的定义类和方法被用在正常的应用程序代码中是安全的。Hello World!No introduction would be complete without the customary Hello, World example. Here is ours. First, a Logger with the name "HelloWorld" is obtained from theLogManager. Next, the logger is used to write the "Hello, World!" message, however the message will be written only if the Logger is configured to allow informational messages.没有通俗易懂的hello world例子就不能称为一个完整的介绍。这是我们的(没明白这里什么意思)。首先,一个名为“Hello world”的Logger是从LogManager来获得的。其次,Logger被用来写“hello,world”消息,然而这个消息仅仅是在Logger配置了允许消息记录信息的时候才能打印出来。[java] view plain copy print?import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class HelloWorld {private static final Logger logger = LogManager.getLogger("HelloWorld");public static void main(String[] args) {logger.info("Hello, World!");} } 这里我是在一个空的工程中搭建的该类,首先的问题就是LogManager和Logger这两个类没有,下载了官网上的zip文件,里面有很多的jar包,尝试了一下,log4j-api-2.0.2.jar这个jar包里面是有这两个类的,引入之后运行,发现还需要引入log4j-core-2.0.2.jar这个类,引入这两个类之后发现还是会报错,报错信息如下:[plain] view plain copy print?ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 该错误的意思应该是没有找到配置文件,使用了默认的配置文件,只有error级别的错误可以在控制台打印,这时往下读文档。The output from the call to logger.info() will vary significantly depending on the configuration used. See theConfiguration section for more details. 调用logger.info()进行输入将会很大程度的依赖配置的使用。更多细节详见配置一章。从这里可以看出,我们需要的配置的内容应该在这个配置章节中,所以跳转到配置章节进行学习。ConfigurationInserting 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%的代码是致力于logging的。因此,甚至一个适中大小的应用都会有数以千记的logging块嵌入他们的代码中。由于他们的数量,不用手动修改他们变得十分重要了。Configuration of Log4j 2 can be accomplished in 1 of 4 ways:配置log4j 2可以用下面1到4方式来配置:Through a configuration file written in XML, JSON, or YAML.Programmatically, by creating a ConfigurationFactory and Configuration implementation.Programmatically, by calling the APIs exposed in the Configuration interface to add components to the default configuration.Programmatically, by calling methods on the internal Logger class.1.通过写一个XML,JSON或者YAML文件来进行配置2.通过编程的方式,通过创建一个ConfigurationFactory类和Configuration接口实现类来配置。3.通过编程的方式,通过调用暴露在Configuration接口中的API,添加组件到默认的配置中。4.通过编程的方式,通过调用在Logger类内部的方法来实现。This page focuses primarily on configuring Log4j through a configuration file. Information on programmatically configuring Log4j can be found atExtending Log4j 2.这页主要是通过一个配置文件来配置log4j。通过编程的方式来实现的信息可以在Extending Log4j 2这个链接中找到。这里我只先学习通过XML,JSON或者YAML配置文件来进行配置的方式,通过编程来实现的方式暂时还用不到,所以这里不点击链接进去学习了,以后有需要再去看Automatic ConfigurationLog4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange then in weighted order from highest to lowest. As delivered, Log4j contains two ConfigurationFactory implementations, one for JSON and one for XML.Log4j有能力在它初始化期间进行自动配置。当Log4j开始时,它将会部署所有的ConfigurationFactory插件并且按照按照权重进行从高到低的排序。当交付的时候,Log4j包含两个显示ConfigurationFactory接口的实现类,一个对应JSON,一个对应XML