log4j technology
1. What is logging technology and why use logging technology in development
Log: It is a mainstream enterprise log technology provided by Apache. It records key information and records errors and exceptions during system operation. The amount of log code accounts for 4% of the total code amount
* Distinguish between System.out and logging techniques
* System.out input information to the console, must output
* Log technology has a level, which controls whether the log is output, the level of detail of the output, and the destination of the output (console, file, sending email)
Usage log: mainly used for debugging during the development process and maintenance after the project goes online (recording bugs)
2. Use log4j to configure the log framework to use the configuration file
src/log4j.xml
src/log4j.properties (simple)
Configuring log4j.properties has three components
①Component 1: Loggers are used to configure the log output level and which output sources to use [A logger specifies multiple output sources (log output targets)]
Format: loggername=level, outputsource1, outputsource2...
// info is the log level, stdout is the output source name log4j.rootLogger=info, stdout
* log4j provides log levels from high to low: fatal (fatal error), error (normal error), warn (warning), info (information), debug (debugging), trace (stack)
* When log4j logs, it will only record information at a higher level of configuration
②Component 2: Output source (Appenders) Multiple output sources (console, log file, email, database) can be defined in log4j
Format: log4j.appender.output source name=implementation class
log4j.appender.stdout=org.apache.log4j.ConsoleAppender // Output to console log4j.appender.file=org.apache.log4j.FileAppender // Output to file
③Component 3: What information does Layouts record in the log
// Custom layout log4j.appender.stdout.layout= org.apache.log4j.PatternLayout // Custom layout format (see layout related documentation, read its meaning) log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE } %5p %c{1}:%L - %m%n
3. Use log4j to record logs in the program
①Step 1: Define the logger (in the Java class)
// The parameter is the current class private static final Logger LOG = Logger.getLogger(Log4jTest.class );
②Step 2: Use log4j to provide each level method to record logs
- LOG.fatal("fatal error");
- LOG.error("normal error");
- LOG.warn("Warning message");
- LOG.info("General Information");
- LOG.debug("Debug information");
- LOG.trace("stack information");
*Common: error, warn, info, debug
example(
log4j.properties
):
### Loggers Appenders Layouts ### ### org.apache.log4j.ConsoleAppender console ### 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 %c{1}:%L - %m%n ### org.apache.log4j.FileAppender file ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=d:/mylog.log ### org.apache.log4j.PatternLayout can flexibly specify the layout pattern ### log4j.appender.file.layout=org.apache.log4j.PatternLayout ### % d The date or time of the output log time point, the default format is ISO8601 ### ### % p output priority, ie DEBUG, INFO, WARN, ERROR, FATAL### ### % c The category to which the output belongs, usually the full name of the category. ### ### % l Output the location of the log event, including the category name, the thread where it occurred, and the number of lines in the code ### ### % m output the message specified in the code ### ### % n output a carriage return line feed, "\r\n" for Windows platforms, "\n" for Unix platforms ### log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### log4j.rootLogger logger is divided into debug debug info message warn warning error error ### log4j.rootLogger=debug,stdout