当前位置:网站首页>Basic use of Log4j2
Basic use of Log4j2
2022-08-10 06:34:00 【clear0217】
Log4j2基本使用
Log4j2简介
log4j2是log4j 1.x 的升级版,2015年5月,Apache宣布log4j1.x 停止更新.最新版为1.2.17.
log4j2参考了logback的一些优秀的设计,并且修复了一些问题,因此带来了一些重大的提升,主要有:
1、异常处理:在logback中,Appender中的异常不会被应用感知到,但是在log4j2中,提供了一些异常处理机制.
2、性能提升:log4j2相较于log4j 1和logback都具有很明显的性能提升.
3、自动重载配置:参考了logback的设计,提供自动刷新参数配置,可以动态的修改日志的级别而不需要重启应用.
4、无垃圾机制,log4j2在大部分情况下,都可以使用其设计的一套无垃圾机制,避免频繁的日志收集导致的jvm gc.
1.Jar包依赖
直接依赖
在Maven的父工程pom.xml添加如下依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
</dependencies>
</dependencyManagement>
使用bom管理依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.12.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
然后在使用Log4j2的子工程pom.xml添加如下依赖,
This eliminates the need to write a specific version number,
Make sure that the subprojects of the project all use the same versionLog4j2.
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
</dependencies>
在复杂场景下,May not only rely on the above twojar包,
这种方法可以保证Log4j2Versions are consistent between modules.
2.测试代码
为了演示Log4j2的功能,Create the following test class:
package org.apache.logging.log4j;
public class Log4j2Test {
private static Logger LOG = LogManager.getLogger(Log4j2Test.class);
public static void main(String[] args) throws Exception {
// The log keeps printing,用于测试Log4j2功能
while (true) {
logAll();
}
}
// Print logs at various levels for testing
public static void logAll() throws Exception {
LOG.trace("trace level log");
LOG.debug("debug level log");
LOG.info("info level log");
LOG.error("error level log");
LOG.fatal("fatal level log");
// 设置休眠时间(单位ms),控制日志打印速度
Thread.sleep(3);
}
}
3.打印日志
运行上面的测试程序,
The following log is printed on the console:
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
11:33:08.438 [main] ERROR org.apache.logging.log4j.Log4j2Test - error level log
11:33:08.440 [main] FATAL org.apache.logging.log4j.Log4j2Test - fatal level log
可以看到ERROR报错了,没有找到Log4j2的配置文件,
使用了默认配置,只打印errorlevel log toconsole控制台,
Then the console outputerror和fatalTwo levels of log information.
4.日志级别
Log4j2共有8种日志级别,
Sort by priority from smallest to largest:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
边栏推荐
猜你喜欢
第12章 数据库其它调优策略【2.索引及调优篇】【MySQL高级】
高质量WordPress下载站模板5play主题
【8月9日活动预告】Prometheus峰会
腾讯云宋翔:Kubernetes集群利用率提升实践
1413. 逐步求和得到正数的最小值
裸辞—躺平—刷题—大厂(Android面试的几大技巧)
Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
Can‘t find bundle for base name jdbc, locale zh_CN解决方法
指纹浏览器在使用易路代理时常见的问题及解决办法
Qt借助隐藏控件和QSS绘制重复元素
随机推荐
Two-dimensional cartoon rendering - coloring
关于Qt高频率信号槽合并的误解和方案
数据库学习之表的约束
The difference between initializing objects as null and empty objects in JS
Log4j2基本使用
求问各位大佬,FLink SQL读取source的时候去指定水位线的时间字段,如果指定的这个字段中格
MySQL笔记
OpenGL学习笔记(LearnOpenGL)-第二部分 绘制三角形
C语言文件操作
第12章 数据库其它调优策略【2.索引及调优篇】【MySQL高级】
手机与雷电模拟器里如何使用YiLu代理?
Unity瓦片地图取消部分刚体效果
新手使用 go channel 需要注意的问题
Qt滚动条(QScrollBar)圆角样式问题跟踪
QEMU guest与host通过网络通信——bridge/hostfwd/guestfwd
[网络安全]实操AWVS靶场复现CSRF漏洞
VS Code插件国际化
进制的前缀表示和后缀表示
裸辞—躺平—刷题—大厂(Android面试的几大技巧)
数据库学习之数据类型