Log4j novice quick start tutorial
brief introduction
Log4j is an open source project of Apache (official website) http://jakarta.apache.org/log4j ), by using log4j in the project, we can control the log information output to the console, files, GUI components, and even the database. We can control the output format of each log. By defining the output level of the log, we can more flexibly control the log output process. Facilitate the commissioning of the project.
form
Log4j is mainly composed of loggers, Appenders and layout. Loggers controls the output level of logs and whether logs are output; Appenders specifies the output method of logs (output to console, file, etc.); layout controls the output format of log information.
log level
Log4j at org apache. log4j. Level class defines seven log levels: off, fat, error, warn, info, debug and all:
Note: generally, only 4 levels are used, and the priority from high to low is error > warn > info > debug
Appender (output)
Appender is used to specify where the log is output, and the output destination of the log can be specified at the same time. Log4j commonly used output destinations are as follows:
Layout (log formatter)
quick get start
First, introduce the dependency of log4j in Maven:
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
Create a Log4jTest class to test the use of log4j:
public class Log4JTest { public static void main(String[] args) { //获取Logger对象的实例 Logger logger = Logger.getLogger(Log4JTest.class); //使用默认的配置信息,不需要写log4j.properties BasicConfigurator.configure(); //设置日志输出级别为WARN,这将覆盖配置文件中设置的级别,只有日志级别高于WARN的日志才输出 logger.setLevel(Level.WARN); logger.debug("这是debug"); logger.info("这是info"); logger.warn("这是warn"); logger.error("这是error"); logger.fatal("这是fatal"); } }
Here, first use the logger GetLogger (Log4Test.class) creates Logger instances, and then calls BasicConfigurator.. The configure () method specifies that the logger uses the default configuration information, and then calls the logger Setlevel (level. Warn) sets the log output level of the logger to warn. Run the main function, and the console will output the error message above warn. The console output is as follows:
Note: if basicconfigurator is not called If the configure () method is used, an error will be reported when running the main function, because the log4j framework will load log4j under the project path when running Properties configuration file (the use of the configuration file will be explained later). However, this file is not available in our project at this time. If the name of the configuration file is not log4j.properties, you can specify the name of the configuration file through propertyconfigurator.configure (string configfilename).
Use of formatter
Modify the code in Log4jTest:
public class Log4JTest { public static void main(String[] args) { Logger logger = Logger.getLogger(Log4JTest.class); BasicConfigurator.configure(); HTMLLayout layout = new HTMLLayout(); // SimpleLayout layout = new SimpleLayout(); try { FileAppender appender = new FileAppender(layout,"D:\\out.html",false); logger.addAppender(appender); //设置日志输出级别为info,这将覆盖配置文件中设置的级别,只有日志级别高于WARN的日志才输出 logger.setLevel(Level.WARN); logger.debug("这是debug"); logger.info("这是info"); logger.warn("这是warn"); logger.error("这是error"); logger.fatal("这是fatal"); } catch (IOException e) { e.printStackTrace(); } } }
First, create a formatter (htmllayout) in HTML format, and then create the file output end (fileappender). Specify that the output end is in HTML format. Here, I specify that the output path is the out.html file under disk D. then load the file output end into the logger through logger.addappender (appender). Run the main function and an out will be generated under disk D HTML file, open the file, and the information in it is the log information output in the code:
log4j. Use of properties configuration file
The above code is used to set the output format of the logger. In this way, we have to set it on each class to output the log. The configuration is too troublesome. There is a more convenient method. We just need to create a new log4j. In the project path The log4j framework will automatically load the configuration file and set the configuration information to the logger. The simplest configuration file is as follows:
# 控制台输出配置 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n # 文件输出配置 log4j.appender.A = org.apache.log4j.DailyRollingFileAppender log4j.appender.A.File = D:/log.txt #指定日志的输出路径 log4j.appender.A.Append = true log4j.appender.A.Threshold = DEBUG log4j.appender.A.layout = org.apache.log4j.PatternLayout #使用自定义日志格式化器 log4j.appender.A.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n #指定日志的输出格式 log4j.appender.A.encoding=UTF-8 #指定日志的文件编码 #指定日志的输出级别与输出端 log4j.rootLogger=DEBUG,Console,A
In log4j In the properties configuration file, we define the log output level and output end, and configure the log output format in the output end respectively.
Then write the test class Log4jTest java :
public class Log4JTest { public static void main(String[] args) { //获取Logger对象的实例 Logger logger = Logger.getLogger(Log4JTest.class); logger.debug("这是debug"); logger.info("这是info"); logger.warn("这是warn"); logger.error("这是error"); logger.fatal("这是fatal"); } }
Console output text after program running:
And a log will be generated on disk D Txt file. The text reads as follows:
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.