LOG4J 2.x usage Precautions

Source: Internet
Author: User
Tags json valid log4j


Directory (?) [+]1. Main components
a) Logger HierarchyIn Log4j 1.x, the hierarchical relationships of each logger are maintained through multiple logger. In log4j 2.x this relationship will cease to exist and replace the relationship between the Loggerconfig objects. Logger and Loggerconfig are named entities, and the names of logger are case-sensitive and follow the following naming conventions:
[Plain]View plain Copy if the name of a loggerconfig is prefixed by another loggerconfig name, then it is said that this loggerconfig is the ancestor of another loggerconfig. If there is no other ancestor loggerconfig between one loggerconfig and another loggerconfig, then it is said that this loggerconfig is the parent of another loggerconfig.
For example, a loggerconfig named Com.foo is the parent of Loggerconfig with the name Com.foo.Bar. Similarly, Java is the parent of Java.util, the ancestor of Java.util.Vector. This naming pattern is quite familiar to most developers.
One exception, however, is root loggerconfig, at the top of any loggerconfig level. The following code can set a loggerconfig to root loggerconfig:[JavaScript]View plain copy Logger Logger = Logmanager.getlogger (logmanager.root_logger_name); or Logger Logger = Logmanager.getrootlogger ();
All logger can be obtained by Logmanager.getlogger static method by passing the logger name.b) LoggercontextIn the log system, Loggercontext plays an important role. However, depending on the actual situation, there may be multiple valid Loggercontext in an application.c) ConfiguarationEach loggercontext has a valid configuaration that contains appenders, Context-wide Filters, Loggerconfigs, and references to Strsubstitutor. During reconfiguration, two configuaration will be present, and once the logger is re-assigned to the new configuaration, the old Configuaration will stop working and discard. See section 3rd: Configuration Filesd) LoggerAs mentioned earlier, the logger (Logger) is created by calling the Logmanager.getlogger static method. The logger simply has a noun and is associated with a loggerconfig. Calling the Logmanager.getlogger method with the same name will get a reference to the same logger. For example:[Java]View plain copy Logger x = Logmanager.getlogger ("Wombat");   Logger y = Logmanager.getlogger ("Wombat"); SYSTEM.OUT.PRINTLN (x = = y); Print Truee) Loggerconfig f) Filter g) Appender h) Layout i) strsubstitutor, Strlookup 2. Jar Package Introduction [Java]View plain copy package Guwen;   Import Org.apache.logging.log4j.LogManager;      Import Org.apache.logging.log4j.Logger;              public class Logtest {private static final Logger log = Logmanager.getlogger (Logtest.class);           public static void Main (string[] args) {log.error ("Hello, log4j.");   Log.info ("Hello, log4j.");}} Log.debug ("Hello, log4j.")} }

Log4j 2.x requires the introduction of two jar packages, namely Log4j-api.jar and Log4j-core.jar. Execute the above code with the following result:

ERROR statuslogger No log4j2 configuration file found. Using default configuration:logging only errors to the console.

15:11:52.731 [main] ERROR Guwen. Logtest-hello, log4j.

If Log4j-core.jar is not introduced, it can be run, but the results are as follows:

ERROR Statuslogger Log4j2 could not find a logging implementation. Please add Log4j-core to the classpath. Using Simplelogger to log to the console ...

ERROR logtest Hello, log4j. 3. Configuration Files

As you can see from the tips above, log4j can work even if there is no configuration file to introduce the jar package. However, the Red section suggests that only error messages will be printed to the console if no configuration is provided.

Log4j 2.x can automatically load configuration files at initialization time, LOG4J2 provides three implementations of Configuarationfactory: JSON, YAML, XML.

The initialization process for the log4j 2.x is:

(1) log4j checks the Log4j.configuarationfile system properties, and if this property is set, it will try to use the configuarationfactory corresponding to the file extension to load the configuration.

(2) If there is no Log4j.configuarationfile system attribute, YAML Configuarationfactory will try to find Log4j2-test.yaml or log4j2-test.yml in classpath;

(3) If the YAML configuration file is not found, JSON Configuarationfactory will try to find Log4j2-test.json or log4j2-test.jsn in classpath;

(4) If the JSON configuration file is not found, XML Configuarationfactory will try to find the log4j2-test.xml in Classpath;

(5) If none of the above test environment configuration files can be found, YAML Configuarationfactory will try to find Log4j2.yaml or log4j2.yml in classpath;

(6) If the YAML configuration file is not found, JSON Configuarationfactory will try to find Log4j2.json or log4j2.jsn in classpath;

(7) If the JSON configuration file is not found, XML Configuarationfactory will try to find the log4j2.xml in Classpath;

(8) If no configuration file is found, log4j will use defaultconfiguaration, which will cause the log to be output to the console. After testing, the so-called Log4j.configuarationfile property is stored in a key-value pair in the Log4j2.component.properties property file under Classpath, and its value is the specified profile name. 4. Log Level

As already seen in part 2nd, log4j can also output normal log information with the default configuration when no configuration is provided. In the case of XML configuration, the default configuration is actually equivalent to the configuration of the following configuration file:

[HTML]   View plain copy <?xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.