简体   繁体   中英

How to generate unique file name for each run in Log4j

I am using log4j in my project to use the logs generated for failure analysis. I have a problem in creating a new file each time as it is not working. My requirement is to create a file name in the following format Filename_<DDMMYYYY_HHMMSS> each time when I run my script. but I have tried multiple codes from net and nothing works for me. A file is getting generated in the file folder by using the below xml and scripts using the same file for all the run. Kindly help me to generate a new file each time with the xml below

XML Used

 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <!-- For Printing message with date , time & class name also--> <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] %5p[%c{1}]: %m%n"/> <!-- For printing message only <param name="ConversionPattern" value="%-5p[%c{1}]: %m%n"/>--> </layout> </appender> <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false"/> <param name="file" value="MyApp_%d{ddMMyyyy_HHMMSS}_MyApp.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss}%x %-5p[%c{1}]: %m%n"/> </layout> <rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy"> <param name="activeFileName" value="MyApp_%d{ddMMyyyy_HHMMSS}.log"/> <param name="fileNamePattern" value="MyApp_%d{ddMMyyyy_HHMMSS}_MyApp.log"/> <param name="minIndex" value="0"/> <param name="maxIndex" value="5"/> </rollingPolicy> </appender> <root> <level value="INFO"/> <appender-ref ref="consoleAppender"/> <appender-ref ref="fileAppender"/> </root> </log4j:configuration> 

Error

log4j:WARN Continuable parsing error 25 and column 12

log4j:WARN The content of element type "appender" must match "(errorHandler?,param*,rollingPolicy?,triggeringPolicy?,connectionSource?,layout?,filter*,appender-ref*)".

log4j:WARN Unrecognized element rollingPolicy

Sorry for attaching the xml in HTML.

We can do this by number of ways. But the one which I found is easy to do with the setup in XML. We need to set the variable value by the line below in script,

DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Date date = new Date();
System.out.println(dateFormat.format(date));   
String dynamicFileName="Logs_"+dateFormat.format(date);
System.setProperty("logfilename", dynamicFileName);

With the above code we will be able to setup a dynamic value for the variable logfilename in the xml. So when the script creates the object it will create a new file name with the below lines,

DOMConfigurator.configure("log4j.xml");
Logger log = Logger.getLogger(Area.class.getName());
Log.error("mulla");

Modified XML Used

  <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <!-- For Printing message with date , time & class name also--> <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss}] %5p[%c{1}]: %m%n"/> <!-- For printing message only <param name="ConversionPattern" value="%-5p[%c{1}]: %m%n"/>--> </layout> </appender> <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="append" value="false"/> <param name="file" value="${logfilename}.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{dd-MM-yyyy HH:mm:ss}%x %-5p[%c{1}]: %m%n"/> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="consoleAppender"/> <appender-ref ref="fileAppender"/> </root> </log4j:configuration> 

So this code will generate a new file name for each run... Thanks all for the effort to make this code....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM