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: MonologWriter.java,v 1.2 2006-06-15 13:47:00 sinisa Exp $
022: */
023:
024: package com.lutris.logging;
025:
026: /**
027: * Class use to write log output to a particular LogChannel and level.
028: * This class is PrintWriter, with println() causing a write.
029: * One should use println() with this rather than write, as with a
030: * LogChannel, since write doesn't write a full line.
031: */
032: public class MonologWriter extends LogWriter {
033: /*
034: * Log channel and associated level.
035: */
036: private LogChannel channel;
037: private int level;
038:
039: /*
040: * Are we enabled.
041: */
042: private boolean enabled;
043:
044: /**
045: * Constructor.
046: * @param logChannel The log channel to write to.
047: * @param logLevel The level associated with this channel.
048: */
049: protected MonologWriter(LogChannel logChannel, String logLevel) {
050: this (logChannel, logChannel.getLevel(logLevel));
051: }
052:
053: /**
054: * Constructor.
055: * @param logChannel The log channel to write to.
056: * @param logLevel The level associated with this channel.
057: */
058: protected MonologWriter(LogChannel logChannel, int logLevel) {
059: // Flushing logic is in ChannelWriter, so don't buffer here
060:
061: super (logChannel, logLevel);
062: channel = logChannel;
063: level = logLevel;
064: enabled = logChannel.isEnabled(level);
065: }
066:
067: /**
068: * Get the associate channel.
069: */
070: public LogChannel getChannel() {
071: return channel;
072: }
073:
074: /**
075: * Get the associate level.
076: */
077: public int getLevel() {
078: return level;
079: }
080:
081: /**
082: * Determine if logging is enabled. This is useful to prevent a series of
083: * unnecessary logging calls, as often encountered with debug logging, or
084: * a call where generating the message is expensive.
085: *
086: * @return <CODE>true</CODE> if enabled, <CODE>false</CODE> if not
087: * enabled.
088: */
089: // public boolean isEnabled() {
090: // return enabled;
091: // }
092: /**
093: * Write a string and exception to the log file.
094: *
095: * @param msg The message to log.
096: * @param throwable Exception or error to log.
097: */
098: public void println(String msg, Throwable throwable) {
099: if (enabled) {
100: // flush();
101: channel.write(level, msg, throwable);
102: }
103: }
104: }
|