Log4j is an open-source project of Apache. By using log4j, we can control the log information shipping destination: console, file, GUI component, and even an interface server, NT event recorder, Unix
Syslog daemon. We can also control the output format of each log. By defining the level of each log information, we can control the log generation process in greater detail. The most interesting thing is that these can be configured flexibly through a configuration file, without the need to modify the application code.
Official station: http://logging.apache.org/log4j/
Steps for using log4j:
1. Add the JAR file of log4j
2. Create the log4j. properties file under classpath and write the corresponding content.
3. Configure and modify attributes
4. Add related statements to the class for log output
Simple use example: use the most basic configuration here
First, put log4j. jar in the classpath environment of the project.
import org.apache.log4j.BasicConfigurator;import org.apache.log4j.Logger;public class LogTest {static final Logger logger = Logger.getLogger(LogTest.class);public static void main(String[] args) {BasicConfigurator.configure();logger.debug("debug message");logger.info("info message");logger.warn("warn message");logger.error("error message");logger.fatal("fatal message");}}
After running, you can see that some Japanese messages are printed. This is the role of log4j.
You can also use the configuration file log4j. properties.
Use propertyconfigurator. Configure ("log4j. propertites ");
You can also write logs to log files.
Example of the log4j. properties file:
Log4j. rootlogger = debug, CA, Fa
# Leleappender
Log4j. appender. CA = org. Apache. log4j. leleappender
Log4j. appender. Ca. layout = org. Apache. log4j. patternlayout
Log4j. appender. Ca. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % N
# File appender
Log4j. appender. Fa = org. Apache. log4j. fileappender
Log4j. appender. Fa. File = test. Log
Log4j. appender. Fa. layout = org. Apache. log4j. patternlayout
Log4j. appender. Fa. layout. conversionpattern = %-4r [% T] %-5 p % C % x-% m % N
# Set the loggerlevel of file appender to warn
Log4j. appender. Fa. Threshold = warn
You can also use the XML file configuration as follows:
<?xml version="1.0" encoding="UTF-8" ?>02.<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">03.<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>04.<appender name="CA" class="org.apache.log4j.ConsoleAppender">05.<layout class="org.apache.log4j.PatternLayout">06.<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />07.</layout>08.</appender>09.<appender name="FA" class="org.apache.log4j.FileAppender">10.<param name="File" value="sample.log"/>11.<param name="Threshold" value="WARN"/>12.<layout class="org.apache.log4j.PatternLayout">13.<param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />14.</layout>15.</appender>16.<root>17.<level value="DEBUG" />18.<appender-ref ref="CA" />19.<appender-ref ref="FA" />20.</root>21.</log4j:configuration>