当前位置:网站首页>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");
}
}边栏推荐
- Product upgrade observation station in June
- Crazy God redis notes 12
- 使用 Terraform 在 AWS 上快速部署 MQTT 集群
- 乐观锁悲观锁适用场景
- Typescript learning 2 - Interface
- MYSQL导入sqllite表格的两种方法
- [JS advanced] JS regular correlation functions and regular objects_ 02
- Solve win10 disk occupation of 100%
- [wechat applet] detailed explanation of applet host environment
- MySQL显式锁
猜你喜欢

MySQL之联表查询、常用函数、聚合函数

Test Driven Development (TDD) online practice room | classes open on September 17

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

如何安装govendor并打开项目

Waterfall flow layout

聊聊如何用 Redis 实现分布式锁?

Verifiable random function VRF

Product upgrade observation station in June
![[wechat applet] detailed explanation of applet host environment](/img/57/582c07f6e6443f9f139fb1af225ea4.png)
[wechat applet] detailed explanation of applet host environment

可验证随机函数 VRF
随机推荐
Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)
The annualized interest rate of treasury bonds is too low. Is there a financial product with a higher annualized interest rate than the reverse repurchase of treasury bonds?
Which led display manufacturer is better
C# 模拟抽奖
Breakthrough in core technology of the large humanoid Service Robot Walker x
MySQL global lock
【小5聊】公众号排查<该公众号提供的服务出现故障,请稍后>
Fastadmin TP installation uses Baidu rich text editor ueeditor
MySQL table write lock
EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
[image hiding] digital image watermarking method technology based on hybrid dwt-hd-svd with matlab code
IaaS基础架构云 —— 云网络
PageHelper.startPage没有生效问题
食品安全丨无处不在的冷冻食品,你真的了解吗?
[JS advanced] JS regular correlation functions and regular objects_ 02
Release of v6.5.1/2/3 series of versions of Xingyun housekeeper: the ability of database OpenAPI continues to be strengthened
2W word detailed data Lake: concept, characteristics, architecture and cases
Record locks
测试框架-unittest-命令行操作、断言方法
【读书会第13期】+FFmpeg视频采集功能