1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.objectledge.logging;
29
30 import java.io.BufferedWriter;
31 import java.io.IOException;
32 import java.io.Writer;
33
34 import org.apache.log4j.FileAppender;
35 import org.apache.log4j.helpers.LogLog;
36 import org.objectledge.filesystem.FileSystem;
37 import org.objectledge.filesystem.UnsupportedCharactersInFilePathException;
38
39 /***
40 * A derivate of log4j.FileAppender that accepts paths within Ledge file system.
41 *
42 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
43 * @version $Id: LedgeFileAppender.java,v 1.4 2004/12/22 08:34:55 rafal Exp $
44 */
45 public class LedgeFileAppender extends FileAppender
46 {
47 private FileSystem fileSystem;
48
49 /***
50 * Creates new LedgeFileAppender instance.
51 *
52 * @param fileSystem Ledge FileSystem object.
53 */
54 public LedgeFileAppender(FileSystem fileSystem)
55 {
56 this.fileSystem = fileSystem;
57 }
58
59 /***
60 * <p>
61 * Sets and <i>opens </i> the file where the log output will go. The specified file must be
62 * writable.
63 *
64 * <p>
65 * If there was already an opened file, then the previous file is closed first.
66 *
67 * <p>
68 * <b>Do not use this method directly. To configure a FileAppender or one of its subclasses, set
69 * its properties one by one and then call activateOptions. </b>
70 *
71 * @param fileName The path to the log file.
72 * @param append If true will append to fileName. Otherwise will truncate fileName.
73 * @param bufferedIO If true buffered IO will be used.
74 * @param bufferSize the size of buffer in bytes.
75 * @throws IOException if setting up the file IO fails.
76 */
77 public synchronized void setFile(String fileName, boolean append, boolean bufferedIO,
78 int bufferSize) throws IOException
79 {
80 LogLog.debug("setFile called: " + fileName + ", " + append);
81
82
83 if(bufferedIO)
84 {
85 setImmediateFlush(false);
86 }
87
88 reset();
89 try
90 {
91 fileSystem.mkdirs(FileSystem.directoryPath(fileName));
92 }
93 catch(UnsupportedCharactersInFilePathException e)
94 {
95 throw (IOException) (new IOException().initCause(e));
96 }
97 Writer fw = createWriter(fileSystem.getOutputStream(fileName, append));
98 if(bufferedIO)
99 {
100 fw = new BufferedWriter(fw, bufferSize);
101 }
102 this.setQWForFiles(fw);
103 this.fileName = fileName;
104 this.fileAppend = append;
105 this.bufferedIO = bufferedIO;
106 this.bufferSize = bufferSize;
107 writeHeader();
108 LogLog.debug("setFile ended");
109 }
110 }