KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > autodoc > v1 > log4j > Log4jLog


1 /*
2  * @(#)Log4jLog.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.autodoc.v1.log4j;
30
31
32 import net.sourceforge.groboutils.autodoc.v1.AutoDocLog;
33
34 import org.apache.log4j.Logger;
35 import org.apache.log4j.Priority;
36  
37
38 /**
39  * An interface for logging. This allows for an abstraction between the
40  * owning class and any underlying logging mechanism desired.
41  * <P>
42  * The actual meaning of the logging levels is implementation independent.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version $Date: 2003/02/10 22:52:12 $
46  * @since March 16, 2002
47  */

48 public class Log4jLog implements AutoDocLog
49 {
50     private Logger log;
51     
52     
53     /**
54      * Standard constructor - uses the Logger based on the given class.
55      *
56      * @param owner the owning class to assign log responsibility to.
57      * Must not be <tt>null</tt>.
58      * @exception IllegalArgumentException if <tt>owner</tt> is <tt>null</tt>.
59      */

60     public Log4jLog( Class JavaDoc owner )
61     {
62         if (owner == null)
63         {
64             throw new IllegalArgumentException JavaDoc("no null arguments");
65         }
66         setLog( Logger.getLogger( owner ) );
67     }
68     
69     
70     /**
71      * Create a log interface based on the given Apache Logger.
72      *
73      * @param log the underlying log instance to use for logging. Must not
74      * be <tt>null</tt>.
75      * @exception IllegalArgumentException if <tt>owner</tt> is <tt>null</tt>.
76      */

77     public Log4jLog( Logger log )
78     {
79         setLog( log );
80     }
81     
82     
83     public void debug( Object JavaDoc message )
84     {
85         this.log.debug( message );
86     }
87
88     public void debug( Object JavaDoc message[] )
89     {
90         if (this.log.isDebugEnabled())
91         {
92             this.log.debug( concatMessage( message ) );
93         }
94     }
95     
96     public void debug( Object JavaDoc message, Throwable JavaDoc error )
97     {
98         this.log.debug( message, error );
99     }
100
101     public void debug( Object JavaDoc message[], Throwable JavaDoc error )
102     {
103         if (this.log.isDebugEnabled())
104         {
105             this.log.debug( concatMessage( message ), error );
106         }
107     }
108     
109     public void info( Object JavaDoc message )
110     {
111         this.log.info( message );
112     }
113
114     public void info( Object JavaDoc message[] )
115     {
116         if (this.log.isInfoEnabled())
117         {
118             this.log.info( concatMessage( message ) );
119         }
120     }
121     
122     public void info( Object JavaDoc message, Throwable JavaDoc error )
123     {
124         this.log.info( message, error );
125     }
126
127     public void info( Object JavaDoc message[], Throwable JavaDoc error )
128     {
129         if (this.log.isInfoEnabled())
130         {
131             Object JavaDoc msg = concatMessage( message );
132             this.log.info( msg, error );
133         }
134     }
135     
136     public void warn( Object JavaDoc message )
137     {
138         this.log.warn( message );
139     }
140
141     public void warn( Object JavaDoc message[] )
142     {
143         if (this.log.isEnabledFor( Priority.WARN ))
144         {
145             this.log.warn( concatMessage( message ) );
146         }
147     }
148     
149     public void warn( Object JavaDoc message, Throwable JavaDoc error )
150     {
151         this.log.warn( message, error );
152     }
153     
154     public void warn( Object JavaDoc message[], Throwable JavaDoc error )
155     {
156         if (this.log.isEnabledFor( Priority.WARN ))
157         {
158             this.log.warn( concatMessage( message ), error );
159         }
160     }
161     
162     
163     /**
164      * Concatenate the given array into a single string. The returned object
165      * will be of type <tt>java.lang.StringBuffer</tt> or
166      * <tt>java.lang.String</tt>; use <tt>toString()</tt> on the resulting
167      * object.
168      *
169      * @param o an array (possibly null) of objects (possibly null) to
170      * concatenate their <tt>toString()</tt> results together.
171      * @return the concatenated message.
172      * @see java.lang.StringBuffer
173      */

174     protected Object JavaDoc concatMessage( Object JavaDoc o[] )
175     {
176         if (o == null)
177         {
178             return "null";
179         }
180         if (o.length <= 0)
181         {
182             return "";
183         }
184         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
185         for (int i = 0; i < o.length; ++i)
186         {
187             sb.append( o[i] );
188         }
189         return sb;
190     }
191     
192     
193     /**
194      * Sets the internal log instance.
195      *
196      * @param log the log to set. It must not be <tt>null</tt>.
197      * @exception IllegalArgumentException if <tt>log</tt> is <tt>null</tt>.
198      */

199     protected void setLog( Logger log )
200     {
201         if (log == null)
202         {
203             throw new IllegalArgumentException JavaDoc("no null arguments");
204         }
205         this.log = log;
206     }
207 }
208
209
Popular Tags