KickJava   Java API By Example, From Geeks To Geeks.

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


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: LogWriter.java,v 1.1 2005/07/13 11:09:06 slobodan Exp $
23  */

24
25 package com.lutris.logging;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 /**
31  * Class use to write log output to a particular LogChannel and level.
32  * This class is PrintWriter, with println() causing a write.
33  * One should use println() with this rather than write, as with a
34  * LogChannel, since write doesn't write a full line.
35  */

36 public class LogWriter extends PrintWriter JavaDoc {
37     /*
38      * Log channel and associated level.
39      */

40     private LogChannel channel;
41     private int level;
42
43     /*
44      * Are we enabled.
45      */

46     private boolean enabled;
47
48     /**
49      * Constructor.
50      * @param logChannel The log channel to write to.
51      * @param logLevel The level associated with this channel.
52      */

53     protected LogWriter(LogChannel logChannel,
54                         String JavaDoc logLevel) {
55         this(logChannel, logChannel.getLevel(logLevel));
56     }
57
58     /**
59      * Constructor.
60      * @param logChannel The log channel to write to.
61      * @param logLevel The level associated with this channel.
62      */

63     protected LogWriter(LogChannel logChannel,
64                         int logLevel) {
65         // Flushing logic is in ChannelWriter, so don't buffer here
66
super(new ChannelWriter(logChannel, logLevel), true);
67         channel = logChannel;
68         level = logLevel;
69         enabled = logChannel.isEnabled(level);
70     }
71
72     /**
73      * Get the associate channel.
74      */

75     public LogChannel getChannel() {
76         return channel;
77     }
78
79     /**
80      * Get the associate level.
81      */

82     public int getLevel() {
83         return level;
84     }
85
86     /**
87      * Determine if logging is enabled. This is useful to prevent a series of
88      * unnecessary logging calls, as often encountered with debug logging, or
89      * a call where generating the message is expensive.
90      *
91      * @return <CODE>true</CODE> if enabled, <CODE>false</CODE> if not
92      * enabled.
93      */

94     public boolean isEnabled() {
95         return enabled;
96     }
97     
98     /**
99      * Write a string and exception to the log file.
100      *
101      * @param msg The message to log.
102      * @param throwable Exception or error to log.
103      */

104     public void println(String JavaDoc msg, Throwable JavaDoc throwable) {
105         if (enabled) {
106             flush();
107             channel.write(level, msg, throwable);
108         }
109     }
110 }
111
112
113 /**
114  * Internal Writer object that interfaces to the LogChannel.
115  * Can't be inner class of LogWriter, it must be passed
116  * to super constructor.
117  */

118 class ChannelWriter extends Writer JavaDoc {
119     // Output collected here
120
private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
121
122     // Log channel, etr.
123
private LogChannel channel;
124     private int level;
125     private boolean enabled;
126
127     /**
128      * Construct a new writer.
129      */

130     public ChannelWriter(LogChannel logChannel,
131                          int logLevel) {
132         channel = logChannel;
133         level = logLevel;
134         enabled = logChannel.isEnabled(level);
135     }
136
137     /**
138      * Main write method that transfers stuff to the buffer.
139      */

140     public void write(char[] cbuf, int off, int len) {
141         buffer.append(cbuf, off, len);
142     }
143
144     /**
145      * Flush the buffer to the log file.
146      */

147     public void flush() {
148         if (enabled && (buffer.length() > 0)) {
149             synchronized(lock) {
150                 channel.write(level, buffer.toString());
151                 buffer.setLength(0);
152             }
153         }
154     }
155
156     /**
157      * Close is a no-op.
158      */

159     public void close() {
160     }
161 }
162
163
Popular Tags