1. /* $Id: LogUtils.java,v 1.8 2004/05/10 06:44:13 skitching Exp $
  2. *
  3. * Copyright 2003-2004 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * 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. package org.apache.commons.digester.plugins;
  18. import org.apache.commons.digester.Digester;
  19. import org.apache.commons.logging.Log;
  20. /**
  21. * Simple utility class to assist in logging.
  22. * <p>
  23. * This class is intended only for the use of the code in the
  24. * plugins packages. No "user" code should use this package.
  25. * <p>
  26. * The Digester module has an interesting approach to logging:
  27. * all logging should be done via the Log object stored on the
  28. * digester instance that the object *doing* the logging is associated
  29. * with.
  30. * <p>
  31. * This is done because apparently some "container"-type applications
  32. * such as Avalon and Tomcat need to be able to configure different logging
  33. * for different <i>instances</i> of the Digester class which have been
  34. * loaded from the same ClassLoader [info from Craig McClanahan].
  35. * Not only the logging of the Digester instance should be affected; all
  36. * objects associated with that Digester instance should obey the
  37. * reconfiguration of their owning Digester instance's logging. The current
  38. * solution is to force all objects to output logging info via a single
  39. * Log object stored on the Digester instance they are associated with.
  40. * <p>
  41. * Of course this causes problems if logging is attempted before an
  42. * object <i>has</i> a valid reference to its owning Digester. The
  43. * getLogging method provided here resolves this issue by returning a
  44. * Log object which silently discards all logging output in this
  45. * situation.
  46. * <p>
  47. * And it also implies that logging filtering can no longer be applied
  48. * to subcomponents of the Digester, because all logging is done via
  49. * a single Log object (a single Category). C'est la vie...
  50. *
  51. * @since 1.6
  52. */
  53. class LogUtils {
  54. /**
  55. * Get the Log object associated with the specified Digester instance,
  56. * or a "no-op" logging object if the digester reference is null.
  57. * <p>
  58. * You should use this method instead of digester.getLogger() in
  59. * any situation where the digester might be null.
  60. */
  61. static Log getLogger(Digester digester) {
  62. if (digester == null) {
  63. return new org.apache.commons.logging.impl.NoOpLog();
  64. }
  65. return digester.getLogger();
  66. }
  67. }