当前位置:网站首页>About the reload of log4j2 and the output of different levels of logs to different log files
About the reload of log4j2 and the output of different levels of logs to different log files
2022-04-22 20:10:00 【bglmmz】
log4j2 The configuration file format can be JSON, perhaps XML.
Usually in classpath Find... In the path log4j2.json, perhaps log4j2.xml,
Alternatively, you can set the configuration file through system parameters , such as ( Generally, this is used for applications launched from the command line ):
-Dlog4j.configurationFile=path/to/log4j2.xml
When the app starts , Except for Jean log4j2 stay classpath And system parameters , Other than initialization , We can also reload at any time in the code log4j2 Configuration file for , Reconfigure .
for example , Consider the following common scenarios :WEB application , We don't want to put log4j2.xml Pack into your own jar In file ( Such changes log4j2 The configuration of is troublesome ), I don't want to log4j2.xml Put it in WEB-INF/classes below ( I don't want users to operate casually WEB-INF The files under the ), Then we can put log4j2.xml And configuration files used in other projects , Put it in a centralized place , such as TOMCAT/bin/config Next , At this time , Change if initialization log4j2 Well ? We can provide one InitServlet, for example :
...
public void init() throws ServletException {
String configRoot = this.getInitParameter("configRoot");
String log4j2Config = configRoot + File.separator + this.getInitParameter("log4j2Config");
File file = new File(log4j2Config);
try {
LoggerContext context =(LoggerContext)LogManager.getContext(false);
context.setConfigLocation(file.toURI());
// Reinitialize Log4j2 Configuration context for
context.reconfigure();
} catch (MalformedURLException e) {
e.printStackTrace();
}
//todo: I got it here logger, It has been initialized according to the new configuration file
logger = LogManager.getLogger(DefaultInitServlet.class);
}
Corresponding , As long as web.xml Configure this InitServlet, And provide configRoot and log4j2Config Just two paths ( You can also not configure configRoot, But through System.getProperty("user.dir") To get the running directory of the application , Yes tomcat for , This directory is tomcat/bin, Other applications such as command line , Namely bat/sh Directory of )
web.xml
[quote]
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>test.InitServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<init-param>
<!-- Profile root -->
<param-name>configRoot</param-name>
<param-value>d://config</param-value>
</init-param>
<init-param>
<!-- log4j2 Relative path of configuration file -->
<param-name>log4j2Config</param-name>
<param-value>log4j2/log4j2.xml</param-value>
</init-param>
</servlet>
[/quote]
Okay , Talked about how to reinitialize log4j2, Let's talk about how to put different levels of logs , Output to different log files . This is online , Including on the official website , None of them is clear .
such as , hope trace/debug Level log output to debug.log, and info Level log output to info.log, Other such as warn/error/fatal Level logs are output to error.log, It's good to separate the output in this way . We follow the following log42j.xml Configuration of , Such output can be realized .
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="off" monitorInterval="1800">
<Properties>
<Property name="log-path">d://logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
</Console>
<File name="app_debug" fileName="${log-path}/app/debug.log" append="false">
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
</File>
<File name="app_info" fileName="${log-path}/app/info.log" append="false">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
</File>
<File name="app_error" fileName="${log-path}/app/error.log" append="false">
<Filters>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="%d{yyyy.MM.dd HH:mm:ss z} %-5level %class{36}.%M()/%L - %msg%xEx%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="com.test.app" level="trace" additivity="false">
<appender-ref ref="Console"/>
<appender-ref ref="app_debug"/>
<appender-ref ref="app_info"/>
<appender-ref ref="app_error"/>
</Logger>
</Loggers>
</configuration>
The main thing is to understand ThresholdFilter Of onMatch/onMismatch The three option values of :ACCEPT/DENY/NEUTRAL, Actually , According to the literal meaning , It's easy to understand .
It is important to , If there are more than one ThresholdFilter, that Filters Is a must , At the same time Filters in , First, filter the non-conforming log level , Put what you don't need first DENY fall , And then again ACCEPT Required log level , This order cannot be reversed .
版权声明
本文为[bglmmz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221916328654.html
边栏推荐
- 【Unity】可玩广告Luna Playable插件的踩坑记录
- [渝粤教育] 南阳理工学院 英语漫话中国文化 参考 资料
- Time format of go language
- Advanced IPC - DBUS details
- What if I don't understand? Google's 540 billion parameter new model can explain the laugh point and guess the movie through Emoji expression
- 怎么才能申购新股呢?申购新股安全吗?
- 2022-01-12 wechat applet debugging
- 文件上传问题记录
- Micro diary: Those seemingly insignificant details and experiences
- Household intelligent projector is the first choice, entering the polar meter h3s projection
猜你喜欢

解决金仓数据库KingbaseES无法打开锁文件的问题

判断是否发生塑性变形的条件:von Mises屈服准则

嵌入式Web项目(一)——Web服务器的引入

Micro diary: Those seemingly insignificant details and experiences

Linux下安装Mysql 8.0 + eclipse配置详细教程

Module 4 operation

Format for creating Zimbra LDAP users using LDAP clients

隔壁的wifi,我秒破

顶测科技整理转行过程中一定要避免的问题
![[eight part essay] thread safety](/img/64/9508be7c7cb39a41a5ba4ecd611ee8.png)
[eight part essay] thread safety
随机推荐
Pytorch deep learning practice 09 multi classification questions
sys_ctl启动kingbase单机服务时报错:could not bind IPv4 address “0.0.0.0“: Address already in use
Open source, free and best used 3 systems and 9 firewall software Amway gives you
Anaconda creates a new environment and installs the GPU version of pytorch
国内期货开户到哪个期货公司开更安全?
Packaging PHP with rpmbuild
Acrobat Pro DC 教程,如何使用密码保护 PDF 文件?
In 2022, the most underestimated function of postman, the efficiency of automated interface testing, is simply invincible
How can I apply for new shares? Is it safe to apply for new shares?
家用智能投影仪首选,走进极米H3S投影
为什么我建议你从事技术岗,而非文职,销售
CVPR 2022: is smile recognition also sexist? Zhejiang University and Wuhan University jointly set up a fairness improvement framework
DNS解析流程&基础知识
[eight part essay] redis cache
【实习总结】
Hard work alone is not enough! And this
新一代代理利器 - traefik
What is the reason why the camera device with built-in 4G card of Haikang cannot register with easycvr platform?
应届毕业生的头一份工作有多重要?
KingbaseES中数据库启动安装程序方法