Source code: konspire/common/log/AbstractFileLoggingMechanism.java
1 package konspire.common.log;
2
3 import java.io.*;
4 import java.util.*;
5 import java.text.*;
6
7 /**
8 * AbstractFileLoggingMechanism is an abstract class which
9 * defines the format and location of log files,
10 * so they all work consistently for the same application. The default
11 * file name where logs are created is:
12 * <pre>
13 * [Path][App][LogClass][date].[extn]
14 *
15 * where: [Path] = "c:\temp\"
16 * [App] = "App"
17 * [LogClass] = The Java Class name of the Log Class
18 * (only used for unique logs)
19 * [date] = today's date (only used for dated logs)
20 * [extn] = "log"
21 *
22 * </pre>
23 * The [App] default can be overridden by the application, it is taken
24 * from the LogManager class. The [Path] and [extn] defaults can be
25 * overridden by settings in a properties file, see the LogProperties
26 * class for more information.
27 *
28 * @author Todd Lauinger
29 * @version $Revision: 1.3 $
30 */
31
32 public abstract class AbstractFileLoggingMechanism extends AbstractLoggingMechanism {
33
34
35 // Default class variables for where logs are put
36 protected static String logPath = LogProperties.DEFAULT_LOG_FILE_PATH;
37
38 // Instance variable to hold the output file stream
39 protected FileOutputStream fileOutputStream;
40
41 /**
42 * Instance variable to hold the current output file name
43 */
44 protected String currentFileName = "";
45
46 // Returns the filename prefix the log should write to
47 protected String logFileNamePrefix(Log log) {
48
49 StringBuffer sb = new StringBuffer();
50
51 // All files have a path and application name
52 sb.append(LogProperties.getInstance().getLoggingPath(log));
53 sb.append(LogManager.getInstance().getApplicationName());
54
55 // Unique logs also have the log name
56 if (! isSharedLog() ) {
57 sb.append(log.getName());
58 }
59
60 // Dated logs also have the current date
61 if (isDatedLog()) {
62 sb.append(compactCurrentDate());
63 }
64
65 return sb.toString();
66 }
67
68 // Returns the filename suffix the log should write to
69 protected String logFileNameSuffix(Log log) {
70
71 return "." + LogProperties.getInstance().getLoggingExtension(log);
72 }
73
74 public String getFileName() {
75 return currentFileName;
76 }
77
78 // Open a file log
79 synchronized protected void open(Log log) {
80
81 super.open(log);
82
83 String fileName = logFileNamePrefix(log) + logFileNameSuffix(log);
84
85 try {
86 fileOutputStream = new FileOutputStream(fileName, true); // append
87 printStream = new PrintWriter(fileOutputStream, true); // autoflush
88 currentFileName = fileName;
89 } catch (IOException e) {
90 System.err.println("Caught IOException: " +
91 e.getMessage() + " trying to open file named " + fileName);
92 currentFileName = "";
93 }
94
95 }
96
97 // Close a file log
98 synchronized protected void close() {
99
100 try {
101 fileOutputStream.close();
102 } catch (IOException e) {
103 System.err.println("Caught IOException: " +
104 e.getMessage() + " trying to close log file");
105 }
106
107 super.close();
108
109 }
110
111 /**
112 * Return a nice debug string.
113 */
114 public String toString() {
115 return super.toString() + " is logging to file " + currentFileName;
116 }
117
118 }