当前位置:网站首页>Slf4j and log4j2 process logs
Slf4j and log4j2 process logs
2022-07-25 16:27:00 【Brother Xing plays with the clouds】
About log4j
Log4j + Slf4j Is the most common use combination , But we know that Log4j It has stopped updating at present .Apache A new Log4j2 Instead of Log4j,Log4j2 It's right Log4j The upgrade , Its predecessor Log4j There has been a significant improvement in comparison , And provided many Logback Available improvements , At the same time Logback Some inherent problems in the architecture . therefore ,Log4j2 + Slf4j Should be the general trend of the future .
About slf4j
- LF4J Unlike other logging libraries , It is quite different from other logging libraries .SLF4J(Simple logging Facade for Java) Not a real logging implementation , It's an abstraction layer ( abstraction layer), It allows you to use any logging library in the background . If written for internal and external use API Or generic class libraries , Then you really don't want clients that use your library to have to use the logging library of your choice .
- If a project is already in use log4j, And you load a library of classes , For example Apache Active MQ—— It depends on another logging library logback, Then you need to load it in as well . But if Apache Active MQ Used SLF4J, You can continue to use your logging library without the pain of loading and maintaining a new logging framework .
- in general ,SLF4J Make your code independent of any particular log API, This is for API Great ideas from developers . The idea of abstract logging libraries is not new, though , and Apache commons logging It's already in use , but SLF4J Is rapidly becoming Java World logging standards .
Use case
introduce slf4j and log4j2 The core of the package
<!-- slf4j Core packages -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
<scope>runtime</scope>
</dependency>
<!--log4j2 Core packages -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.8.2</version>
<scope>runtime</scope>
</dependency>
<!--log4j2 Asynchronous rely on -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version>
</dependency>log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="off" monitorInterval="120">
<properties>
<property name="LOG_HOME">/mytest_log</property>
</properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<!--RollingFile Global synchronization RandomAccessFile For asynchronous -->
<RollingRandomAccessFile name="rootAppeder"
fileName="${LOG_HOME}/rattanapi.log"
filePattern="${LOG_HOME}/$${date:yyyy-MM}/rattanapi-root-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %ex%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
<!-- Error log output -->
<RollingRandomAccessFile name="errorAppeder"
fileName="${LOG_HOME}/rattanapi-error.log"
filePattern="${LOG_HOME}/$${date:yyyy-MM}/rattanapi-error-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %ex%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="30"/>
</RollingRandomAccessFile>
</appenders>
<loggers>
<asyncRoot level="info">
<!-- Depending on whether the configuration file is open or not console Output -->
<appender-ref ref="Console"/>
<appender-ref ref="rootAppeder"/>
<appender-ref ref="errorAppeder"/>
</asyncRoot>
</loggers>
</configuration>web.xml Set in log4j2 Listeners and filters (servlet3.0 And above versions do not require this step ) Development Servlet3.0 The program needs some environmental support .
<!-- about log4j2,Servlet2.5 Previous versions required -->
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>Be careful :log4j2 No longer supported properties The file , Only support xml,json or yaml, Does not specify the location of the default in src/main/resources The lookup .
If you need to customize the location , It needs to be up there web.xml Add the following code in
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:log4j2.xml</param-value>
</context-param>import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
String world = "world";
logger.info("hellp world:{}",world);
logger.error("exception e");
}
}边栏推荐
- pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
- 链游开发现成版 链游系统开发详细原理 链游源码交付
- MySQL页锁
- Win11动态磁贴没了?Win11中恢复动态磁贴的方法
- 【读书会第13期】+FFmpeg开源项目
- 终极套娃 2.0 | 云原生交付的封装
- Talk about how to use redis to realize distributed locks?
- 从业务需求出发,开启IDC高效运维之路
- Leetcode:528. select randomly according to the weight [ordinary random failure + prefix and dichotomy]
- 0x80131500打不开微软商店的解决办法
猜你喜欢

可验证随机函数 VRF

leetcode:154. 寻找旋转排序数组中的最小值 II【关于旋转排序数组的中后定位二分法】
![Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]](/img/b5/5c7fc70b8025cf7ef21d645a3ac22e.png)
Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]
![[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code](/img/39/716c62d6ca533a7e84704b2c55d072.png)
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code

Crazy God redis notes 12

Cookie、cookie与session区别

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具

easyui修改以及datagrid dialog form控件使用

Use huggingface to quickly load pre training models and datasets in moment pool cloud

Various useful forms of London Silver K-line chart
随机推荐
3D 语义分割——Scribble-Supervised LiDAR Semantic Segmentation
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
Breakthrough in core technology of the large humanoid Service Robot Walker x
柏睿数据加入阿里云PolarDB开源数据库社区
Solve win10 disk occupation of 100%
测试框架-unittest-测试套件、结果输出到文件
城市燃气安全再拉警钟,如何防患于未“燃”?
【读书会第13期】+FFmpeg开源项目
leetcode:154. 寻找旋转排序数组中的最小值 II【关于旋转排序数组的中后定位二分法】
Cookie、cookie与session区别
Understanding service governance in distributed development
Permission management - delete menu (recursive)
Talk about how to use redis to realize distributed locks?
C # simulation lottery
权限管理-角色分配菜单
EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
Gap Locks(间隙锁)
Various useful forms of London Silver K-line chart
C# 模拟抽奖
【读书会第13期】+FFmpeg视频采集功能