writing to a file using log4j
807580Apr 14 2010 — edited Apr 16 2010Hi ,
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