KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > logging > StandardLogChannel


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: StandardLogChannel.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
23  */

24
25 package com.lutris.logging;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.text.DateFormat JavaDoc;
30 import java.text.FieldPosition JavaDoc;
31 import java.text.SimpleDateFormat JavaDoc;
32 import java.util.Date JavaDoc;
33
34 /**
35  * Standard implementation of a channel associated with a logging
36  * facility. All messages for the facility are written using a channel.
37  * Care is take to avoid synchronization when possible for performance
38  * reasons.
39  *
40  * @author Mark Diekhans
41  * @see com.lutris.logging.LogChannel
42  * @see com.lutris.logging.StandardLogger
43  */

44 public class StandardLogChannel implements LogChannel {
45     /**
46      * Our symbolic name.
47      */

48     private String JavaDoc facility;
49
50     /**
51      * Format for the date.
52      */

53     private static final SimpleDateFormat JavaDoc dateFormatter =
54         new SimpleDateFormat JavaDoc("yyyy.MM.dd HH:mm:ss");
55
56     /**
57      * Logger object with which we are associated.
58      */

59     private StandardLogger logger;
60
61
62     /**
63      * Construct a new log channel.
64      *
65      * @param chanFacility The facility that the channel is associate with.
66      * @param loggerObj The logging object that this channel will be associated
67      * with.
68      */

69     protected StandardLogChannel(String JavaDoc chanFacility,
70                                  StandardLogger loggerObj) {
71         facility = chanFacility;
72         logger = loggerObj;
73
74     }
75
76     /**
77      * @see LogChannel.getLevel
78      */

79     public int getLevel(String JavaDoc level) {
80         return logger.getLevel(level);
81     }
82
83     /**
84      * @see LogChannel.getLogWriter#String
85      */

86     public LogWriter getLogWriter(String JavaDoc level) {
87         return new LogWriter(this, level);
88     }
89
90     /**
91      * @see LogChannel.getLogWriter#int
92      */

93     public LogWriter getLogWriter(int level) {
94         return new LogWriter(this, level);
95     }
96
97     /**
98      * @see LogChannel.isEnabled
99      */

100     public boolean isEnabled(int level) {
101         // Copy to avoid needing to be synchronized.
102
boolean[] enabledLevelFlags = logger.enabledLevelFlags;
103         return (enabledLevelFlags != null)
104             && (level >= 0 && level < enabledLevelFlags.length)
105             && enabledLevelFlags[level];
106     }
107
108     /**
109      * @see LogChannel.isEnabled
110      */

111     public boolean isEnabled(String JavaDoc level) {
112         return isEnabled(logger.getLevel(level));
113     }
114
115     /**
116      * Do the work of writting a log message. It should already be
117      * determined if the channel is enabled; but limited synchronization
118      * is required, as we ignore any error while writing. Handles
119      * multiline messages.
120      *
121      *
122      * @param out Print output stream to write to.
123      * @param date The date to format into the message
124      * @param level Level string for message.
125      */

126     private void doWrite(PrintWriter JavaDoc out, Date JavaDoc date, int level, String JavaDoc msg) {
127         try {
128             // Create prefix for each line.
129
StringBuffer JavaDoc msgBuf = new StringBuffer JavaDoc();
130             dateFormatter.format(date, msgBuf,
131                                  new FieldPosition JavaDoc(DateFormat.YEAR_FIELD));
132             msgBuf.append(": ");
133             msgBuf.append(facility);
134             msgBuf.append(',');
135
136             msgBuf.append(logger.levelNames[level]);
137
138             // Write as individual lines with the same prefix.
139
int nextIdx = 0;
140             int newlineIdx;
141
142             synchronized (out) {
143                 while (nextIdx < msg.length()) {
144                     out.print(msgBuf);
145                     if (nextIdx == 0) {
146                         out.print(": ");
147                     } else {
148                         out.print("+ ");
149                     }
150                     newlineIdx = msg.indexOf('\n', nextIdx);
151                     if (newlineIdx < 0) {
152                         newlineIdx = msg.length();
153                     }
154                     out.write(msg, nextIdx, (newlineIdx - nextIdx));
155                     out.println();
156                     nextIdx = newlineIdx + 1;
157                 }
158                 out.flush();
159             }
160         } catch (Throwable JavaDoc ignore) {
161             // FIXME: Delete eventually or maybe make a fatal exception??
162
System.err.println("StandardLogChannel.doWrite ignored exception:");
163             ignore.printStackTrace();
164             // All errors, including runtime ones, are ignored.
165
}
166     }
167
168
169     /**
170      * @see LogChannel.write
171      */

172     public void write(int level, String JavaDoc msg) {
173         if (isEnabled(level)) {
174             Date JavaDoc date = new Date JavaDoc();
175             if (logger.logFileLevelFlags[level]) {
176                 doWrite(logger.logFileStream, date, level, msg);
177             }
178             if (logger.stderrLevelFlags[level]) {
179                 doWrite(logger.stderrStream, date, level, msg);
180             }
181         }
182     }
183
184     /**
185      * @see LogChannel.write
186      */

187     public synchronized void write(String JavaDoc level, String JavaDoc msg) {
188         write(getLevel(level), msg);
189     }
190
191     /**
192      * @see LogChannel.write
193      */

194     public synchronized void write(int level, String JavaDoc msg, Throwable JavaDoc throwable) {
195         if (isEnabled(level)) {
196             Date JavaDoc date = new Date JavaDoc();
197             StringWriter JavaDoc stackBuf = new StringWriter JavaDoc();
198             throwable.printStackTrace(new PrintWriter JavaDoc(stackBuf));
199             stackBuf.flush();
200
201             String JavaDoc errMsg = msg + ":" + " " + throwable.getMessage() + '\n'
202                 + stackBuf;
203
204             if (logger.logFileLevelFlags[level]) {
205                 doWrite(logger.logFileStream, date, level, errMsg);
206             }
207             if (logger.stderrLevelFlags[level]) {
208                 doWrite(logger.stderrStream, date, level, errMsg);
209             }
210         }
211     }
212
213     /**
214      * @see LogChannel.write
215      */

216     public synchronized void write(String JavaDoc level, String JavaDoc msg, Throwable JavaDoc throwable) {
217         write(getLevel(level), msg, throwable);
218     }
219 }
220
Popular Tags