Dynamically generate file names based on variables
Simple log4j settings
Generally speaking, the log4j configuration file is simply set to:
=debug,stdout,R =. = =100KB = '.'yyyy-MM-dd =ex. =%d{yyyy-MM-dd HH:mm:ss} %c %5p - %m%n =1
Simple example
The java code is as follows:
public class LogTest { static Logger log = (); public static void main(String[] args) throws IOException { (""); ("helo"); } }
Generally, the settings above can meet the simple log4j needs.
If you need to type the log into a different file according to the variables in the program (for example: according to a certain ID, it is easier to find the desired log when 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) ().getAppender("R"); (filePath/fileName); ();
Before printing the log, set the above code to it.
Note: Adding (); can make the logs printed later not overwrite the previous logs.
log4j dynamic file name
In the project, there are many requirements for log output. Let’s analyze the output of dynamic log file names in detail.
1. Generate logs according to user ID
In this case, loggers can be generated dynamically based on each user ID.
The code is as follows:
import .; import .; import .; import .; import .; public class LoggerUtil { public static Logger getLoggerByName(String name) { // Generate a new logger // If you have a Logger instance, return the existing one Logger logger = (name); // Clear Appender. Especially if you don't want to use existing examples, you must initialize them. (); // Set Logger level. (); // Set whether to inherit the parent Logger. // Default true. Continue root output. // No root will be output after setting false. (true); // Generate a new Appender FileAppender appender = new RollingFileAppender(); PatternLayout layout = new PatternLayout(); // log output form String conversionPattern = "[%d] %p %t %c - %m%n"; (conversionPattern); (layout); // log output path // The environment variable [] is used here, and can only be retrieved in tomcat environment String tomcatPath = (""); (tomcatPath + "/logs/" + name + ".log"); // The text code of the log ("UTF-8"); // true: add false after existing log file: the new log overwrites the previous log (true); // Applicable to current configuration (); // Add new Appender to Logger (appender); return logger; } }
2. In the batch program, each batch is realized through a setting.
Different settings for file names.
definition
<appender name="daily" class="."> <param name="file" value="/opt//batch/log/${}.log" /> <param name="threshold" value="debug"/> <param name="DatePattern" value="yyyyMMdd"/> <param name="append" value="true" /> <layout class="."> <param name="ConversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss,SSS}\t[%-5p]\t(%c:%L)\t%m%n"/> </layout> </appender>
log4j can accept custom environment variables, please note that the following line [${}] is the custom environment variable
<param name="file" value="/opt//batch/log/${}.log" />
So how to set environment variables? There are two ways
(Before using the configuration file, declare these variables in the program):
("", "batch001");
2. Setting in JVM
java -Xmx512M -=batch001 .TestBatch001
3. In the batch program, each batch is realized through multiple settings.
Different settings for file names.
Dynamic configuration file path: (log4j can accept URLs)
URL = ("/com/test/java/log/config/"); (URL);
The above is personal experience. I hope you can give you a reference and I hope you can support me more.