WSO2 Log4J RollingFileAppendeder does not work in wso2esb-4.8.1 -
we using wso2esb-4.8.1.
default log4j properties uses
log4j.appender.carbon_logfile=org.wso2.carbon.logging.appenders.carbondailyrollingfileappender
i want size based rolling file. per documentation @ https://docs.wso2.com/display/carbon420/managing+logs, following should trick.
##comment following ###log4j.appender.carbon_logfile=org.wso2.carbon.logging.appenders.carbondailyrollingfileappender ##add followng log4j.appender.carbon_logfile=org.apache.log4j.rollingfileappender log4j.appender.carbon_logfile.maxfilesize=10mb log4j.appender.carbon_logfile.maxbackupindex=20
but after these changes, logs rotating @ 10mb, 1 file maintained.
is known issue in wso2 esb 4.8.1 ?
this working cleanly in wso2 esb 4.9.0.
however, not have option upgrade since of other features need broken there. simulated log rotation behavior in wso2 task. refer https://docs.wso2.com/display/esb481/writing+tasks+sample understand how write sample wso2 task.
this code
import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.nio.channels.filechannel; import java.util.arraylist; import java.util.arrays; import java.util.collections; import java.util.date; import java.util.list; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import org.apache.synapse.managedlifecycle; import org.apache.synapse.core.synapseenvironment; import org.apache.synapse.task.task; public class logfilebackuptasksample implements task, managedlifecycle { private static final log log = logfactory.getlog(logfilebackuptask.class); private synapseenvironment synapseenvironment; // default, program assumed run wso2 home folder public static final string default_log_file_location ="repository/logs/"; public static final string wso2_log_file_name ="wso2carbon.log"; // if value not provided logfilesizeinmbstr, backup of log file size happen after reaches 100 mb public static final int default_log_file_size_in_mb =100; // if value not provided noofbackupfiles, 20 files backed up. public static final int default_backup_files_number =20; public static final int mb_to_bytes=1024 *1000; private string loglocation; private int logfilesizeinmb; private string logfilesizeinmbstr; private string noofbackupfiles; private int noofbackupfilesint; public void execute() { filechannel sourcechannel = null; filechannel outchannel = null; if(loglocation==null || loglocation.trim().equals("")) { if(log.isinfoenabled()) { log.info("no log location provided. therefore using default location of "+default_log_file_location); } loglocation= default_log_file_location; } if(!loglocation.endswith("/") && !loglocation.endswith("\\")) { loglocation= loglocation+file.separator; } if(logfilesizeinmb==0) { if(log.isinfoenabled()) { log.info("no log file size in mb. therefore using default size of "+default_log_file_size_in_mb+" mb"); } logfilesizeinmb= default_log_file_size_in_mb * mb_to_bytes; } string inputlogfilenamewithdirectory= loglocation+wso2_log_file_name; file inputlogfilewithdirectory = new file(inputlogfilenamewithdirectory); long currentlogsize=0; boolean fileswapped= false; try { currentlogsize = inputlogfilewithdirectory.length(); if(currentlogsize> logfilesizeinmb) { long currentdatelong = system.currenttimemillis(); date date = new date(currentdatelong); string outfilename= wso2_log_file_name+"."+date.tostring().replace(' ', '_').replace(':', '_'); sourcechannel = new fileinputstream(inputlogfilewithdirectory).getchannel(); file outfile = new file (loglocation+outfilename); outfile.createnewfile(); outchannel = new fileoutputstream(outfile).getchannel(); outchannel.transferfrom(sourcechannel, 0, currentlogsize); fileswapped= true; } } catch(ioexception e) { log.error(e.tostring(),e); throw new runtimeexception(e); } { if(sourcechannel!=null) { try { sourcechannel.close(); } catch (ioexception e) { // ignored log.error(e.tostring(),e); } } if(outchannel!=null) { try { outchannel.close(); } catch (ioexception e) { // ignored log.error(e.tostring(),e); } } } filechannel sourcetruncatechannel = null; try { if(fileswapped) { sourcetruncatechannel = new fileoutputstream(inputlogfilewithdirectory).getchannel(); sourcetruncatechannel.truncate(currentlogsize); } } catch(ioexception e) { log.error(e.tostring(),e); throw new runtimeexception(e); } { if(sourcetruncatechannel!=null) { try { sourcetruncatechannel.close(); } catch (ioexception e) { // ignored log.error(e.tostring(),e); } } } if(fileswapped) { deletingoldfiles(); } } public void deletingoldfiles() { if(noofbackupfilesint==0) { if(log.isinfoenabled()) { log.info("noofbackupfiles 0. using default number of "+default_backup_files_number); } noofbackupfilesint= default_backup_files_number; } file[] listoffiles= new file(loglocation).listfiles(); list <timestampwisefile> listofwso2files = new arraylist<timestampwisefile>(); (int = 0; < listoffiles.length; i++) { if(listoffiles[i].getname().startswith(wso2_log_file_name) && !listoffiles[i].getname().equals(wso2_log_file_name)) { listofwso2files.add(new timestampwisefile(loglocation, listoffiles[i].getname())); } } // no files delete in case. if(listofwso2files.size()<=noofbackupfilesint) { return; } timestampwisefile[] listofwso2filesarray = new timestampwisefile[listofwso2files.size()]; listofwso2filesarray= listofwso2files.toarray(listofwso2filesarray); // need in descending order old files arranged @ bottom of stack. arrays.sort(listofwso2filesarray, collections.reverseorder()); int index=0; (int = 0; < listofwso2filesarray.length; i++) { timestampwisefile timestampwisefile = listofwso2filesarray[i]; if(++index > noofbackupfilesint) { string filename = timestampwisefile.getname(); timestampwisefile.delete(); if(log.isinfoenabled()) { log.info("removed file "+filename); } } } } public void destroy() { } public void init(synapseenvironment synapseenvironment) { this.synapseenvironment = synapseenvironment; } public string getloglocation() { return loglocation; } public void setloglocation(string loglocation) { this.loglocation = loglocation; if(loglocation== null || loglocation.trim().equals("")) { if(log.isinfoenabled()) { log.info("no log location provided. therefore using default location of "+default_log_file_location); } loglocation= default_log_file_location; } } public string getlogfilesizeinmbstr() { return logfilesizeinmbstr; } public void setlogfilesizeinmbstr(string logfilesizeinmbstr) { this.logfilesizeinmbstr = logfilesizeinmbstr; if(logfilesizeinmbstr== null || logfilesizeinmbstr.trim().equals("")) { if(log.isinfoenabled()) { log.info("no log file size in mb. therefore using default size of "+default_log_file_size_in_mb+" mb"); } logfilesizeinmb= default_log_file_size_in_mb * mb_to_bytes; } else { try { logfilesizeinmb= integer.parseint(logfilesizeinmbstr) * mb_to_bytes; } catch(numberformatexception e) { log.error("logfilesizeinmb not proper. if size 20mb, provide 20 2nd argument. due exception" + "using default size of "+default_log_file_size_in_mb+" mb"); logfilesizeinmb= default_log_file_size_in_mb * mb_to_bytes; } } } public string getnoofbackupfiles() { return noofbackupfiles; } public void setnoofbackupfiles(string noofbackupfiles) { this.noofbackupfiles = noofbackupfiles; try { noofbackupfilesint= integer.parseint(noofbackupfiles) ; } catch(numberformatexception e) { log.error("noofbackupfiles not proper. proper proper integer value. due exception" + "using default number of "+default_backup_files_number); noofbackupfilesint= default_backup_files_number; } } }
now add scheduled in master tenant. refer https://docs.wso2.com/display/esb480/adding+and+scheduling+tasks details on this.
<?xml version="1.0" encoding="utf-8"?> <task xmlns="http://ws.apache.org/ns/synapse" name="logfilebackuptask" class="logfilebackuptask" group="synapse.simple.quartz"> <trigger cron="0 0/1 * * * ?"/> <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="noofbackupfiles" value="20"/> <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="logfilesizeinmbstr" value="20"/> <property xmlns:task="http://www.wso2.org/products/wso2commons/tasks" name="loglocation" value="repository/logs/"/> </task>
Comments
Post a Comment