dynamic filename in custom rollingfileappender using log4j -


i'm customizing rollingfileappender (extends fileappender) log files on daily basis , want set dynamic filename using log4j.properties. here part of customrollingfileappender.

public class customrollingfileappender extends fileappender {  public customrollingfileappender() {     } public customrollingfileappender(layout layout, string filename, string datepattern) throws ioexception {         super(layout, filename, true);         this.datepattern = datepattern;         activateoptions();     }     public void activateoptions() {         simpledateformat sdf = new simpledateformat("yyyymmdd");         simpledateformat sd = new simpledateformat("yyyymmddhhmm");         string getcurrentdate = sdf.format(new date(system.currenttimemillis()));         string getcurrentmin = sd.format(new date(system.currenttimemillis()));          string destdir = "/my/dest/path/" + getcurrentdate +"/";           file destpath = new file(destdir);           // 1. make initial directory , create rolling files on it.         if ( !destpath.exists() ) {             destpath.mkdir();         }                 // 2. change last digit 0, 5 minutes.         string resultdate = modifylastdigittozeroorfive( getcurrentmin );          // 3. set rolling file(filename)         super.setfile( destdir+ "abc.002."+resultdate+".log");         // here want set using log4j super.setfile({log4j.appender.r4.file} + resultdate + ".log");         super.activateoptions();           if (datepattern != null && filename != null) {             now.settime(system.currenttimemillis());             sdf = new simpledateformat(datepattern);             int type = computecheckperiod();             printperiodicity(type);             rc.settype(type);             scheduledfilename = filename;              // , main() method or other method             timer timer = new timer();             automatedrollovertrigger t = new automatedrollovertrigger();             t.init(this);             timer.schedule(t, 0, 1000);          } else {             loglog.error("either file or datepattern options not set appender [" + name + "].");         }     } 

first step make initial directory, , change last digit of resultdate log files every 5 minutes (ex. abc.002.201707281200.log, abc.002.201707281205.log ..) after that, set rolling file using super.setfile() need dynamically set log files' prefix abc.001, abc.002, abc.003.. etc. want handle in log4j properties.

here part of log4j.properties.

log4j.appender.r4=com.mypackage.common.customrollingfileappender log4j.appender.r4.file=/my/dest/path/abc.002  <-i've got no idea how use in fileappender source code log4j.appender.r4.datepattern='.'yyyymmddhhmm'.log' log4j.appender.r4.layout=org.apache.log4j.patternlayout 

i'm of newbie in rollingfileappneder , log4j. suggestion welcomed. thanks!


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -