Goal: Dynamically modify the log location according to the parameters passed to main() when starting the jar
1. Modify the startup item
MainMapLookup.setMainArguments(args);
Note: Do not initialize the log before the lookup is set (eg: private static final Logger log = LoggerFactory.getLogger(HttpServer.class); )
1 public static void main(String[] args) throws InterruptedException, ServletException { 2 // Set the log folder 3 MainMapLookup.setMainArguments(args); 4 5 // Read the configuration file and start 6 try { 7 String confPathName = args[0 ]; 8 // Read the configuration file outside the jar 9 InputStream in = new BufferedInputStream( new FileInputStream(confPathName+ "/conf.properties" )); 10 config.load(in); 11 }catch (Exception e) { 12 e.printStackTrace(); 13 } 14 new HttpServer().start(); 15 }
2. Set log4j2.xml
${main:0}
The lo4j2.xml code is as follows
1 <? xml version="1.0" encoding="UTF-8" ?> 2 3 <!-- 4 status : This is used to set the internal information output of log4j2 itself. Various detailed outputs inside log4j2 5 monitorInterval : Log4j can automatically detect modifying the configuration file and reconfigure itself, setting the interval in seconds. 6 --> 7 < Configuration status ="DEBUG" monitorInterval ="6000" > 8 9 < Properties > 10 <!-- Configuration log file output directory --> 11 < Property name ="LOG_HOME" value ="log/log_ ${main:0}" Property name ="LOG_NAME" > http </ Property > 13 </ Properties > 14 15 < Appenders > 16 17 <!-- Configuration of this output console --> 18 < Console name ="Console" target ="SYSTEM_OUT" > 19 <!-- The console only outputs information of level and above (onMatch), other directly rejected (onMismatch) --> 20 < ThresholdFilter level = "trace" onMatch = "ACCEPT" onMismatch = "DENY"/> 21 <!-- 输出日志的格式 --> 22 <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/> 23 </Console> 24 25 <!-- DEBUG日志格式 --> 26 <RollingFile name="service_debug_appender" fileName="${LOG_HOME}/${LOG_NAME}.log" 27 filePattern="${LOG_HOME}/${LOG_NAME}.log.%d{yyyy-MM-dd}-%i" append="true" immediateFlush="true"> 28 <Filters> 29 < ThresholdFilter level ="trace" onMatch ="ACCEPT" onMismatch ="DENY" /> 30 </ Filters > 31 <!-- 32 %d{yyyy-MM-dd HH:mm:ss, SSS} : log production time 33 %p : log output format 34 %c : logger name 35 %m : log content, that is, logger.info("message") 36 %n : newline 37 %C : Java class name38 %L : where the log output is located Line number 39 %M : Method name where log output is located 40 hostName : local machine name 41 hostAddress : 本地ip地址 42 --> 43 <PatternLayout> 44 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M -- %msg%xEx%n</pattern> 45 </PatternLayout> 46 <Policies> 47 <TimeBasedTriggeringPolicy interval="1" modulate="true"/> 48 <SizeBasedTriggeringPolicy size="500 MB"/> 49 </Policies> 50 </RollingFile> 51 52 53 <!-- ERROR日志格式 --> 54 <RollingFile name="service_error_appender" fileName="${LOG_HOME}/${LOG_NAME}.error" 55 filePattern="${LOG_HOME}/${LOG_NAME}.error.%d{yyyy-MM-dd}-%i" append="true" immediateFlush="true"> 56 <Filters> 57 <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> 58 </Filters> 59 <PatternLayout> 60 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M -- %msg%xEx%n</pattern> 61 </PatternLayout> 62 <Policies> 63 <TimeBasedTriggeringPolicy interval="1" modulate="true"/> 64 <SizeBasedTriggeringPolicy size="500 MB"/> 65 </Policies> 66 </RollingFile> 67 </Appenders > 68 69 < Loggers > 70 <!-- The root node of the configuration log --> 71 < Root level = "debug" > 72 < appender-ref ref = "Console" /> 73 < appender-ref ref = "service_debug_appender" " /> 74 < appender-ref ref ="service_error_appender" /> 75 </ Root > 76 77 <!-- Third Party Logging System --> 78 <logger name="org.springframework" level="INFO"/> 79 <logger name="io.netty" level="warn"/> 80 <logger name="org.apache.http" level="warn"/> 81 <logger name="org.mongodb.driver" level="INFO"/> 82 <logger name="org.jboss.netty" level="warn"/> 83 <logger name="org.springframework.data.redis" level="INFO"/> 84 85 </Loggers> 86 87 </Configuration>
3. Results
The log_${main:0} folder is generated under the log folder, and the system logs are placed under it