# Output format
# % M output the specified message in the code
# % P output priority, namely debug, info, warn, error, fatal
# % R the number of milliseconds it takes to output the log information from application startup to output
# % C output category, usually the full name of the class
# % T name of the thread that outputs the log event
# % N output a line break, "\ r \ n" for Windows, and "\ n" for Unix"
# % D date or time of the log output time point. The default format is iso8601. You can also specify the format after it, for example, % d {yyyy Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921
# % L location of log event output, including category name, thread, and number of lines in the code. Example: testlog4.main (testlog4.java: 10)
Format example:
Log4j. appender. thisproject. file. out. layout. conversionpattern = log4j --> % d {yyyy Mmm dd hh: mm: SS, SSS} % 5 p {% F \: % L}-% m % N
Note:
1. Information formatting tips
These parameters may contain numbers such as % 5 P, which means how many spaces are added before the parameter is output, and "\" is used to escape characters.
2. Location of the log4j. properties File
(1) In a Java project, it is placed in the root directory of the project rather than in the src directory of the project.
(2) In the javaweb project, it is placed in the src directory, because Tomcat will load it by default, we do not need to manually load the log4j configuration file. We only need to obtain the logger instance we need according to the configuration. Therefore, we can know that if we are not using a tomcat container, does it need to be manually loaded or configured at least? For example, non-Apache servers such as WebSphere are used.
3. Configure the log location in the Java Web project
(1) If you want to set the log file to the log.txt file on the D Drive.
Log4j. appender. thisproject. file. Out. File = D: \ log.txt
(2) if the location of the specified log file is a file in the current Tomcat working directory
Log4j. appender. thisproject. file. Out. File =$ {Catalina. Home}/logs/logs_tomcat.log
4. log4j Loading Mechanism
What is the loading mechanism of log4j. properties? In fact, log4j. properties is only the configuration file of log4j. When the program starts, the log4j component reads log4j. properties, which is similar to reading common configuration files. Obtain the log4j attribute values configured by the user. Call the desired method to set the log4j attribute.
Use log4j. properties as a struts. xml or a hibernate-cfg.xml. But the difference is that, unlike struts and hibernate, log4j is not an independent component and cannot be initialized by itself. Generally, it is required to initialize any component. For example, the default log component of Hibernate is log4j, which initializes log4j during hibernate initialization. If you do not configure log4j. properties, Hibernate will throw an exception, but it can still be initialized normally.