Preface
For details about log4j2, refer to the previous blog.
A good thing about log4j2 is that you can use configuration file to configure log system. However, the storage location of this configuration file is quite tangled. This article describes how to change the path and name of the log4j2 configuration file.
Directory
The content of this article will answer the following questions:
1. How to generate a logevent?
2. What is the transfer of logevent?
3. How do I change the name and path of the log4j2 configuration file?
Body
1. How to generate a logevent?
Logevent is generated when you call functions such as info, error, and trace of a logger object. Like loggerconfig, logevent is also level-based. The level of logevent is mainly used to determine where to stop when the event is passed. For details, refer to the previous log.
Import Org. apache. logging. log4j. logmanager; Import Org. apache. logging. log4j. logger; Public class test { private static logger = logmanager. getlogger ("helloworld" ); Public static void main (string [] ARGs) {test. logger.info ( "Hello, world" ); test. logger. error ( "there is a error here" );}}
For exampleCodeAs shown in, two logevents are generated.
2. What is the transfer of logevent?
Run this in IDE.ProgramTo see what output will be.
It is found that only the error statement is output. What about the info statement?
Don't worry. Let's take a look at the project directory structure:
As you can see, no configuration files are written in the project. Therefore, the application should use the default loggerconfig level. What is the default level?
In the source file of log4j2, you can see the Java Doc of its defaultconfiguration class, which is described as follows:
/**
*Default Configuration writes all output to the console using the default logging level.You configure Default logging level by setting
* System property "org. Apache. Logging. log4j. Level" to a level name.If you do not specify the property, log4j uses the error level.Log
* Events will be printed using the basic formatting provided by each message.
It can be seen that the default output location is the console, and the default level is the error level.
So why is the default error level blocking Info-level information?
See the following table:
The vertical bar on the left is the level of the event, and the horizontal bar on the right is the level of loggerconfig. Yes means that the event can be filtered, and no means that the event cannot be filtered.
As you can see, info-level events cannot be accepted by the filter of the error-level loggerconfig. Therefore, info information is not output.
3. How to change the log4j2 name and path?
If you want to change the default configuration, you need configuration file. Log4j configuration is written in the log4j. properties file, but log4j2 can be written in XML and JSON files.
(1) Put it under classpath (SRC) and name it log4j2. xml
Generally, log4j2 is used to write a log4j2. xml file in the src directory. This is not controversial.
(2) Place the configuration file elsewhere
In the system project, it is inconvenient to put the log4j2 configuration file under the src directory. If you can put all the configuration files used in the project in a folder, of course, it will be more neat and better managed. To achieve this, the premise is that the log4j2 configuration file can be located elsewhere, rather than under classpath.
So is this possible?
Let's take a look at the document:
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.
2.if no system property is set the JSON configurationfactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
3.If no such file is found the XML configurationfactory will look for the log4j2-test.xml in the classpath.
4.if a test file cannot be located the JSON configurationfactory will look for log4j2. JSON or log4j2. jsn on the classpath.
5.If a json file cannot be located the XML configurationfactory will try to locate log4j2. XML on the classpath.
6.if no configuration file cocould be located the defaultconfiguration will be used. this will cause logging output to go to the console.
It can be seen that if"Log4j. configurationfile"System property, the application will find the configuration file in the classpath in the following order:
Log4j2-test.json or log4j2-test.jsn files
Log4j2-test.xml files
Log4j2. JSON or log4j2. jsn File
Log4j2. xml file
This is why the log4j2. xml file under the src directory can be identified.
If you want to rename the configuration file and place it elsewhere, you need to set System PropertiesLog4j. configurationfile.
The setting method is to write the key and value of this attribute in VM arguments:
-Dlog4j. configurationfile = "D: \ learning \ blog \ 20130115 \ config \ logconfig. xml"
In myeclipse, right-click "Run as-" Run configuration-"and choose" (x) = argument "=" VM arguments "in the right window.
Then write the preceding key and value.
-D is a parameter and cannot be missing.
Test
Write the file in the path "D: \ learning \ blog \ 20130115 \ config:
The level of root loggerconfig is set to info.
Write Data in myeclipseLog4j. configurationfileSystem attributes:
The tested Java program is shown above and will not be repeated here. Run. The console output is as follows:
The long-awaited info statement appears.
Summary:
The most tangled part of this test is the relocation of the configuration file. First, Google. On overflowstack, we can see that some people have the same problem, as on csdn, but no one said how to solve it. Read the manual.Log4j. configurationfile: set the system attributes. I tried it (the parameter-D was not written at the time) and failed. I thought this method was incorrect. I downloaded it from the log4j2 website.Source codeTo find the path to change. Find the finalLog4j. configurationfile. Helpless, and back to the origin. In the end, the parameter-D was not written. Ah, heaven ......