当前位置:网站首页>Log (common log framework)
Log (common log framework)
2022-07-02 06:23:00 【I think starfish_ ninety-eight】
List of articles
Common log framework
Log4j
Log4j yes Apache One of the Java The logstore of , By using Log4j, We can control the destination of log information transmission ( Console 、 file 、 Database etc. ); We can also control the output format of each log ; By defining the level of each log message , We can control the log generation process more carefully .
Logback
Logback, One “ reliable 、 Universal 、 Fast and flexible Java Log framework ”.logback Currently divided into three modules :logback-core,logback- classic and logback-access.logback-core It is the basic module of the other two modules .logback-classic yes log4j An improved version of . Besides logback-classic Complete implementation SLF4J API So that you can easily change to other log system , Such as log4j or JDK14 Logging.logback-access Module and Servlet Containers ( Such as Tomcat and Jetty) Integrate , In order to provide HTTP Access logging . Please note that , You can go to logback-core It's easy to build your own modules .
Log4j2
Apache Log4j 2 It's right Log4j The upgrade , It's better than its predecessor Log4j 1.x Provides significant improvements , And provides Logback Many of the improvements available in , And fixed Logback Some problems in Architecture .
Now the best Java The log framework is Log4j2, Not one of them. . According to official tests , In multithreaded environment ,Log4j2 Asynchronous logging is better . In the asynchronous log ,Log4j2 Use independent threads to execute I/O operation , Can greatly improve the performance of the application .
Performance comparison of the three frameworks :
effect
debugging
stay Java During project commissioning , Viewing the stack information can easily know the running state of the current program , The output log is convenient for recording the previous running results of the program . If you use a lot of System.out perhaps System.err, This is the most convenient and effective way , But it's not professional enough .
The wrong location
Don't think that if the project can run correctly, you can rest easy , After the project has been running for some time , Maybe due to data problems , Network problems , Memory problems and other exceptions . At this time, logs can help developers or operation and maintenance personnel quickly locate the wrong location , Propose solutions .
Data analysis
The rise of big data , Make a lot of log analysis possible ,ELK It also lowers the threshold of log analysis . The log contains a lot of user data , Including click behavior , Interest preference, etc , The user portrait can guide the company's next strategic direction .
Log4j2
Apache Log4j 2 It's right Log4j The upgrade , It's better than its predecessor Log4j 1.x Provides significant improvements , And provides Logback Many of the improvements available in , And fixed Logback Some problems in Architecture . It's the best Java Log framework , Not one of them. .
features
API Separate
Log4j Of API Separate from implementation , So that application developers can clearly understand which classes and methods can be used , While ensuring forward compatibility . This makes Log4j Teams can improve in a safe and compatible way .
Performance improvement
Log4j 2 Include based on LMAX Disruptor Library's next generation of asynchronous recorders . In a multithreaded scenario , The throughput of asynchronous recorder is better than Log4j 1.x and Logback high 18 times , Low latency .
Auto reload configuration
And Logback equally ,Log4j 2 You can automatically reload its configuration when you modify it . And Logback Different , It will not lose log events when reconfiguration occurs .
Advanced filtering
And Logback equally ,Log4j 2 Support based on Log Context data in events , Mark , Regular expressions and other components for filtering . Besides , Filters can also be associated with recorders . And Logback Different , You can use generic... In any of these cases Filter class .
Plug-in architecture
Log4j Use plug-in mode to configure components . therefore , You don't have to write code to create and configure Appender,Layout,Pattern Converter etc. . When configured ,Log4j Automatically identify plug-ins and use them .
No garbage mechanism
During steady state logging ,Log4j 2 Garbage free in stand-alone applications , stay Web Low garbage in applications . This reduces the pressure on the garbage collector , And it can provide better response performance .
Use
Citation depends on
it is to be noted that , Simultaneous introduction spring-boot-starter and spring-boot-starter-web When you depend on , Need to be in spring-boot-starter Exclude from spring-boot-starter-logging rely on , because spring-boot-starter-web Depend on spring-boot-starter, according to Maven Dependency relationship , stay spring-boot-starter Exclude from spring-boot-starter-logging Success is ruled out by dependence .
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- exclude spring boot The default log logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- introduce log4j2 rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Add configuration file
By default ,Log4j2 stay classpath The following search name is log4j2.xml
Configuration file for . You can also use Java The start command specifies the full path of the configuration file .-Dlog4j.configurationFile=opt/demo/log4j2.xml
, You can still use it Java The code specifies the configuration file path :
public static void main(String[] args) {
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
File file = new File("opt/demo/log4j2.xml");
loggerContext.setConfigLocation(file.toURI());
}
Asynchronous profile
Log4j-2.9 And later versions require disruptor-3.3.4.jar Or later . stay Log4j-2.9 Before , need disruptor-3.0.0.jar Or later
By default , Asynchronous recorders do not pass positions to I / O Threads . If you need location information for a layout or custom filter , You need to set... In the configuration of all related recorders “includeLocation = true”, Including the root logger .
introduce disruptor rely on
<dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency>
stay src/java/resources Catalog add log4j2.component.properties The configuration file
# Set asynchronous logging system properties log4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
The configuration is as follows :
adopt log.info(“ Whether it is an asynchronous log :{}”, AsyncLoggerContextSelector.isSelected()); You can check whether it is an asynchronous log .
<?xml version="1.0" encoding="UTF-8"?> <Configuration> <Properties> <!-- Log output level --> <Property name="LOG_INFO_LEVEL" value="info"/> <!-- error The level of log --> <Property name="LOG_ERROR_LEVEL" value="error"/> <!-- Create a file in the current directory called log Directory is the directory where logs are stored --> <Property name="LOG_HOME" value="./log"/> <!-- File log storage directory --> <Property name="LOG_ARCHIVE" value="./log/archive"/> <!-- Module name , Affect log configuration name , Log file name , Configure according to your own project --> <Property name="LOG_MODULE_NAME" value="spring-boot"/> <!-- Log file size , Beyond this size will be compressed --> <Property name="LOG_MAX_SIZE" value="100 MB"/> <!-- How many days of logs to keep --> <Property name="LOG_DAYS" value="15"/> <!-- Format of output log :%d Indicates the date ,%thread Represents the thread name ,%-5level: The level is shown from the left 5 Character width , %msg: Log message ,%n Is a newline --> <Property name="LOG_PATTERN" value="%d [%t] %-5level %logger{0} - %msg%n"/> <!--interval Property is used to specify how often to scroll --> <Property name="TIME_BASED_INTERVAL" value="1"/> </Properties> <Appenders> <!-- Console output --> <Console name="STDOUT" target="SYSTEM_OUT"> <!-- Format of output log --> <PatternLayout pattern="${LOG_PATTERN}"/> <!-- Console output only level And above (onMatch), Other direct rejection (onMismatch)--> <ThresholdFilter level="${LOG_INFO_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> </Console> <!-- This will print out all the info Above grade ,error Log below level , Each time the size exceeds size Or satisfied TimeBasedTriggeringPolicy, The log will be automatically saved under the folder created by the date of year and compressed , As archive --> <!-- Asynchronous logs are automatically refreshed in batches , So will immediateFlush Property is set to false--> <RollingRandomAccessFile name="RollingRandomAccessFileInfo" fileName="${LOG_HOME}/${LOG_MODULE_NAME}-infoLog.log" filePattern="${LOG_ARCHIVE}/${LOG_MODULE_NAME}-infoLog-%d{yyyy-MM-dd}-%i.log.gz" immediateFlush="false"> <Filters> <!-- If it is error Level reject , Set up onMismatch="NEUTRAL" You can let the log go through subsequent filters --> <ThresholdFilter level="${LOG_ERROR_LEVEL}" onMatch="DENY" onMismatch="NEUTRAL"/> <!-- If it is info\warn Output --> <ThresholdFilter level="${LOG_INFO_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <!--interval Property is used to specify how often to scroll , Based on the current filePattern Set yes 1 The day rolls once --> <TimeBasedTriggeringPolicy interval="${TIME_BASED_INTERVAL}"/> <SizeBasedTriggeringPolicy size="${LOG_MAX_SIZE}"/> </Policies> <!-- DefaultRolloverStrategy Property if not set , By default, it can save at most in the same folder 7 File --> <DefaultRolloverStrategy max="${LOG_DAYS}"/> </RollingRandomAccessFile> <!-- Record only error Log above level , And info Level logs are saved in different files --> <RollingRandomAccessFile name="RollingRandomAccessFileError" fileName="${LOG_HOME}/${LOG_MODULE_NAME}-errorLog.log" filePattern="${LOG_ARCHIVE}/${LOG_MODULE_NAME}-errorLog-%d{yyyy-MM-dd}-%i.log.gz" immediateFlush="false"> <Filters> <ThresholdFilter level="${LOG_ERROR_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <TimeBasedTriggeringPolicy interval="${TIME_BASED_INTERVAL}"/> <SizeBasedTriggeringPolicy size="${LOG_MAX_SIZE}"/> </Policies> <DefaultRolloverStrategy max="${LOG_DAYS}"/> </RollingRandomAccessFile> </Appenders> <Loggers> <!-- Development environment use --> <!--<Root level="${LOG_INFO_LEVEL}"> <AppenderRef ref="STDOUT"/> </Root>--> <!-- test , Production environment use --> <Root level="${LOG_INFO_LEVEL}" includeLocation="false"> <AppenderRef ref="RollingRandomAccessFileInfo"/> <AppenderRef ref="RollingRandomAccessFileError"/> </Root> </Loggers> </Configuration>
边栏推荐
猜你喜欢
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
注解和反射详解以及运用
Hydration failed because the initial UI does not match what was rendered on the server.问题原因之一
LeetCode 78. 子集
队列(线性结构)
Linear DP (split)
The official zero foundation introduction jetpack compose Chinese course is coming!
Zhuanzhuanben - LAN construction - Notes
Redis——大Key問題
Pbootcms collection and warehousing tutorial quick collection release
随机推荐
LeetCode 90. 子集 II
Summary of WLAN related knowledge points
浅谈三点建议为所有已经毕业和终将毕业的同学
Cglib代理-代码增强测试
LeetCode 40. Combined sum II
【程序员的自我修养]—找工作反思篇二
Redis---1. Data structure characteristics and operation
Singleton mode compilation
CUDA中的Warp Shuffle
LeetCode 39. 组合总和
BGP报文详细解释
BGP 路由優選規則和通告原則
【每日一题】—华为机试01
深入学习JVM底层(四):类文件结构
LeetCode 27. Removing Elements
I/o multiplexing & event driven yyds dry inventory
加密压缩文件解密技巧
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
Linear DP (split)
Find the highest value of the current element Z-index of the page