Package org.jzonic.jlo.handler

Source Code of org.jzonic.jlo.handler.FileHandler

package org.jzonic.jlo.handler;

import org.jzonic.jlo.LogRecord;
import org.jzonic.jlo.VariableManager;
import org.jzonic.jlo.error.ErrorHandler;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;
/**
*@author     Andreas Mecky
*@author     Terry Dye
*@created    9. M�rz 2002
*@version    1.0
*/
public class FileHandler extends AbstractHandler {
   
    private static final VariableManager vm = VariableManager.getInstance();
    private String fileName = null;
    private int maxSize = -1;

    /**
     *  Constructor for the FileHandler object
     */
    public FileHandler(String configName) {
      super(configName);
    }


    /**
     *@param  msg
     */
    public void publish(String msg) {       
        if ( fileName == null )
            ErrorHandler.reportError("No filename specified");
        try {           
          File file = new File(fileName);
          boolean append = true;
          if ( file.exists() && maxSize != -1 ) {
            long length = file.length();
            if ( length > maxSize*1024 ) {
              append = false;
            }
          }
            FileWriter fw = new FileWriter(fileName, append);
            fw.write(msg + "\n");
            fw.close();
        } catch (Exception e) {
            ErrorHandler.reportError("Exception while trying to write to file: " + fileName, e);
        }
    }


    /**
     *  Description of the Method
     *
     *@param  lr  Description of the Parameter
     */
    public void publish(LogRecord lr) {
        publish(lr.getMessage());
    }


    /**
     *@param  parameters
     */
    public void setParameter(Map parameters) {       
        if ( parameters.containsKey("file") ) {
            fileName = (String) parameters.get("file");
            fileName = vm.replaceVariables(fileName, getConfigurationName());
        }
        if ( parameters.containsKey("maxsize") )
            maxSize = Integer.parseInt((String)parameters.get("maxsize"));
    }
}
TOP

Related Classes of org.jzonic.jlo.handler.FileHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.