1 package org.jzonic.jlo.handler; 2 3 import org.jzonic.jlo.LogRecord; 4 import org.jzonic.jlo.VariableManager; 5 import org.jzonic.jlo.error.ErrorHandler; 6 7 import java.io.File ; 8 import java.io.FileWriter ; 9 import java.util.Map ; 10 16 public class FileHandler extends AbstractHandler { 17 18 private static final VariableManager vm = VariableManager.getInstance(); 19 private String fileName = null; 20 private int maxSize = -1; 21 22 25 public FileHandler(String configName) { 26 super(configName); 27 } 28 29 30 33 public void publish(String msg) { 34 if ( fileName == null ) 35 ErrorHandler.reportError("No filename specified"); 36 try { 37 File file = new File (fileName); 38 boolean append = true; 39 if ( file.exists() && maxSize != -1 ) { 40 long length = file.length(); 41 if ( length > maxSize*1024 ) { 42 append = false; 43 } 44 } 45 FileWriter fw = new FileWriter (fileName, append); 46 fw.write(msg + "\n"); 47 fw.close(); 48 } catch (Exception e) { 49 ErrorHandler.reportError("Exception while trying to write to file: " + fileName, e); 50 } 51 } 52 53 54 59 public void publish(LogRecord lr) { 60 publish(lr.getMessage()); 61 } 62 63 64 67 public void setParameter(Map parameters) { 68 if ( parameters.containsKey("file") ) { 69 fileName = (String ) parameters.get("file"); 70 fileName = vm.replaceVariables(fileName, getConfigurationName()); 71 } 72 if ( parameters.containsKey("maxsize") ) 73 maxSize = Integer.parseInt((String )parameters.get("maxsize")); 74 } 75 } 76 | Popular Tags |