Slf4j and log4j Configuration

Source: Internet
Author: User

In the selection of slf4j-api-1.7.7.jar + slf4j-log4j12-1.7.7.jar + log4j-1.2.17.jar output (the premise is that there is a corresponding configuration file log4j. properties, put in the root directory, usually SRC folder) to the file logs. log and errors. Log


1. The basic format is as follows:

# Configure root loggerlog4j. rootlogger = [level], appendername1, appendername2 ,... # Configure the log output destination appenderlog4j. appender. appendername = fully. Qualified. Name. Of. appender. Class log4j. appender. appendername. option1 = value1... Log4j. appender. appendername. optionn = valuen # configure the log information format (layout) log4j. appender. appendername. layout = fully. qualified. name. of. layout. class log4j. appender. appendername. layout. option1 = value1... Log4j. appender. appendername. layout. optionn = valuen

The [level] log output level has a total of five levels.

FATAL      0  ERROR      3  WARN       4  INFO       6  DEBUG      7 

Appender is the log output destination. log4j provides the following types of appender:

Org. apache. log4j. consoleappender (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)

Layout: log output format. log4j provides the following layout formats:

Org. apache. log4j. htmllayout (layout in HTML form), org. apache. log4j. patternlayout (you can flexibly specify the layout mode), org. apache. log4j. simplelayout (Level and information string containing log information), org. apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)

Print parameters: log4j uses a format similar to the printf function in C language to format log information, as shown below:

<Span style = "font-size: 12px;"> % m the output priority of the message % P specified in the output code, that is, debug, info, warn, error, fatal </span>
% R the number of milliseconds it takes to output the log information from application startup to output
<Span style = "font-size: 12px;"> % C output category, usually the full name of the class </span>
<Span style = "font-size: 12px;"> % t name of the thread that outputs the log event </span>
<Span style = "font-size: 12px;"> % N returns a carriage return line break, which is "\ r \ n" on Windows ", the UNIX platform is "\ n" </span>
<Span style = "font-size: 12px;"> % 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 {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22: 10: 28,921 </span>
<Span style = "font-size: 12px;"> % L location of log event output, including category name, thread, and number of lines in the code. Example: test. Main (test. Java: 10) </span>

Parameter description

Type of output-level error is a serious error mainly because the program error warn is a general warning. For example, the session loss info is the information to be displayed, for example, logging out of DEBUG is the debugging information of the program.
Configure the log4j. appender. appendername = ?? 1.org. apache. log4j. consoleappender (console) 2.org. apache. log4j. fileappender (File) 3.org. apache. log4j. dailyrollingfileappender (a log file is generated every day) 4.org. apache. log4j. rollingfileappender (a new file is generated when the file size reaches the specified size) 5.org. apache. log4j. writerappender (send log information to any specified place in stream format)
Configure the log information format log4j. appender. appendername. layout = ?? 1.org. apache. log4j. htmllayout (layout in HTML form), 2.org. apache. log4j. patternlayout (you can flexibly specify the layout mode), 3.org. apache. log4j. simplelayout (Level and information string containing log information), 4.org. apache. log4j. ttcclayout (including the log generation time, thread, category, and so on)
Consoleappender option Threshold = Debug: Specify the lowest level of log message output. Immediateflush = true: the default value is true, meaning that all messages will be output immediately. Target = system. Err: system. Out by default, which specifies the output Console
Fileappender option Threshold = debuf: specify the minimum output level of log messages. Immediateflush = true: the default value is true, meaning that all messages will be output immediately. File1_mylog.txt: refers to the output to the mylog.txt file. Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content.
Rollingfileappender option Threshold = Debug: Specify the lowest level of log message output. Immediateflush = true: the default value is true, meaning that all messages will be output immediately. File1_mylog.txt: refers to the output to the mylog.txt file. Append = false: The default value is true. To add a message to a specified file, false means to overwrite the specified file content. Maxfilesize = 100kb: The suffix can be kb, MB, or GB. When the log file reaches this hour, it will automatically scroll, moving the original content to the mylog. log.1 file. Maxbackupindex = 2: specify the maximum number of rolling files that can be generated.
Meanings of several symbols in log information format:-X: Left alignment for output of X information; % P: Priority of output log information, that is, debug, info, warn, error, fatal, % 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 {YYY Mmm dd hh: mm: SS, SSS}, output is similar to: October 18, 2002 22:10:28, 921% R: The number of milliseconds that the application started to output the log information % C: the category of the output log information, this is usually the full name of the Class % T: name of the thread that outputs the log event % L: location where the log event is output, equivalent to % C. % m (% F: % L) combination, including the category name, the thread that occurs, and the number of lines in the code. Example: testlog4.main (testlog4.java: 10) % x: Output NDC (nested Diagnostic Environment) associated with the current thread, especially in applications with multiple clients and multithreading such as Java Servlets. %: Output A "%" character % F: name of the file where the output Log message was generated % L: Row Number % m in the output code: output the message specified in the Code, specific log information % N: Output A carriage return line break. For Windows, the line feed is "\ r \ n", and for UNIX, the line feed is "\ n ".

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.