First understand the log4j printing parameters as follows:
parameter | Description |
---|---|
%L | Line number in output code |
%l | The location of the output log event, including the category name, the thread that occurred, and the number of lines in the code, such as: Testlog.main(TestLog.java:10) |
%m | Output the message specified in the code |
%p | Output priority, namely DEBUG, INFO, WARN, ERROR, FATAL |
%c | The category to which the output belongs, usually the full name of the category |
%c{?} | The output category, the question mark? can be filled with numbers, which means the level of the output category, 1 means only output the first level category, for example %c{1}: the category name is com.skeleton.demo, and the output is When, only demo |
%t | Output the name of the thread that generated the log event |
%n | Output a carriage return and line feed, "\r\n" for Windows platform and "\n" for Unix platform |
%d | The date or time of the output log time point, the default format is ISO8601, you can also specify the format afterwards such as: %d{yyyy year MM month dd day HH: mm: ss, SSS}, the output is similar to: January 05, 2012 22:10:28,921 |
From this we can set various styles as follows:
- The most commonly used style:
[%p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
Results: - The above looks very complicated, we can customize the style:
[%p]: %c{3}(%c{1}.java:%L)\t\t %n%m%n
result - or:
[%p]: %c{3}(%c{1}.java:%L)\t\t %m%n