[SLF4j] Use the log component SLF4j to write system logs

Why write logs?

The benefits of adding logs to the program:
1. Monitor the changes of variables in the code, and periodically record them in files for statistical analysis by other applications
2. Track the runtime trace of the code as a basis for future audits
3. Responsible for integrated development The role of the debugger in the environment, printing debug information of the code to the file or console

How to use slf4j

write configuration file

In fact, you can also not use the configuration file at all, but configure the Log4j environment in the code. However, using configuration files will make your application more flexible.
Log4j supports two configuration file formats, one is an XML format file, and the other is a Java property file (key=value). Whether you use slf4j or log4j, the writing in the configuration file is the same. The configuration file is written as follows:

# 定义Log输出级别:将等级为INFO的日志信息输出到Console,File两个目的# 地(两个目的地的命名可自定义,下面的代码随着命名的改变而改变)
log4j.rootLogger=INFO,Console,File

# 定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
# 定义日志输出格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%t] %l:%n %m%n

# 定义日志输出目的地为文件
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
# 文件输出目录(包括文件名)
log4j.appender.File.File=qc.log
# 定义日志输出格式
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern==%d{yyyy-MM-dd HH\:mm\:ss} %p [%t] %l:%n %m%n

The configuration file can be placed anywhere, but remember to fill in the correct path when reading the configuration file.
The layouts provided by Log4j are as follows:
org.apache.log4j.HTMLLayout (layout in HTML form),
org.apache.log4j.PatternLayout (the layout mode can be flexibly specified),
org.apache.log4j.SimpleLayout (including log Information level and information string),
org.apache.log4j.TTCCLayout (including log generation time, thread, category, etc.)

Use slf4j in your program

1. Get the logger (ChromeLogin is the class name of the class):
static Logger LOGGER = LoggerFactory.getLogger(ChromeLogin.class);

2. Read the configuration file:

// 自动快速地使用缺省Log4j环境。
BasicConfigurator.configure () 
// 读取使用Java的特性文件编写的配置文件。(即.properties文件)
PropertyConfigurator.configure ( String configFilename)
// 读取XML形式的配置文件。
DOMConfigurator.configure ( String filename ) 

3. Insert record information:

Logger.debug ( Object message ) ; 
Logger.info ( Object message ) ; 
Logger.warn ( Object message ) ; 
Logger.error ( Object message ) ;

example:

public class ChromeLogin {
    static Logger LOGGER = LoggerFactory.getLogger(ChromeLogin.class);
    public static void main(String[] args){
        PropertyConfigurator.configure("Log4j.properties");
        //打印日志信息
        LOGGER.info("hello log4j !");
    }
}

Symbolic meaning of log output format

%p: The priority of output log information, namely DEBUG, INFO, WARN, ERROR, FATAL,
%d: The date or time of the output log time point, the default format is ISO8601, and the format can also be specified after it, for example: %d{ yyy MMM dd HH:mm:ss,SSS}, the output is similar: October 18, 2002 22:10:28, 921
%r: The number of milliseconds it took to output the log information from the application startup to outputting the log information
%c: The log information was output The category it belongs to is usually the full name of the class it belongs to.
%t: Output the thread name that generated the log event
%l: Output the location of the log event, which is equivalent to the combination of %C.%M(%F:%L), Include the category name, the thread in which it occurred, and the number of lines in the code. Example: Testlog4.main (TestLog4.java:10)
%F: The name of the file where the output log message is generated
%L: The line number in the output code
%m: The message specified in the output code, the specific log information generated
%M : Output the name of the method to print the log
%n: Output a carriage return line feed, "\r\n" for Windows platform, "\n" for Unix platform to output log information line feed

References
About Log4j
Log4j Official Documentation
Using Log4j for Log Operations
Log4j Log Storage Location and Related Basic Configuration
Log Components slf4j Introduction and Configuration Details

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325875612&siteId=291194637