Writing to a file using log4j

Hi ,
I am facing an issue in rolling out file on an hourly basis. I have a source file , say usagelog_date.log which records logging, then after an hour an hour I have to separate that file , give it a different name say usagelog_date.10.log , I copy the contents of the source file into the rolling file, and make that source file empty. But after making that file empty when I am writing to the file using some threads simultaneously, I get some blank characters first and only after that the logging starts, that increases the file size to a large extent.
My java code which does this separation is as follows.
package com.proquest.services.usage.helper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.nio.channels.FileLock;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UsageRotator {
     private Log scpLog = LogFactory.getLog("SCPLog");
     public String rotateAndReturnFilename(String rotationdate, String location) throws Exception {
          Calendar calendar = Calendar.getInstance();
          int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
          String rotationalTime = ((hour_of_day < 10) == true) ? "0" + hour_of_day : "" + hour_of_day;
          String rotatedFileName = null;
          File usageLoggingLogFile = new File(location + "usagelogging." + rotationdate + ".log");
          if (usageLoggingLogFile != null && usageLoggingLogFile.exists()) {
               File usageLoggingRotatedFile = new File(location + "usagelogging." + rotationdate + "." + rotationalTime + ".log");
               copyFile(usageLoggingLogFile, usageLoggingRotatedFile);
//copying the contents of source file to a rolling file
               FileOutputStream fout = new FileOutputStream();
// making the source file empty fout.flush();               
               usageLoggingLogFile.setWritable(true);
               boolean isEmpty = isFileEmpty(usageLoggingLogFile);
               if(isEmpty)     
                    scpLog.info("Existing file has been empty so it's good to proceed for looging next occurances");
               rotatedFileName = usageLoggingRotatedFile.getName();
               scpLog.info("Log file has been rotated to "+rotatedFileName);
          } else {
               throw new Exception("File rotation failed : UsageLogging file couldn't be found for the supplied date");
          return rotatedFileName;
     private void copyFile(File source, File destn) throws Exception {
          FileInputStream fis = new FileInputStream(source);
     FileOutputStream fos = new FileOutputStream(destn);
     try {
     byte[] buf = new byte[1024];
     int i = 0;
     while ((i = fis.read(buf)) != -1) {
     fos.write(buf, 0, i);
     catch (FileNotFoundException e) {
     throw new Exception("[ERROR] File copy failed");
     finally {
     if (fis != null) fis.close();
     if (fos != null) fos.close();
     private boolean isFileEmpty(File file) throws Exception {
          FileReader fr = new FileReader(file);
          BufferedReader br = new BufferedReader(fr);
          boolean isEmpty = true;
          while (br.readLine() != null) {
               isEmpty = false;
          return isEmpty;
And my log4j file with which I am writing is
# Default is to send information messages and above to the console
log4j.rootLogger = DEBUG, DailyLogFileAppender
# Logger configurations
#log4j.logger.com.proquest.services.usage=DEBUG, DailyLogFileAppender
log4j.logger.com.proquest.services.UsageLog=INFO, UsageLogFileAppender
# Appender configurations
# Define an appender which writes to a file which is rolled over daily
log4j.appender.DailyLogFileAppender = com.proquest.services.log.PqDailyRollingFileAppenderExt
log4j.appender.DailyLogFileAppender.File = logs/usagelogging/usagelogging-error.log
log4j.appender.DailyLogFileAppender.DatePattern = '.'yyyy-MM-dd
log4j.appender.DailyLogFileAppender.Append = true
log4j.appender.DailyLogFileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.DailyLogFileAppender.layout.ConversionPattern=%d{ISO8601} %m%n
log4j.additivity.com.proquest.services.usage = false
log4j.additivity.com.proquest.services.UsageLog = false
log4j.appender.UsageLogFileAppender = com.proquest.services.log.PqDailyRollingFileAppenderExt
log4j.appender.UsageLogFileAppender.File = logs/usagelogging/usagelogging.log
log4j.appender.UsageLogFileAppender.DatePattern = '.'yyyy-MM-dd
log4j.appender.UsageLogFileAppender.Append = true
log4j.appender.UsageLogFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.UsageLogFileAppender.layout.ConversionPattern=%d{ISO8601} %m%n
Can somebody please suggest me what to do, as I have been badly deadlocked into the problem.
Thanks
Suman

neoghy wrote:
... rolling out file on an hourly basis.
And my log4j file with which I am writing is
#  Default is to send information messages and above to the console
log4j.rootLogger = DEBUG, DailyLogFileAppender
# Logger configurations
#log4j.logger.com.proquest.services.usage=DEBUG, DailyLogFileAppender
log4j.logger.com.proquest.services.UsageLog=INFO, UsageLogFileAppender
# Appender configurations
#  Define an appender which writes to a file which is rolled over daily
log4j.appender.DailyLogFileAppender = com.proquest.services.log.PqDailyRollingFileAppenderExt
log4j.appender.DailyLogFileAppender.File = logs/usagelogging/usagelogging-error.log
log4j.appender.DailyLogFileAppender.DatePattern = '.'yyyy-MM-dd
log4j.appender.DailyLogFileAppender.Append = true
log4j.appender.DailyLogFileAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.DailyLogFileAppender.layout.ConversionPattern=%d{ISO8601} %m%n
log4j.additivity.com.proquest.services.usage = false
log4j.additivity.com.proquest.services.UsageLog = false
log4j.appender.UsageLogFileAppender = com.proquest.services.log.PqDailyRollingFileAppenderExt
log4j.appender.UsageLogFileAppender.File = logs/usagelogging/usagelogging.log
log4j.appender.UsageLogFileAppender.DatePattern = '.'yyyy-MM-dd
log4j.appender.UsageLogFileAppender.Append = true
log4j.appender.UsageLogFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.UsageLogFileAppender.layout.ConversionPattern=%d{ISO8601} %m%nCan somebody please suggest me what to do, as I have been badly deadlocked into the problem.I assume you mean [DailyRollingFileAppender ^apache^|http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html]
log4j.appender.DailyRollingFileAppender.DatePattern = '.'yyyy-MM-dd-HH

Similar Messages

  • Writing into Excel file using PL/SQL and formatting the excel file

    Hi,
    I am writing into a excel file using PL/SQL and I want to make the first line bold on the excel. Also let me know if there are any other formatting options when writing into excel.
    Regards,
    -Anand

    I am writing into a excel file using PL/SQL
    Re: CSV into Oracle and Oracle into CSV
    check that thread or search in this forum...

  • Multiple log files using Log4j

    Hello,
    I want to generate log files based on package structure. Like com.temp.test in test.log ,also I am having a log file at application like app.log .
    This is my requirement what has been logged in test.log should not be logged in app.log.This is my log4j.properties file.
    # Log4j configuration file.
    # Available levels are DEBUG, INFO, WARN, ERROR, FATAL
    # Default logger
    log4j.rootLogger=DEBUG, PFILE
    log4j.logger.com.temp.test=DEBUG,TEST
    # PFILE is the primary log file
    log4j.appender.PFILE=org.apache.log4j.RollingFileAppender
    log4j.appender.PFILE.File=./App.log
    log4j.appender.PFILE.MaxFileSize=5120KB
    log4j.appender.PFILE.MaxBackupIndex=10
    #log4j.appender.PFILE.Threshold=DEBUG
    log4j.appender.PFILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.PFILE.layout.ConversionPattern=%p %d[%l][%C] %m%n
    #log4j.appender.PFILE.layout.ConversionPattern=%p %d %m%n
    log4j.appender.TEST=org.apache.log4j.RollingFileAppender
    log4j.appender.TEST.File=./test.log
    log4j.appender.TEST.MaxFileSize=5120KB
    log4j.appender.TEST.MaxBackupIndex=10
    log4j.appender.TEST.layout=org.apache.log4j.PatternLayout
    log4j.appender.TEST.layout.ConversionPattern=%p %d[%l][%C] %m%n
    Can u help me!!!

    You have to configure the temp logger so that it does not send its info on to the root logger.
    For this, you can use the additivity flag.
    # Default logger
    log4j.rootLogger=DEBUG, PFILE
    log4j.additivity.com.temp.test=false
    log4j.logger.com.temp.test=DEBUG,TESTThe rest of the file remains the same.

  • Reading and Writing large Excel file using JExcel API

    hi,
    I am using JExcelAPI for reading and writing excel file. My problem is when I read file with 10000 records and 95 columns (file size about 14MB), I got out of memory error and application is crashed. Can anyone tell me is there any way that I can read large file using JExcelAPI throug streams or in any other way. Jakarta POI is also showing this behaviour.
    Thanks and advance

    Sorry when out of memory error is occurred no stack trace is printed as application is crashed. But I will quote some lines taken from JProfiler where this problem is occurred:
              reader = new FileInputStream(new File(filePath));
              workbook = Workbook.getWorkbook(reader);
              *sheeet = workbook.getSheet(0);* // here out of memory error is occured
               JProfiler tree:
    jxl.Workbook.getWorkBook
          jxl.read.biff.File 
                 jxl.read.biff.CompoundFile.getStream
                       jxl.read.biff.CompoundFile.getBigBlockStream Thanks

  • Writing data into files using VHDL Textio

    Hi 
    I was trying to write nos. from 1 to 8 into a text file using the below program.
    process
    type IntegerFileType is file of integer;
    file data_out: IntegerFileType ;
    variable fstatus: FILE_OPEN_STATUS;
    variable coun: natural:= 1;
    begin
    file_open(fstatus,data_out,"myfile.txt",write_mode);
    for i in 1 to 8 loop
    write(data_out, coun);
    coun := coun + 1;
    end loop;
    file_close(data_out);
    wait; -- an artificial way to stop the process
    end process;
    But getting the below attached result..
    Can you please help me out what could be wrong with the program.
    Thanks & regards
    Madhur

    Do you want the numbers in the file to be human readable ASCII?
    Then you'll need to convert your coun to a string. 
    declare another variable of type line (type access to string).
    do a write() to the line, then a writeline() to the file.
    natural'image(coun) will convert coun to a string.
    Google should help you find example code that will help.

  • Writing to a file using an applet?

    I'm trying to write to a .txt file using an applet. When I run the applet from JBuilder everything works perfectly, but when I run it from internet explorer I don't seem to be able to read or write from and to the file... Anyone has an idea? Is it possible to do that?

    Applets run on restricted security priveelege. Unless you sign your applet, you cant access the files from the applet.
    Go through this.. This might help you
    http://developer.java.sun.com/developer/technicalArticles/Security/Signed/

  • Writing to a file using labview

    hello,
    so i wrote this vi to write some data to a text file and that part works correctly.
    the problem i'm having is that, it doesn't append to the file. so for each data point that it writes it requires a new file.
    in otherwords, if i need to write 10 data points the program prompts each time for 10 different file names.
    how do i resolve this so that the program will write all the 10 data points to one file, by appending each time.
    e.g. data points: 29.5, 34.2, 21.34, 543.2 ... etc
    i want something like this:
    29.5
    34.2
    21.34
    543.2
    etc
    thanks
    -r

    If the path is correctly wired, it should not prompt you for another file. Make sure you wire the path to a shift register so it is available the next time the "write charaters ..." is called.
    You can do the "exception" in many ways. Some examples:
    --Check if the file exists, and if so, append.
    --Use a shift register initialized by "false", then wire it to the append terminal. Feed a "true" to the shift register on the right.
    However, you should consider using some of the lower level file I/O and open the file only once, then keep writing data and close it only at the end. The high-level "write characters to file" would need to do a lot of extra work because whenever it is called it opens the file, writes/appends data, then
    closes the file again.
    LabVIEW Champion . Do more with less code and in less time .

  • Problem writing to excel file using report generation toolkit

    hello everyone, i have this report generation toolkit... and i want to output DAQmx Analog I/P data on to an excel sheet. the DAQmx is programmed to collect 
    data at 3samples/sec. however, when i see the excel file that Report Generation Toolkit generates, the time stamp is updated every second instead of every 0.33sec. 
    can anyone please help me?  i am using the MS Office Report Express VI. 
    Now on LabVIEW 10.0 on Win7

    @All, I got rid of the express VI, decided to work on the custom low level VIs instead. however, i have a new problem now... 
    I have a case statement wherein, the user selects if he wants to start generating a report. once the program enters tat loop, the program speed reduces! 
    can anyone please tell me why is it happening? i ahve attached the vi... also another question.. in this VI, i am capturing the unwanted data into the graph as I am indexin the graph input. how can i make a logic 
    that the graph captures the data only when I am switching the CREATE REPORT button (which is in the while loop). is there a way that I can append the data to the graph without creating a new graph every iteration? please let me know
    thanks
    Now on LabVIEW 10.0 on Win7
    Attachments:
    Untitled 7.vi ‏75 KB
    Untitled 7.JPG ‏99 KB

  • Writing an XML file using a Servlet

    Hello, I'm trying to code a servlet that receives a POST from a HTTP and output its data to a XML file, the problem is that I get the following error:
    The XML page cannot be displayed
    Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
    XML document must have a top level element. Error processing resource 'http://localhost:8080/XMLSender/xmlsend'.
    I don't know what happens, because I'm NOT trying to show the content, just to save it, I post my code here so anyone can help me, please. Thanks in advance.
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class xmlsender extends HttpServlet
    public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    ServletOutputStream salida = res.getOutputStream();
    res.setContentType("text/xml");
    String cadenanumero = req.getParameter("numero");
    String cadenaoperadora = req.getParameter("operadora");
    String cadenabody = req.getParameter("mensaje");
    String cadenashortcode = req.getParameter("shortcode");
    File f1 = new File("salida.xml");
    FileWriter writer = new FileWriter(f1);
    writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    writer.write("<root>");
    writer.write("<tlf>" + cadenanumero + "</tlf>");
    writer.write("<op>" + cadenaoperadora + "</op>");
    writer.write("<sc>" + cadenashortcode + "</sc>");
    writer.write("<body>" + cadenabody + "</body>");
    writer.write("</root>");
    writer.close();
    }

    Yes, in fact what I want is the file to be in the server, now, I modificated my code to the following:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class xmlsender extends HttpServlet
    public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    ServletOutputStream salida = res.getOutputStream();
    res.setContentType("text/HTML");
    String cadenanumero = req.getParameter("numero");
    String cadenaoperadora = req.getParameter("operadora");
    String cadenabody = req.getParameter("mensaje");
    String cadenashortcode = req.getParameter("shortcode");
    File f1 = new File ("salida.xml");
    FileWriter writer = new FileWriter(f1);
    /*salida.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    salida.println("<root>");
    salida.println("<tlf>" + cadenanumero + "</tlf>");
    salida.println("<op>" + cadenaoperadora + "</op>");
    salida.println("<sc>" + cadenashortcode + "</sc>");
    salida.println("<body>" + cadenabody + "</body>");
    salida.println("</root>"); */
    salida.println("Finalizado");
    f1.createNewFile();
    writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    writer.write("<root>");
    writer.write("<tlf>" + cadenanumero + "</tlf>");
    writer.write("<op>" + cadenaoperadora + "</op>");
    writer.write("<sc>" + cadenashortcode + "</sc>");
    writer.write("<body>" + cadenabody + "</body>");
    writer.write("</root>");
    writer.close();
    It still do not create my file "salida.xml", still don't know why. Any help is welcome.

  • Writing to remote files using an applet.

    I programmed a basic game as an applet for a web site, but it really needs some kind of high score functionality.
    I plan to store the high scores in a file in the server. Reading information from the file is no problem at all, but updating the file with new highscores runs into security problems. Obviously I can't simple use a simle file writer to do that. What is the proper way to do this?
    One possibility which came to my mind is to make the applet open a ftp connection to the server and upload the new highscores file to the server. but that would mean hardcoding my password into the applet code.
    Please keep in mind using servlets or any other programs running on the server is out of question.
    Any help greatly appreciated,thanks in advance.

    Why not host this applet on another free server which supports mysql/postgresql/etc and store it in a database? Any hosts/tools needed, just ask - there are several good freeware.
    /DanX

  • Sending log data to two different files using log4j

    Hi,
    Can some one please help me with my problem I have here?
    I want to send log data to two diffrent files depending on the logging level such as DEBUG and WARN.
    How can you configure this in log4j.properties.
    Please post sample code for log4j.properties to achieve this.
    Thanks in advance.
    rsreddych

    Hi,
    Finally, I found the solution to this problem my self.
    What you need to do is define two loggers in the application, and set two priority levels to these loggers and define two out put files to these loggers. Deploy the war file, restart application server and you are good to go.
    This seems to be working for me. Only glitch I found is, the out put in
    the second file is displaying one space character at the start of line starting from second line (First line don't have this problem). This is odd. It may be because of my faulty code. Any how thanks for you all.
    rsreddych

  • Writing Objects to file using Externalizable

    Hi,
    I'm trying to write an object to file. My sample code is:
    public class Junk implements Externalizable{
    private static java.util.Random generator = new java.util.Random();
    private int answer;
    private double[] numbers;
    private String thought;
    public Junk(String thought) {
    this.thought = thought;
    answer = 42;
    numbers = new double[3+ generator.nextInt(4)];
    for (int i=0; i<numbers.length; i++) {
    numbers[i] = generator.nextDouble();
    public void writeExternal(ObjectOutput stream) throws java.io.IOException {
    stream.writeInt(answer);
    stream.writeBytes(thought);
    for(int i=0; i< numbers.length; i++) {
    stream.writeDouble(numbers);
    public void readExternal(ObjectInput stream) throws java.io.IOException {
    answer = stream.readInt();
    String thought = stream.readUTF();
    and the class with main() is:
    package MyTest;
    import java.io.*;
    public class SerializeObjects {
    public SerializeObjects() {
    public static void main(String args[]) {
    Junk obj1 = new Junk("A green twig is easily bent.");
    Junk obj2 = new Junk("A little knowledge is a dangerous thing.");
    Junk obj3 = new Junk("Flies light on lean horses.");
    ObjectOutputStream oOut = null;
    FileOutputStream fOut = null;
    try {
    fOut = new FileOutputStream("E:\\FileTest\\test.bin");
    oOut = new ObjectOutputStream(fOut);
    obj1.writeExternal(oOut);
    //obj2.writeExternal(oOut);
    } catch (IOException e) {
    e.printStackTrace(System.err);
    System.exit(1);
    try {
    oOut.flush();
    oOut.close();
    fOut.close();
    } catch(IOException e) {
    e.printStackTrace(System.err);
    System.exit(1);
    The output I get in test.bin contains some junk ascii codes. The only item that is written correctly in the file is the string.
    Is there anyway I can write correct data into a file?
    My output needs to be a readable text format file.
    Can anyone help please?

    obj1.writeExternal(oOut);This should be
    oOut.writeObject(obj1);However,
    The output I get in test.bin contains some junk ascii
    codes. The only item that is written correctly in the
    file is the string.If you don't want 'junk' don't use Externalizable and ObjectOutputStream at all, just use PrintStream/PrintWriter.println().

  • Writing to a file using url

    Hi all,
    I have build an applet which I am going to put in a web. Right now I am working locally and I'm able to read file's using this code:
    url = new URL("http://localhost/file.txt");
                is = url.openStream();
                BufferedReader bufRdr  = new BufferedReader(new InputStreamReader(is));
                while((line = bufRdr.readLine()) != null)
                  //do things
                bufRdr.close();
                is.close();
            }Now I would like to be able to write in this file. How could I do it?
    Thanks!

    Using HTTP to open a local file will only work if you have a web server running locally, and if that server happens to know how to find and deal with that file.
    If you want to use that same web server to write files locally, then your local web server will need to have functionality to write files.
    So your first issue is dealing with that. How you do this will depend on what web server you're currently running.
    Using a web server might not be the best approach, by the way.

  • Writing to a file using DataOutputStream

    the goal is to write an object named cd1 to a file data.dat using a method writeData.
    writeData is defined as:
    void writeData(DataOutputStream out), which writes an object, field y field to a DataOutputStream. (plus some other blah blah).
    so here's what I have so far (only pieces of code are posted)
    File dataFile = new File(ioDir, "data.dat");
                   dataFile.createNewFile();
                   DataOutputStream out1 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
                   cd1.writeData(out1);
         public void writeData(DataOutputStream out) {
              iWritten = true;
              try {
                   out.writeInt(iNumTracks);
                   out.writeBoolean(iWritten);
                   out.close();
              } catch (IOException xcp) {
                   System.out.println(xcp);
         }but when I open the file data.dat it seems to be totally empty, doesn't seem like anything gets written to it.

    have to check the file size ?
    the data you write to the file maybe not displayable on screen. example, all character/byte < 32

  • Writing a text file using Opaque Schema??

    Hi all
    Is it possible to use a File Adapter with Opaque Schema to create a text file (.txt)??
    I have a variable of type base64Binary but it´s contents is, in fact, text and I want to write it´s contents to a plan text file.
    I have tried this using Opaque Schema but the result is an empty file. How can I create a plan text file?

    There are many libraries for converting between base64encoded and decoded strings in java. The oracle library that supports this should already be in your classpath.
    <bpelx:exec import="com.collaxa.common.util.Base64Encoder"/>
    byte[] outputBytes = decoded.getBytes();
    String reencoded = Base64Encoder.encode(outputBytes);
    setVariableData("Var_Outbound_File", reencoded);
    Thanks,
    Sean.

Maybe you are looking for

  • ABAP-HR helps

    Dear Friends, I have experience in abap development for MM and SD module, now i need to do for HR module also, when i was looking into table its totally different all are in the form of clusters. I dont know how and when the data will be loaded in th

  • Please give me advices when to use black and white sliders in LR4.

    Please give me advices when to use black and white sliders in LR4.

  • Doing FTP using a Dynamic Port

    Hi, I am having an odx with a Receive Shape, expression and Send shape. The send shape is connected to a dynamic port: There is a message var called ShowPartMessage. There is no transformation. This message needs to be received and sent via FTP. In t

  • Download of Discoverer doumentation fails

    I get a "This page cannot be displayed" message when I click on any of the links on http://otn.oracle.com/documentation/discoverer.html Thanks Ram

  • Grant privileges  to new user + few tables

    Hi all. How can I give my new user all privileges for inserting deleting records in some tables... CREATE USER user1 IDENTIFIED BY user1