2 *******************************************************************************
\r
3 * Copyright (C) 2009-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.impl;
\r
9 import java.util.logging.ConsoleHandler;
\r
10 import java.util.logging.Level;
\r
11 import java.util.logging.Logger;
\r
15 * Extends the Java Logger class adding a method to turn off/on logging.
\r
16 * Classes where logging is wanted contains a static ICULogger object
\r
17 * with logging turned off by default unless the system property
\r
18 * "icu4j.debug.logging" is set to "all"
\r
20 * If "icu4j.debug.logging" is not set to "all", then the individual loggers needs
\r
21 * to be turned on manually. (e.g. TimeZone.TimeZoneLogger.turnLoggingOn())
\r
23 * To use logging, the system property "icu4j.debug.logging" must be set to "on" or "all",
\r
24 * otherwise the static ICULogger object will be null. This will help lower any unneccessary
\r
25 * resource usage when logging is not desired.
\r
27 * <strong>Examples</strong>:<P>
\r
31 * public class Class {
\r
32 * // Create logger object (usually with the class name)
\r
33 * public static ICULogger ClassLogger = ICULogger.getICULogger(Class.class.getName());
\r
35 * // Method that will use logger.
\r
36 * public boolean hasSomething(Object obj) {
\r
37 * if (obj == null) {
\r
38 * // Log that obj is null.
\r
39 * // Note: Good to check for null and if logging is turned on to minimize resource usage when logging is not needed.
\r
40 * if (ClassLogger != null && ClassLogger.isLoggingOn()) {
\r
41 * ClassLogger.warning("obj is null so false was returned by default.");
\r
52 * Turning on logging (using the default settings)
\r
55 * java -Dicu4j.debug.logging=all program
\r
60 public class ICULogger extends Logger {
\r
61 private static enum LOGGER_STATUS { ON, OFF, NULL };
\r
62 private static final String GLOBAL_FLAG_TURN_ON_LOGGING = "all";
\r
63 private static final String SYSTEM_PROP_LOGGER = "icu4j.debug.logging";
\r
65 private LOGGER_STATUS currentStatus;
\r
68 * ICULogger constructor that calls the parent constructor with the desired parameters.
\r
70 private ICULogger(String name, String resourceBundleName) {
\r
71 super(name, resourceBundleName);
\r
75 * Set the status to either on or off. Set the level of the logger to INFO.
\r
77 private void setStatus(LOGGER_STATUS newStatus) {
\r
78 if (currentStatus != newStatus) {
\r
79 /* Default to level INFO */
\r
80 if (currentStatus == LOGGER_STATUS.OFF && newStatus == LOGGER_STATUS.ON) {
\r
81 this.setLevel(Level.INFO);
\r
84 currentStatus = newStatus;
\r
86 if (currentStatus == LOGGER_STATUS.OFF){
\r
87 this.setLevel(Level.OFF);
\r
93 * Check the system property SYSTEM_PROP_LOGGER to see if it is set.
\r
94 * return true if it is otherwise return false.
\r
96 private static LOGGER_STATUS checkGlobalLoggingFlag() {
\r
98 String prop = System.getProperty(SYSTEM_PROP_LOGGER);
\r
100 if (prop != null) {
\r
101 if (prop.equals(GLOBAL_FLAG_TURN_ON_LOGGING)) {
\r
102 return LOGGER_STATUS.ON;
\r
104 return LOGGER_STATUS.OFF;
\r
106 } catch (SecurityException e) {
\r
107 // Ignore the security exception and fall-through
\r
110 return LOGGER_STATUS.NULL;
\r
114 * Instantiates a new ICULogger object with logging turned off by default.
\r
116 * @param name to be use by the logger (usually is the class name)
\r
117 * @return a new ICULogger object
\r
119 * @provisional This API might change or be removed in a future release.
\r
121 public static ICULogger getICULogger(String name) {
\r
122 return getICULogger(name, null);
\r
126 * Instantiates a new ICULogger object with logging turned off by default
\r
127 * unless the system property "icu4j.debug.logging" is set to "all"
\r
129 * @param name to be use by the logger (usually is the class name)
\r
130 * @param resourceBundleName name to localize messages (can be null)
\r
131 * @return a new ICULogger object
\r
133 * @provisional This API might change or be removed in a future release.
\r
135 public static ICULogger getICULogger(String name, String resourceBundleName) {
\r
136 LOGGER_STATUS flag = checkGlobalLoggingFlag();
\r
137 if (flag != LOGGER_STATUS.NULL) {
\r
138 ICULogger logger = new ICULogger(name, resourceBundleName);
\r
140 /* Add a default handler to logger*/
\r
141 logger.addHandler(new ConsoleHandler());
\r
143 /* Turn off logging by default unless SYSTEM_PROP_LOGGER property is set to "all" */
\r
144 if (flag == LOGGER_STATUS.ON) {
\r
145 logger.turnOnLogging();
\r
147 logger.turnOffLogging();
\r
156 * Determined if logging is turned on or off. The return value is true if logging is on.
\r
158 * @return whether logging is turned on or off.
\r
160 * @provisional This API might change or be removed in a future release.
\r
162 public boolean isLoggingOn() {
\r
163 if (currentStatus == LOGGER_STATUS.ON) {
\r
174 * @provisional This API might change or be removed in a future release.
\r
176 public void turnOnLogging() {
\r
177 setStatus(LOGGER_STATUS.ON);
\r
181 * Turn logging off.
\r
184 * @provisional This API might change or be removed in a future release.
\r
186 public void turnOffLogging() {
\r
187 setStatus(LOGGER_STATUS.OFF);
\r