The website is about to be released. To track some logs, you need to use log4j. So I studied the configuration of log4j.
First, paste a configuration source file log4j. properties.
log4j.rootLogger=DEBUG, CONSOLE, FILE## for consolelog4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n## for filelog4j.appender.FILE=org.apache.log4j.RollingFileAppenderlog4j.appender.FILE.File=D:/logs/log4j.loglog4j.appender.FILE.MaxFileSize=1MBlog4j.appender.FILE.Append = truelog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH\:mm\:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n
The following is an important explanation:
Log4j. rootlogger = debug, console, file ----- configure the root logger. The first word "debug" after the equal sign indicates the output level (there are five levels of fatal errorwarn info Debug. the more detailed the output is, in order, each level contains all the preceding outputs and shields all the subsequent outputs. Here, we recommend two types: Error: only error information is recorded; debug: detailed information of all errors is recorded. In the production configuration, disable the "apply to console" option and only enable file). The following two consoles and files represent the location of log output, and the following two can be named at will, to be clear and easy to understand, you can use words with a glance. For example, the console represents the console output, and the file represents the file output.
Log4j. appender. console = org. apache. log4j. consoleappender ------- pay attention to the console before the equal sign, which corresponds to your definition above. This sentence means that the console is defined as Org. apache. log4j. consoleappender (log4j console output). log4j provides the following appender types:
Org. Apache. log4j. leleappender (console)
Org. Apache. log4j. fileappender (file)
Org. Apache. log4j. dailyrollingfileappender (a log file is generated every day)
Org. Apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size)
Org. Apache. log4j. writerappender (send log information to any specified place in stream format)
Log4j. appender. Console. layout = org. Apache. log4j. patternlayout ------- defines the output layout (output format) used by log4j ).
Log4j provides the following four layout styles:
Org. Apache. log4j. htmllayout (layout in HTML form)
Org. Apache. log4j. patternlayout (you can flexibly specify the layout mode, that is, you can customize the output style ),
Org. Apache. log4j. simplelayout (including the log information level and information string ),
Org. Apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)
Log4j. appender. console. layout. conversionpattern = % d {mm-DDHH: mm: SS} [% C-% L] [% T] [%-4r]-% m % N ------- this is the output format customized for patternlayou, this article focuses on the printing parameters. log4j uses a format similar to the printing format of the printf function in C language to format log information.
% 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 carriage return line break. For Windows, the value is \ r \ n, and for UNIX, the value is \ n. That is, one line is occupied by a hop message.
% D date or time of the log output time point, followed by a pair of curly brackets for custom format
% C output category, usually the full name of the class
% L accurate to line
% X output alignment
The following sentence refers to the one that I extract from the log information output from my console. The comparison of the definition formats above is similar.
09-3010: 34: 04 [COM. opensymphony. xwork2.config. providers. InterceptorBuilder-45] [main] [106]-No interceptor found for name = includemethods
Log4j. appender. file. File = D:/logs/log4j. Log -------- define the location and file name of the output file
Log4j. appender. file. maxfilesize = 1 MB ---------- defines the size of each file. If the size is exceeded, a new file is created. Note that the unit is MB or kb.