at.spardat.xma.boot.logger.LogHandlerFile Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
/*
* Created on : 25.06.2003
* Created by : s3595
*/
package at.spardat.xma.boot.logger;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;
import at.spardat.xma.boot.Statics;
/**
* LogHandlerDefault
*
* @author s3595,(CGS)
* @version $Id: LogHandlerFile.java 2084 2007-11-27 14:53:31Z s3460 $
*
*/
public class LogHandlerFile extends LogHandlerBase implements ILogHandler {
private String lineSeparator;
private File logfile;
protected LogHandlerFile() {
lineSeparator = System.getProperty( "line.separator");
}
public void setProperties( Properties in ) {
super.setProperties(in);
String strLogDir = (String)props.get( Statics.CFG_PROP_LOGDIRECTORY );
if(strLogDir==null)
return;
File logParrent = new File( strLogDir );
if(!logParrent.exists()){
logParrent.mkdirs();
}
File logdir = new File( logParrent, "/logs");
if(!logdir.exists()){
logdir.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat( "ddMMMyyyy", Locale.getDefault() ); //$NON-NLS-1$
String strDate = sdf.format( new Date( System.currentTimeMillis()) );
logfile = new File ( logdir, strDate + ".log");
synchronized(logfile) {
if(!logfile.exists()) {
try {
if( logfile.createNewFile() ) { }
} catch (IOException e) {
System.err.println("error creating logfile: " + logfile.toString() );
logfile = null;
return;
}
}
if(logfile.exists()) {
SimpleDateFormat sdf_file = new SimpleDateFormat( "EEE, dd MMM yyyy hh:mm:ss zzz", Locale.getDefault() ); //$NON-NLS-1$
String strDateFile = sdf_file.format( new Date(System.currentTimeMillis()) );
writeToDisk( lineSeparator + "LogHandler: New File-Logger opened: "+ strDateFile + lineSeparator );
}
}
}
public synchronized void publish(LogRecord record) {
if( record!=null && logfile!=null && logfile.exists() ){
writeToDisk(format(record)+ lineSeparator );
}//if
}
public void writeToDisk(String strMessage) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(logfile.toString(), true);
fos.write( strMessage.getBytes());
} catch (FileNotFoundException e) {
System.err.println("error open logfile: " + e.getMessage());
} catch (IOException ex) {
System.err.println("error writing logfile: " + ex.getMessage());
} finally {
if(fos!=null)
try {
fos.close();
} catch (IOException e1) {
System.err.println("error closing logfile: " + logfile.toString());
}
}
}
public void close() {
System.out.println("log handler close");
}
/**
* Get the full qualified name of the used log file.
* @since 1.6.0
*/
public String getLogFileName() {
return logfile!=null?logfile.getAbsolutePath():null;
}
}