logging - How to generate separate log files for each and every file processed using java log4j? -


i need separate log file each input file processed. have written app using java,log4j,javamail api validate xml.it takes input ini files(to path of xml file validated).once file processed, should generate log file before going next file file name. have trigger error mail each , every ini file if has errors. present status: took xml path ini file name,validated,but generating single log if process multiple file.i need separate log files each , every log files.i attach entire source code this. kindly me how achieve using java log4j

my piece of java code:

static void sendmail() throws ioexception,     messagingexception,addressexception    {           string to1=carparser1.to1;           dateformat dateformat = new simpledateformat("dd/mm/yyyy  hh:mm:ss");           calendar cal = calendar.getinstance();             string to2 = carparser1.to2;             string to3= carparser1.to3;             string to4=carparser1.to4;             string = carparser1.from;             string host = carparser1.host;             properties properties = system.getproperties();             properties.setproperty("mail.smtp.host", host);             session session = session.getdefaultinstance(properties);             mimemessage message = new mimemessage(session);              int m_toterr,m_totwarn;             string getfilepath="";             string pathlogfile = "d:/logfile.log";             enumeration enumeration =  carparser1.logger.getrootlogger().getallappenders();             try {                 m_toterr=validatexml.total_err;                 message.setfrom(new internetaddress(from));                    message.addrecipient(message.recipienttype.to, new  internetaddress(to1));                  message.setsubject("re : car validation report @ :  "+dateformat.format(cal.gettime()));                    while ( enumeration.hasmoreelements() )                  {                        appender appender = (appender)  enumeration.nextelement();                        if ( appender instanceof fileappender )                        {                           pathlogfile  =  ((fileappender)appender).getfile(); //here path                           break;                        }                     }                     stringbuffer sb = new stringbuffer();                     fileinputstream fstream = new   fileinputstream(pathlogfile);                     bufferedreader br = new bufferedreader(new   inputstreamreader(fstream));                      string singleline;                     while ((singleline = br.readline()) != null)                      {    if(singleline.startswith("error")||  singleline.startswith("pls")||  singleline.startswith("total")){                         sb.append(singleline + "<br><br>");                     }                     }                     br.close();                     string alllines = sb.tostring();                   message.setcontent(alllines, "text/html; charset=iso- 8859-1");                  transport.send(message);                    system.out.println("email sent successfully....");                 carparser1.logger.info("email sent successfully...");                 system.out.println();              }              catch (messagingexception mex)              {                 system.out.println("invalid email address.please provide  valid email id send with");                 mex.printstacktrace();               }       } } 

my log4j.properties:

#log console stdout log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout.target=system.out log4j.appender.stdout.layout=org.apache.log4j.patternlayout log4j.appender.stdout.layout.conversionpattern=%d{yyyy-mm-dd  hh:mm:ss}      %-5p %c %3x - %m%n #log file file log4j.appender.file=org.apache.log4j.dailyrollingfileappender log4j.appender.file.file=d:\logfile.log log4j.appender.file.datepattern='.'dd/mm/yyyy hh:mm:ss log4j.appender.file.append=true log4j.appender.file.layout=org.apache.log4j.patternlayout log4j.appender.file.layout.conversionpattern= %d{yyyy-mm-dd hh:mm:ss}    %-5p %c %3x - %m%n # log4j daily rolling log files configuration log4j.rootlogger=debug, rollingappender log4j.appender.rollingappender=org.apache.log4j.dailyrollingfileappender log4j.appender.rollingappender.file=d:\logfile.log log4j.appender.rollingappender.datepattern='.'yyyy-mm-dd  log4j.appender.rollingappender.layout=org.apache.log4j.patternlayout log4j.appender.rollingappender.layout.conversionpattern= %m%n 

please click on below link download entire project docs. https://www.dropbox.com/s/m0sfw35t1obxl7s/project_docs.zip?dl=0

thanks in advance expecting have got dead-lined submit today.

code separate log file:

file dir=new file("d:/newlog");             if(!dir.exists()){             dir.mkdir();}               string filename=carparser1.si_orderid;             file logfile=new file(dir,filename+"_log.txt");             if(!logfile.exists()){             logfile.createnewfile();             }  fileutils.writestringtofile(logfile,carparser1.sb.tostring()); //appending  sb.append("\ncar validating @ path:"+final_filepath+" order id  :"+si_orderid); sb.append("\nunit id : "+si_unitid+" order id: "+si_orderid); 

but getting o/p

java version 1.7 or below supported car validating @ path:\fms\fms_workarea\bdops\opsbank-ii\signals\eflow\car\abpel\january-2016\batch_16.01.16\6668662\itemfile order id :6668662unit id : 2518073 order id: 6668662order id equalunit idequalsupplier id equal

as dump no next line though wrote on code. please guide me results written read , line line out put read.

thanks in advance

how log4j works: log files written file configuration mentioned in log4j.properties/.xml. example : log4j.appender.rollingappender.file=d:\logfile.log

we can configure different level of logging info,debug,error different log file.

for requirement of writing different log files different input files, please following. 1) create stringbuffer object , append information want log 2) create new file object required name , dump stringbuffer object.


Comments