DefaultLog.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * ---------------
  28. * DefaultLog.java
  29. * ---------------
  30. * (C) Copyright 2004, by Object Refinery Limited.
  31. *
  32. * Original Author: Thomas Morgner;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: DefaultLog.java,v 1.9 2006/02/19 21:10:48 taqua Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 07-Jun-2004 : Added JCommon header (DG);
  40. *
  41. */
  42. package org.jfree.base.log;
  43. import org.jfree.util.Log;
  44. import org.jfree.util.LogTarget;
  45. import org.jfree.util.PrintStreamLogTarget;
  46. /**
  47. * A default log implementation. The Log class defines how to create Logger-contexts
  48. * and how to forward messages to the logtargets.
  49. *
  50. * @author Thomas Morgner
  51. */
  52. public class DefaultLog extends Log {
  53. /** The default log target. */
  54. private static final PrintStreamLogTarget DEFAULT_LOG_TARGET =
  55. new PrintStreamLogTarget();
  56. /** The default log instance. */
  57. private static final DefaultLog defaultLogInstance;
  58. /**
  59. * Creates a new log.
  60. */
  61. protected DefaultLog () {
  62. // nothing required
  63. }
  64. static {
  65. defaultLogInstance = new DefaultLog();
  66. defaultLogInstance.addTarget(DEFAULT_LOG_TARGET);
  67. try {
  68. // check the system property. This is the developers backdoor to activate
  69. // debug output as soon as possible.
  70. final String property = System.getProperty("org.jfree.DebugDefault", "false");
  71. if (Boolean.valueOf(property).booleanValue()) {
  72. defaultLogInstance.setDebuglevel(LogTarget.DEBUG);
  73. }
  74. else {
  75. defaultLogInstance.setDebuglevel(LogTarget.WARN);
  76. }
  77. }
  78. catch (SecurityException se) {
  79. defaultLogInstance.setDebuglevel(LogTarget.WARN);
  80. }
  81. }
  82. /**
  83. * Initializes the log system after the log module was loaded and a log target
  84. * was defined. This is the second step of the log initialisation.
  85. */
  86. public void init() {
  87. removeTarget(DEFAULT_LOG_TARGET);
  88. final String logLevel = LogConfiguration.getLogLevel();
  89. if (logLevel.equalsIgnoreCase("error")) {
  90. setDebuglevel(LogTarget.ERROR);
  91. }
  92. else if (logLevel.equalsIgnoreCase("warn")) {
  93. setDebuglevel(LogTarget.WARN);
  94. }
  95. else if (logLevel.equalsIgnoreCase("info")) {
  96. setDebuglevel(LogTarget.INFO);
  97. }
  98. else if (logLevel.equalsIgnoreCase("debug")) {
  99. setDebuglevel(LogTarget.DEBUG);
  100. }
  101. }
  102. /**
  103. * Adds a log target to this facility. Log targets get informed, via the
  104. * LogTarget interface, whenever a message is logged with this class.
  105. *
  106. * @param target the target.
  107. */
  108. public synchronized void addTarget(final LogTarget target)
  109. {
  110. super.addTarget(target);
  111. // as soon as there is a real log target added, we do no longer need
  112. // the default logging. This was only installed to be able to send messages
  113. // if the deepest basic logging failed.
  114. if (target != DEFAULT_LOG_TARGET) {
  115. removeTarget(DEFAULT_LOG_TARGET);
  116. }
  117. }
  118. /**
  119. * Returns the default log.
  120. *
  121. * @return The default log.
  122. */
  123. public static DefaultLog getDefaultLog() {
  124. return defaultLogInstance;
  125. }
  126. /**
  127. * Makes this implementation the default instance.
  128. */
  129. public static void installDefaultLog () {
  130. Log.defineLog(defaultLogInstance);
  131. }
  132. }