View Javadoc

1   package org.apache.commons.digester3.plugins;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.digester3.Digester;
23  import org.apache.commons.logging.Log;
24  
25  /**
26   * Simple utility class to assist in logging.
27   * <p>
28   * This class is intended only for the use of the code in the plugins packages. No "user" code should use this package.
29   * <p>
30   * The Digester module has an interesting approach to logging: all logging should be done via the Log object stored on
31   * the digester instance that the object *doing* the logging is associated with.
32   * <p>
33   * This is done because apparently some "container"-type applications such as Avalon and Tomcat need to be able to
34   * configure different logging for different <i>instances</i> of the Digester class which have been loaded from the same
35   * ClassLoader [info from Craig McClanahan]. Not only the logging of the Digester instance should be affected; all
36   * objects associated with that Digester instance should obey the reconfiguration of their owning Digester instance's
37   * logging. The current solution is to force all objects to output logging info via a single Log object stored on the
38   * Digester instance they are associated with.
39   * <p>
40   * Of course this causes problems if logging is attempted before an object <i>has</i> a valid reference to its owning
41   * Digester. The getLogging method provided here resolves this issue by returning a Log object which silently discards
42   * all logging output in this situation.
43   * <p>
44   * And it also implies that logging filtering can no longer be applied to subcomponents of the Digester, because all
45   * logging is done via a single Log object (a single Category). C'est la vie...
46   *
47   * @since 1.6
48   */
49  class LogUtils
50  {
51  
52      /**
53       * Get the Log object associated with the specified Digester instance, or a "no-op" logging object if the digester
54       * reference is null.
55       * <p>
56       * You should use this method instead of digester.getLogger() in any situation where the digester might be null.
57       */
58      static Log getLogger( Digester digester )
59      {
60          if ( digester == null )
61          {
62              return new org.apache.commons.logging.impl.NoOpLog();
63          }
64  
65          return digester.getLogger();
66      }
67  
68  }