Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use log4j2 custom configuration file location and file name

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use log4j2 custom configuration file location and file name", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use log4j2 custom configuration file location and file name"!

Log4j2 Custom profile location and File name

The general way to use log4j2 is to place the log4j2.xml file in the root of the resource folder. For obsessive-compulsive developers, I prefer to create a new package or folder under the resource folder and put the configuration file in it. This blog will show you how to customize the location and file name of the log4j2.xml file.

Web.xml configure the path to the edu.example.holder.system.Log4j2ConfigListener log configuration file log4j.configurationFile log4j/log4j2.xml

The Log4j2ConfigListener class is a custom class that implements the ServletContextListener interface so that the default path and file name of the log configuration file can be changed when tomcat starts.

Effective configuration file package edu.example.holder.system;import java.util.Enumeration;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import org.apache.logging.log4j.core.config.Configurator;public class Log4j2ConfigListener implements ServletContextListener {private static final String KEY = "log4j.configurationFile"; @ Override public void contextDestroyed (ServletContextEvent arg0) {} @ Override public void contextInitialized (ServletContextEvent arg0) {String fileName = getContextParam (arg0) Configurator.initialize ("Log4j2", "classpath:" + fileName);} @ SuppressWarnings ("unchecked") private String getContextParam (ServletContextEvent event) {Enumeration names = event.getServletContext () .getInitParameterNames (); while (names.hasMoreElements ()) {String name = names.nextElement (); String value = event.getServletContext () .getInitParameter (name) If (name.trim () .equals (KEY)) {return value;}} return null }} Log profile instance ${sys:catalina.home} / WebAppLogs/SSHExample % 5p [% t]% d {yyyy-MM-dd HH:mm:ss} (% FRV% L)% m% n % 5p [% t]% d {yyyy-MM-dd HH:mm:ss} (% FRV% L)% m% n % 5p [% t]% d {yyyy-MM-dd HH:mm:ss} (% FRV% L)% m% n % 5p [% t]% d {yyyy-MM-dd HH:mm:ss} (% FRV% L)% m% n % 5p [% t]% d {yyyy-MM-dd HH:mm:ss} (% FRV% L)% m% n Log4j2.xml configuration and example 1. You need to download the package to use log4j2

As follows:

two。 Configuration files can be in three formats

(the file name must be standardized, otherwise the system cannot find the configuration file):

Classpath is named as log4j-test.json or log4j-test.jsn file

Classpath under the name log4j2-test.xml

Classpath is named as log4j.json or log4j.jsn file

Classpath under the name log4j2.xml

3. There are two methods of logging: static Logger logger = LogManager.getLogger (MyApplication.class.getName ()); private static Logger logger = LogManager.getLogger ("MyApplication"); 4. The configuration of the xml I used

So introduce the xml configuration:

1. First of all, there are several appender to be used. These appender functions are different, such as regular output, major abnormal output, you may need them to output in different forms or output files, which can be configured in appender.

Some configuration instructions in 2.appender:

The name of the name:appender

FileName: output file and name

Append: whether to append or not. True means to append the content to the log, and false means to overwrite it every time.

FilePattern: indicates that the log reaches the specified size or time, and when a new log is generated, the named path of the old log

PatternLayout: specify the format of the output log. For more information, please see the previous blog.

Policies: policy, indicating when the log should generate a new log. There can be time policy, size policy, etc.

ThresholdFilter: filter, there are a lot of filter in log4j2

3. Next, configure logger.

Specify which classes do what output, which is relatively simple, you can look directly at the chestnut:

E:/log% d%-5p% t (% CMV% L) -% m% n SYSTEM_OUT GBK ${log.layout} GBK ${log.layout} so far I believe you have a deeper understanding of "how to use log4j2 custom configuration file location and file name". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report