KickJava   Java API By Example, From Geeks To Geeks.

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


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: Log4jLogWriter.java,v 1.2 2005/03/24 10:51:20 slobodan Exp $
23  */

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

34 public class Log4jLogWriter extends LogWriter {
35     /*
36      * Log channel and associated level.
37      */

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

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

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

62     protected Log4jLogWriter(LogChannel logChannel,
63                         int logLevel) {
64         // Flushing logic is in ChannelWriter, so don't buffer here
65

66         super(logChannel, logLevel);
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
Popular Tags