Package org.jzonic.jlo.handler

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

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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

// Referenced classes of package org.jlo.handler:
//            Handler

public class DateFileHandler extends AbstractHandler {
   
    private static final VariableManager vm = VariableManager.getInstance();
    private String fileName;
    private int maxSize;
    private String dateFormat;
   
    public DateFileHandler(String configName) {
        super(configName);
        fileName = null;
        maxSize = -1;
        dateFormat = "dd.MM.yyyy";
    }
   
    public void publish(String msg) {
        String fn = prepareFileName();
        if (fileName == null)
            ErrorHandler.reportError("No filename specified");
        try {
          File file = new File(fn);
          boolean append = true;
          if ( file.exists() && maxSize != -1 ) {
            long length = file.length();
            if ( length > maxSize*1024 ) {
              append = false;
            }
          }
            FileWriter fw = new FileWriter(fn, append);
            fw.write(msg + "\n");
            fw.close();
        } catch (Exception e) {
            ErrorHandler.reportError("Exception while trying to write to file: " + fn,e);
        }
    }
   
    public void publish(LogRecord lr) {
        publish(lr.getMessage());
    }
   
    public void setParameter(Map parameters) {
        if (parameters.containsKey("file"))
            fileName = (String) parameters.get("file");
        if (parameters.containsKey("maxsize"))
            maxSize = Integer.parseInt((String) parameters.get("maxsize"));
        if (parameters.containsKey("format"))
            dateFormat = (String) parameters.get("format");
    }
   
    public String prepareFileName() {
        if (fileName != null) {
            String fn = null;
            String tmpName = fileName.toLowerCase();
            int pos = tmpName.indexOf("${date}");
            if (pos == -1) {
                fn = fileName;
            } else {
                Date rightNow = new Date(System.currentTimeMillis());
                String firstPart = fileName.substring(0, pos );
                String secondPart = fileName.substring(pos + 7);
                SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
                String dateString = formatter.format(rightNow);
                fn = firstPart + dateString + secondPart;
            }
            return vm.replaceVariables(fn, getConfigurationName());
        } else {
            return null;
        }
    }
   
}
TOP

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

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.