Log4J’s Configuration File is used to set the logger level, repository, and layout. It can take Settings in key=value format or XML format. Through configuration, you can create a Log4J runtime environment.

Configuration file The Log4J configuration file is in the following format:

Log4j. RootLogger = [level], appenderName1, appenderName2,... . # Appender configuration log information output destination log4j appenders.. AppenderName = fully qualified). The name of the Appender. Class . Log4j appenders. AppenderName. Option1 = value1... . Log4j appenders. AppenderName. OptionN = valueN # configure the format of the log information (layout) log4j. Appender. AppenderName. Layout = .... Fully qualified name of the layout. The class log4j. Appender. AppenderName. Layout. Option1 = value1... log4j.appender.appenderName.layout.optionN = valueNCopy the code

[level] indicates the log output level. There are five levels:

FATAL       0  
ERROR      3  
WARN       4  
INFO         6  
DEBUG      7
Copy the code

Appenders are log output destinations and Log4j provides the following appenders:

Org, apache log4j. ConsoleAppender (console), org.. Apache log4j. FileAppender (documents), Org, apache log4j. DailyRollingFileAppender (every day to create a log file), org.. Apache log4j. RollingFileAppender (file size to specified size to create a new file). Org, apache log4j. WriterAppender (the log information to flow format to send to any designated place)Copy the code

Layout: log output format. Log4j provides the following layouts:

Org, apache log4j. HTMLLayout (layout) in HTML form,. Org. Apache log4j. PatternLayout (the flexibility to specify layout mode), Org, apache log4j. SimpleLayout (string) contains the log information level and information,. Org. Apache log4j. TTCCLayout (containing the produce time, thread, categories, etc.)Copy the code

Log4J formats the log information in a format similar to printf in C:

%m Displays the message specified in the code. %p Displays the priority of the message, that is, DEBUG, INFO, WARN, ERROR, FATAL %r Displays the number of milliseconds between the start of the application and the output of the log. %c Displays the class to which the log belongs. %t Displays the name of the thread that generated the log event. %n Displays a carriage return newline character (/r/n on Windows or /n on Unix). %d Displays the date or time of the log point in time. %d{yyy MMM dd HH:mm:ss, SSS}, the output is similar to: October 18, 2002 22:10:28, 921% l The output contains the location of the log event, including the class name, the thread that occurred, and the number of lines in the code. Example: testlog4.main (testlog4.java: 10)Copy the code

Initialize Logger in code: 1) in a program called BasicConfigurator. Configure () method: Add a ConsoleAppender to the root logger with the output format PatternLayout set to “%-4r [%t] %-5p % C %x – %m%n” and the root logger’s default Level is level.debug. 2.) Put the configuration in the file, From the command line parameter file name, through the PropertyConfigurator. Configure (args [x]) are resolved and configuration; 3) Put the configuration in a file, pass the filename and other information through environment variables, and use log4J default initialization process to parse and configure; 4) The configuration is placed in a file, and information such as file name is passed through the application server configuration, using a special servlet to complete the configuration.

Set log levels for different appenders: When debugging a system, we tend to focus only on exception level log output, but usually all levels of log output are in one file, if the level of log output is BUG! ? Well, take your time. At this point we might think that it would be nice to be able to output the exception information to a single file. Log4j already provides this functionality by modifying the Appender Threshold in configuration, as shown in the following example:

[Configuration file]

### set log levels ### log4j.rootLogger = debug , stdout , D , Output to the console E # # # # # #. Log4j appenders. Stdout = org.. Apache log4j. ConsoleAppender log4j. Appender. Stdout. Target = System. Out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = % d {ABSOLUTE} % 5 p % c {1} : % L - % m % n output to the log file # # # # # # log4j. Appender. D = org.. Apache log4j. DailyRollingFileAppender Log4j. Appender. D.F ile = logs/log. The log log4j. Appender. Da ppend = true log4j. Appender. Which hreshold = DEBUG more than # # output level DEBUG log  log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH: mm: ss} [% t: % r] - [p] % % m % n # # # save exception information to separate file # # # log4j. Appender. D = org.. Apache log4j. DailyRollingFileAppender Log4j. Appender. D.F ile = logs/error log # # exception log filename log4j. Appender. Da ppend = true. Log4j appenders. Which hreshold = error # # Only logs at or above the ERROR level are output!! log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%nCopy the code

[Used in code]

public class TestLog4j { public static void main(String[] args) { PropertyConfigurator.configure( " D:/Code/conf/log4j.properties " ); Logger logger = Logger.getLogger(TestLog4j. class ); logger.debug( " debug " ); logger.error( " error " ); }}Copy the code

Add log4j.properties to the usual configuration in the project

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p [%t] %c{1}:%L - %m%n


log4j.rootLogger=warn, stdout, R

log4j.logger.com.yun=debug

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=E:/yun/yun.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%t] %5p  %c - %m%n
Copy the code