KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > BasicLogger


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.net.*;
54 import java.util.*;
55 import java.text.*;
56
57 /**
58  * A base class for Syslogger implementations.
59  * This class provides common functions for setting
60  * the date format for log entries and for formatting dates.
61  * The default policy used by this logger is the
62  * {@link SimpleLogPolicy SimpleLogPolicy} policy. The
63  * default text formatter is the
64  * {@link SimpleSyslogTextFormatter SimpleSyslogTextFormatter}
65  * formatter. If this class is used in JDK 1.4, the
66  * defauly text formatter is the
67  * {@link JDK14SyslogTextFormatter JDK14SyslogTextFormatter}.
68  *
69  * @see com.protomatter.syslog.xml.BasicLogger_Helper XML configuration class
70  */

71 public abstract class BasicLogger
72 implements Syslogger
73 {
74   protected LogPolicy policy = new SimpleLogPolicy();
75   protected LogPolicy realPolicy = null;
76   protected SyslogTextFormatter formatter = null;
77   private String JavaDoc name = null;
78
79   /**
80    * The default constructor -- configure() will need to be called.
81    */

82   public BasicLogger()
83   {
84     super();
85
86     formatter = new SimpleSyslogTextFormatter();
87   }
88
89   /**
90    * Suspend the operation of this logger. After suspending
91    * a logger, it will no longer accept new messages.
92    * Any messages alread received an in a buffer may or
93    * may not be written.
94    */

95   public void suspend()
96   {
97       this.realPolicy = this.policy;
98       this.policy = new FalseLogPolicy();
99   }
100
101   /**
102    * Resume operations with this logger. Any new messages
103    * will be processed normally.
104    */

105   public void resume()
106   {
107       this.policy = this.realPolicy;
108       this.realPolicy = null;
109   }
110
111   /**
112    * Determine if this logger is suspended or not.
113    */

114   public boolean isSuspended()
115   {
116       return (this.realPolicy != null);
117   }
118
119   /**
120    * Set the log policy used by this logger.
121    */

122   public void setPolicy(LogPolicy policy)
123   {
124     this.policy = policy;
125   }
126
127   /**
128    * Get the log policy used by this logger.
129    */

130   public LogPolicy getPolicy()
131   {
132     return this.policy;
133   }
134
135   /**
136    * Determine if it's likely that a message from the given
137    * logger at the given level on the given channel will be
138    * paid attention to. This method delegates the decision
139    * to the <TT>LogPolicy</TT> that's being used. The message
140    * generated to query the policy has everything except the
141    * message, detail and hostname filled in.
142    *
143    * @see LogPolicy
144    * @deprecated
145    */

146   public boolean mightLog(Object JavaDoc logger, int level, String JavaDoc channel)
147   {
148     SyslogMessage m = new SyslogMessage();
149     m.level = level;
150     m.channel = channel;
151     m.thread = Thread.currentThread();
152
153     m.logger = logger;
154     if (logger != null)
155     {
156       // get the name of the logger's class.
157
if (logger instanceof Class JavaDoc)
158         m.loggerClassname = ((Class JavaDoc)logger).getName();
159       else
160         m.loggerClassname = logger.getClass().getName();
161     }
162     else
163     {
164       m.loggerClassname = "?";
165     }
166
167     // current time.
168
m.time = System.currentTimeMillis();
169
170     // ask the policy what it thinks.
171
return policy.shouldLog(m);
172   }
173
174   /**
175    * Set the log formatter object used by this logger.
176    */

177   public void setTextFormatter(SyslogTextFormatter formatter)
178   {
179     this.formatter = formatter;
180   }
181
182   /**
183    * Get the log formatter object used by this logger.
184    */

185   public SyslogTextFormatter getTextFormatter()
186   {
187     return this.formatter;
188   }
189
190   /**
191    * A utility method to see if the current log policy says we
192    * should pay attention to this message.
193    */

194   protected boolean shouldLog(SyslogMessage message)
195   {
196     return policy.shouldLog(message);
197   }
198
199   /**
200    * Format the log entry using the current log formatter.
201    *
202    * @see SimpleSyslogTextFormatter
203    */

204   protected void formatLogEntry(StringBuffer JavaDoc b, SyslogMessage message)
205   {
206     formatter.formatLogEntry(b, message);
207   }
208
209   /**
210    * Reset the text formatter's date format. This is
211    * basically a kludge for loggers that rotate
212    * their logs.
213    *
214    * @see SyslogTextFormatter
215    */

216   protected void resetDateFormat()
217   {
218     formatter.resetDateFormat();
219   }
220
221   /**
222    * Set this logger's name.
223    */

224   public void setName(String JavaDoc name)
225   {
226     this.name = name;
227   }
228
229   /**
230    * Get this logger's name.
231    */

232   public String JavaDoc getName()
233   {
234     return this.name;
235   }
236 }
237
Popular Tags