View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.logging.impl;
19  
20  import java.io.Serializable;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.log.Hierarchy;
24  import org.apache.log.Logger;
25  
26  /**
27   * Implementation of {@code org.apache.commons.logging.Log}
28   * that wraps the <a href="https://avalon.apache.org/logkit/">avalon-logkit</a>
29   * logging system. Configuration of {@code LogKit} is left to the user.
30   * <p>
31   * {@code LogKit} accepts only {@code String} messages.
32   * Therefore, this implementation converts object messages into strings
33   * by called their {@code toString()} method before logging them.
34   *
35   * @deprecated Scheduled for removal because the Apache Avalon Project has been discontinued.
36   */
37  @Deprecated
38  public class LogKitLogger implements Log, Serializable {
39  
40      /** Serializable version identifier. */
41      private static final long serialVersionUID = 3768538055836059519L;
42  
43      /** Logging goes to this {@code LogKit} logger */
44      protected transient volatile Logger logger;
45  
46      /** Name of this logger */
47      protected String name;
48  
49      /**
50       * Constructs {@code LogKitLogger} which wraps the {@code LogKit}
51       * logger with given name.
52       *
53       * @param name log name
54       */
55      public LogKitLogger(final String name) {
56          this.name = name;
57          this.logger = getLogger();
58      }
59  
60      /**
61       * Logs a message with {@code org.apache.log.Priority.DEBUG}.
62       *
63       * @param message to log
64       * @see org.apache.commons.logging.Log#debug(Object)
65       */
66      @Override
67      public void debug(final Object message) {
68          if (message != null) {
69              getLogger().debug(String.valueOf(message));
70          }
71      }
72  
73      /**
74       * Logs a message with {@code org.apache.log.Priority.DEBUG}.
75       *
76       * @param message to log
77       * @param t log this cause
78       * @see org.apache.commons.logging.Log#debug(Object, Throwable)
79       */
80      @Override
81      public void debug(final Object message, final Throwable t) {
82          if (message != null) {
83              getLogger().debug(String.valueOf(message), t);
84          }
85      }
86  
87      /**
88       * Logs a message with {@code org.apache.log.Priority.ERROR}.
89       *
90       * @param message to log
91       * @see org.apache.commons.logging.Log#error(Object)
92       */
93      @Override
94      public void error(final Object message) {
95          if (message != null) {
96              getLogger().error(String.valueOf(message));
97          }
98      }
99  
100     /**
101      * Logs a message with {@code org.apache.log.Priority.ERROR}.
102      *
103      * @param message to log
104      * @param t log this cause
105      * @see org.apache.commons.logging.Log#error(Object, Throwable)
106      */
107     @Override
108     public void error(final Object message, final Throwable t) {
109         if (message != null) {
110             getLogger().error(String.valueOf(message), t);
111         }
112     }
113 
114     /**
115      * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}.
116      *
117      * @param message to log
118      * @see org.apache.commons.logging.Log#fatal(Object)
119      */
120     @Override
121     public void fatal(final Object message) {
122         if (message != null) {
123             getLogger().fatalError(String.valueOf(message));
124         }
125     }
126 
127     /**
128      * Logs a message with {@code org.apache.log.Priority.FATAL_ERROR}.
129      *
130      * @param message to log
131      * @param t log this cause
132      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
133      */
134     @Override
135     public void fatal(final Object message, final Throwable t) {
136         if (message != null) {
137             getLogger().fatalError(String.valueOf(message), t);
138         }
139     }
140 
141     /**
142      * Gets the underlying Logger we are using.
143      *
144      * @return the underlying Logger we are using.
145      */
146     public Logger getLogger() {
147         Logger result = logger;
148         if (result == null) {
149             synchronized(this) {
150                 result = logger;
151                 if (result == null) {
152                     logger = result = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
153                 }
154             }
155         }
156         return result;
157     }
158 
159     /**
160      * Logs a message with {@code org.apache.log.Priority.INFO}.
161      *
162      * @param message to log
163      * @see org.apache.commons.logging.Log#info(Object)
164      */
165     @Override
166     public void info(final Object message) {
167         if (message != null) {
168             getLogger().info(String.valueOf(message));
169         }
170     }
171 
172     /**
173      * Logs a message with {@code org.apache.log.Priority.INFO}.
174      *
175      * @param message to log
176      * @param t log this cause
177      * @see org.apache.commons.logging.Log#info(Object, Throwable)
178      */
179     @Override
180     public void info(final Object message, final Throwable t) {
181         if (message != null) {
182             getLogger().info(String.valueOf(message), t);
183         }
184     }
185 
186     /**
187      * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
188      */
189     @Override
190     public boolean isDebugEnabled() {
191         return getLogger().isDebugEnabled();
192     }
193 
194     /**
195      * Checks whether the {@code LogKit} logger will log messages of priority {@code ERROR}.
196      */
197     @Override
198     public boolean isErrorEnabled() {
199         return getLogger().isErrorEnabled();
200     }
201 
202     /**
203      * Checks whether the {@code LogKit} logger will log messages of priority {@code FATAL_ERROR}.
204      */
205     @Override
206     public boolean isFatalEnabled() {
207         return getLogger().isFatalErrorEnabled();
208     }
209 
210     /**
211      * Checks whether the {@code LogKit} logger will log messages of priority {@code INFO}.
212      */
213     @Override
214     public boolean isInfoEnabled() {
215         return getLogger().isInfoEnabled();
216     }
217 
218     /**
219      * Checks whether the {@code LogKit} logger will log messages of priority {@code DEBUG}.
220      */
221     @Override
222     public boolean isTraceEnabled() {
223         return getLogger().isDebugEnabled();
224     }
225 
226     /**
227      * Checks whether the {@code LogKit} logger will log messages of priority {@code WARN}.
228      */
229     @Override
230     public boolean isWarnEnabled() {
231         return getLogger().isWarnEnabled();
232     }
233 
234     /**
235      * Logs a message with {@code org.apache.log.Priority.DEBUG}.
236      *
237      * @param message to log
238      * @see org.apache.commons.logging.Log#trace(Object)
239     */
240     @Override
241     public void trace(final Object message) {
242         debug(message);
243     }
244 
245     /**
246      * Logs a message with {@code org.apache.log.Priority.DEBUG}.
247      *
248      * @param message to log
249      * @param t log this cause
250      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
251      */
252     @Override
253     public void trace(final Object message, final Throwable t) {
254         debug(message, t);
255     }
256 
257     /**
258      * Logs a message with {@code org.apache.log.Priority.WARN}.
259      *
260      * @param message to log
261      * @see org.apache.commons.logging.Log#warn(Object)
262      */
263     @Override
264     public void warn(final Object message) {
265         if (message != null) {
266             getLogger().warn(String.valueOf(message));
267         }
268     }
269 
270     /**
271      * Logs a message with {@code org.apache.log.Priority.WARN}.
272      *
273      * @param message to log
274      * @param t log this cause
275      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
276      */
277     @Override
278     public void warn(final Object message, final Throwable t) {
279         if (message != null) {
280             getLogger().warn(String.valueOf(message), t);
281         }
282     }
283 }