01: package org.jzonic.jlo.handler;
02:
03: import org.jzonic.jlo.LogRecord;
04: import org.jzonic.jlo.VariableManager;
05: import org.jzonic.jlo.error.ErrorHandler;
06:
07: import java.io.File;
08: import java.io.FileWriter;
09: import java.util.Map;
10:
11: /**
12: *@author Andreas Mecky
13: *@author Terry Dye
14: *@created 9. März 2002
15: *@version 1.0
16: */
17: public class FileHandler extends AbstractHandler {
18:
19: private static final VariableManager vm = VariableManager
20: .getInstance();
21: private String fileName = null;
22: private int maxSize = -1;
23:
24: /**
25: * Constructor for the FileHandler object
26: */
27: public FileHandler(String configName) {
28: super (configName);
29: }
30:
31: /**
32: *@param msg
33: */
34: public void publish(String msg) {
35: if (fileName == null)
36: ErrorHandler.reportError("No filename specified");
37: try {
38: File file = new File(fileName);
39: boolean append = true;
40: if (file.exists() && maxSize != -1) {
41: long length = file.length();
42: if (length > maxSize * 1024) {
43: append = false;
44: }
45: }
46: FileWriter fw = new FileWriter(fileName, append);
47: fw.write(msg + "\n");
48: fw.close();
49: } catch (Exception e) {
50: ErrorHandler.reportError(
51: "Exception while trying to write to file: "
52: + fileName, e);
53: }
54: }
55:
56: /**
57: * Description of the Method
58: *
59: *@param lr Description of the Parameter
60: */
61: public void publish(LogRecord lr) {
62: publish(lr.getMessage());
63: }
64:
65: /**
66: *@param parameters
67: */
68: public void setParameter(Map parameters) {
69: if (parameters.containsKey("file")) {
70: fileName = (String) parameters.get("file");
71: fileName = vm.replaceVariables(fileName,
72: getConfigurationName());
73: }
74: if (parameters.containsKey("maxsize"))
75: maxSize = Integer.parseInt((String) parameters
76: .get("maxsize"));
77: }
78: }
|