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 does log4j dynamically generate file names based on variables

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

Share

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

This article will explain in detail how log4j dynamically generates file names according to variables. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Dynamically generate simple log4j settings for file names based on variables

In general, the log4j configuration file is simply set to:

Log4j.rootLogger=debug,stdout,Rlog4j.appender.R=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.R.File=test.loglog4j.appender.R.MaxFileSize=100KBlog4j.appender.R.DatePattern ='.' yyyy-MM-ddlog4j.appender.R.layout=ex.log4j.ExPatternLayoutlog4j.appender.R.layout.ConversionPattern=%d {yyyy-MM-dd HH:mm:ss}% c% 5p -% m%nlog4j.appender.R.MaxBackupIndex=1

A simple example.

The java code is as follows:

Public class LogTest {static Logger log = Logger.getLogger (LogTest.class); public static void main (String [] args) throws IOException {PropertyConfigurator.configure ("log4j.properties"); log.debug ("helo");}}

In general, simple log4j requirements can be met by setting up as above.

If you need to type the logs into different files according to the variables in the program, (for example, according to an ID, it is easier to find the desired logs for a large number of logs)

Just go to the configured FileAppender in the java code, and then serFile to modify the file name.

The java code is as follows:

FileAppender appender = (FileAppender) log.getRootLogger (). GetAppender ("R"); appender.setFile (filePath/fileName); appender.activateOptions ()

Before printing the log, you can set the above code.

Note: add appender.activateOptions (); so that the log printed later will not overwrite the previous log.

Log4j dynamic file name

In the project, there are a variety of requirements for log output, the following is a detailed analysis of the dynamic log file name output.

Generate log according to the user's ID

In this case, the logger can be generated dynamically based on each user's ID.

The code is as follows:

Import org.apache.log4j.FileAppender;import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.apache.log4j.PatternLayout;import org.apache.log4j.RollingFileAppender;public class LoggerUtil {public static Logger getLoggerByName (String name) {/ / generate a new Logger / / return the Logger logger = Logger.getLogger (name) if you already have a Logger instance; / / empty the Appender. In particular, be sure to initialize logger.removeAllAppenders () when you do not want to use the storage practice; / / set the Logger identity. Logger.setLevel (Level.DEBUG); / / sets whether to inherit the parent Logger. / / default is true. Thanks to root. / / root will not be exported after false is set. Logger.setAdditivity (true); / / generate a new Appender FileAppender appender = new RollingFileAppender (); PatternLayout layout = new PatternLayout (); / / output form String conversionPattern = "[% d]% p% c -% m% n"; layout.setConversionPattern (conversionPattern); appender.setLayout (layout) / / log output path / / the environment variable [catalina.home] is used here, and String tomcatPath = java.lang.System.getProperty ("catalina.home") can only be obtained in tomcat environment; appender.setFile (tomcatPath + "/ logs/" + name + ".log"); / / log text code appender.setEncoding ("UTF-8") / / true: append false to the existing log file: the new log overrides the previous log appender.setAppend (true); / / applies the current configuration appender.activateOptions (); / / adds the new Appender to the Logger logger.addAppender (appender); return logger;}} II. In the batch program, implement each batch through a setting

Different settings for file names.

Log.xml definition

Log4j can accept custom environment variables. Note that the following line [${tuhan.crm.log.name}] is custom environment variables.

So how to set the environment variable? there are two ways.

Set in 1.Java (declare these variables in the program before using the configuration file):

System.setProperty ("tuhan.crm.log.name", "batch001")

two。 Set in JVM

Java-Xmx512M-Dtuhan.crm.log.name=batch001 com.stu.TestBatch001 3. In the batch program, each batch is realized through multiple settings.

Different settings for file names.

Dynamic profile path: (log4j can accept URL)

Java.net.URL URL = consandpatt.class.getResource ("/ com/test/java/log/config/log4jConfig.properties"); PropertyConfigurator.configure (URL); on "log4j how to dynamically generate file names based on variables" this article shares here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

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