1
2
3
4
5
6
7
8 package org.codehaus.dna.impl;
9
10 import org.apache.log4j.Level;
11 import org.apache.log4j.Priority;
12 import org.codehaus.dna.Logger;
13
14 /***
15 * Logging facade implmentation for Apache Log4J project.
16 * The following lists the mapping between DNA log levels
17 * and Log4J log levels.
18 *
19 * <ul>
20 * <li>trace ==> debug</li>
21 * <li>debug ==> debug</li>
22 * <li>info ==> info</li>
23 * <li>warn ==> warn</li>
24 * <li>error ==> error</li>
25 * </ul>
26 *
27 * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
28 */
29 public class Log4JLogger
30 implements Logger
31 {
32 /***
33 * The fully qualified name of the current class so
34 * Log4J will not include it in traces.
35 */
36 private static final String FQCN = Log4JLogger.class.getName();
37
38 /***
39 * The log4j logger instance.
40 */
41 private final org.apache.log4j.Logger m_logger;
42
43 /***
44 * Create an instance of Log4J facade.
45 *
46 * @param logger the log4j logger
47 */
48 public Log4JLogger( final org.apache.log4j.Logger logger )
49 {
50 if( null == logger )
51 {
52 throw new NullPointerException( "logger" );
53 }
54 m_logger = logger;
55 }
56
57 /***
58 * Log a trace message.
59 *
60 * @param message the message
61 */
62 public void trace( final String message )
63 {
64 m_logger.log( FQCN, Level.DEBUG, message, null );
65 }
66
67 /***
68 * Log a trace message with an associated throwable.
69 *
70 * @param message the message
71 * @param throwable the throwable
72 */
73 public void trace( final String message,
74 final Throwable throwable )
75 {
76 m_logger.log( FQCN, Level.DEBUG, message, throwable );
77 }
78
79 /***
80 * Return true if a trace message will be logged.
81 *
82 * @return true if message will be logged
83 */
84 public boolean isTraceEnabled()
85 {
86 return m_logger.isDebugEnabled();
87 }
88
89 /***
90 * Log a debug message.
91 *
92 * @param message the message
93 */
94 public void debug( final String message )
95 {
96 m_logger.log( FQCN, Level.DEBUG, message, null );
97 }
98
99 /***
100 * Log a debug message with an associated throwable.
101 *
102 * @param message the message
103 * @param throwable the throwable
104 */
105 public void debug( final String message,
106 final Throwable throwable )
107 {
108 m_logger.log( FQCN, Level.DEBUG, message, throwable );
109 }
110
111 /***
112 * Return true if a debug message will be logged.
113 *
114 * @return true if message will be logged
115 */
116 public boolean isDebugEnabled()
117 {
118 return m_logger.isDebugEnabled();
119 }
120
121 /***
122 * Log a info message.
123 *
124 * @param message the message
125 */
126 public void info( final String message )
127 {
128 m_logger.log( FQCN, Level.INFO, message, null );
129 }
130
131 /***
132 * Log a info message with an associated throwable.
133 *
134 * @param message the message
135 * @param throwable the throwable
136 */
137 public void info( final String message,
138 final Throwable throwable )
139 {
140 m_logger.log( FQCN, Level.INFO, message, throwable );
141 }
142
143 /***
144 * Return true if an info message will be logged.
145 *
146 * @return true if message will be logged
147 */
148 public boolean isInfoEnabled()
149 {
150 return m_logger.isInfoEnabled();
151 }
152
153 /***
154 * Log a warn message.
155 *
156 * @param message the message
157 */
158 public void warn( final String message )
159 {
160 m_logger.log( FQCN, Level.WARN, message, null );
161 }
162
163 /***
164 * Log a warn message with an associated throwable.
165 *
166 * @param message the message
167 * @param throwable the throwable
168 */
169 public void warn( final String message,
170 final Throwable throwable )
171 {
172 m_logger.log( FQCN, Level.WARN, message, throwable );
173 }
174
175 /***
176 * Return true if a warn message will be logged.
177 *
178 * @return true if message will be logged
179 */
180 public boolean isWarnEnabled()
181 {
182 return m_logger.isEnabledFor( Priority.WARN );
183 }
184
185 /***
186 * Log a error message.
187 *
188 * @param message the message
189 */
190 public void error( final String message )
191 {
192 m_logger.log( FQCN, Level.ERROR, message, null );
193 }
194
195 /***
196 * Log a error message with an associated throwable.
197 *
198 * @param message the message
199 * @param throwable the throwable
200 */
201 public void error( final String message,
202 final Throwable throwable )
203 {
204 m_logger.log( FQCN, Level.ERROR, message, throwable );
205 }
206
207 /***
208 * Return true if a error message will be logged.
209 *
210 * @return true if message will be logged
211 */
212 public boolean isErrorEnabled()
213 {
214 return m_logger.isEnabledFor( Priority.ERROR );
215 }
216
217 /***
218 * Get the child logger with specified name.
219 *
220 * @param name the name of child logger
221 * @return the child logger
222 */
223 public Logger getChildLogger( final String name )
224 {
225
226 return new Log4JLogger( org.apache.log4j.Logger.
227 getLogger( m_logger.getName() + "." + name ) );
228 }
229 }