001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: LogWriter.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.logging;
025:
026: import java.io.PrintWriter;
027: import java.io.Writer;
028:
029: /**
030: * Class use to write log output to a particular LogChannel and level.
031: * This class is PrintWriter, with println() causing a write.
032: * One should use println() with this rather than write, as with a
033: * LogChannel, since write doesn't write a full line.
034: */
035: public class LogWriter extends PrintWriter {
036: /*
037: * Log channel and associated level.
038: */
039: private LogChannel channel;
040: private int level;
041:
042: /*
043: * Are we enabled.
044: */
045: private boolean enabled;
046:
047: /**
048: * Constructor.
049: * @param logChannel The log channel to write to.
050: * @param logLevel The level associated with this channel.
051: */
052: protected LogWriter(LogChannel logChannel, String logLevel) {
053: this (logChannel, logChannel.getLevel(logLevel));
054: }
055:
056: /**
057: * Constructor.
058: * @param logChannel The log channel to write to.
059: * @param logLevel The level associated with this channel.
060: */
061: protected LogWriter(LogChannel logChannel, int logLevel) {
062: // Flushing logic is in ChannelWriter, so don't buffer here
063: super (new ChannelWriter(logChannel, logLevel), true);
064: channel = logChannel;
065: level = logLevel;
066: enabled = logChannel.isEnabled(level);
067: }
068:
069: /**
070: * Get the associate channel.
071: */
072: public LogChannel getChannel() {
073: return channel;
074: }
075:
076: /**
077: * Get the associate level.
078: */
079: public int getLevel() {
080: return level;
081: }
082:
083: /**
084: * Determine if logging is enabled. This is useful to prevent a series of
085: * unnecessary logging calls, as often encountered with debug logging, or
086: * a call where generating the message is expensive.
087: *
088: * @return <CODE>true</CODE> if enabled, <CODE>false</CODE> if not
089: * enabled.
090: */
091: public boolean isEnabled() {
092: return enabled;
093: }
094:
095: /**
096: * Write a string and exception to the log file.
097: *
098: * @param msg The message to log.
099: * @param throwable Exception or error to log.
100: */
101: public void println(String msg, Throwable throwable) {
102: if (enabled) {
103: flush();
104: channel.write(level, msg, throwable);
105: }
106: }
107: }
108:
109: /**
110: * Internal Writer object that interfaces to the LogChannel.
111: * Can't be inner class of LogWriter, it must be passed
112: * to super constructor.
113: */
114: class ChannelWriter extends Writer {
115: // Output collected here
116: private StringBuffer buffer = new StringBuffer();
117:
118: // Log channel, etr.
119: private LogChannel channel;
120: private int level;
121: private boolean enabled;
122:
123: /**
124: * Construct a new writer.
125: */
126: public ChannelWriter(LogChannel logChannel, int logLevel) {
127: channel = logChannel;
128: level = logLevel;
129: enabled = logChannel.isEnabled(level);
130: }
131:
132: /**
133: * Main write method that transfers stuff to the buffer.
134: */
135: public void write(char[] cbuf, int off, int len) {
136: buffer.append(cbuf, off, len);
137: }
138:
139: /**
140: * Flush the buffer to the log file.
141: */
142: public void flush() {
143: if (enabled && (buffer.length() > 0)) {
144: synchronized (lock) {
145: channel.write(level, buffer.toString());
146: buffer.setLength(0);
147: }
148: }
149: }
150:
151: /**
152: * Close is a no-op.
153: */
154: public void close() {
155: }
156: }
|