mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-07 22:44:49 +00:00
ICU-6710 Add ICULogger class to ICU4J.
X-SVN-Rev: 26488
This commit is contained in:
parent
48294075bd
commit
be4903380b
1 changed files with 152 additions and 0 deletions
152
icu4j/main/classes/core/src/com/ibm/icu/impl/ICULogger.java
Normal file
152
icu4j/main/classes/core/src/com/ibm/icu/impl/ICULogger.java
Normal file
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
*******************************************************************************
|
||||
* Copyright (C) 2009, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
package com.ibm.icu.impl;
|
||||
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
*
|
||||
* Extends the Java Logger class adding a method to turn off/on logging.
|
||||
* Classes where logging is wanted contains a static ICULogger object
|
||||
* with logging turned off by default unless the system property
|
||||
* "icu4j.debug.logging" is set to "all"
|
||||
*
|
||||
* If "icu4j.debug.logging" is not set to "all", then the individual loggers needs
|
||||
* to be turned on manually. (e.g. TimeZone.TimeZoneLogger.turnLoggingOn())
|
||||
* <p>
|
||||
* To use logging, the system property "icu4j.debug.logging" must be set to "on" or "all",
|
||||
* otherwise the static ICULogger object will be null. This will help lower any unneccessary
|
||||
* resource usage when logging is not desired.
|
||||
*
|
||||
* @author Michael Ow
|
||||
* @draft ICU 4.4
|
||||
*/
|
||||
|
||||
public class ICULogger extends Logger {
|
||||
private static enum LOGGER_STATUS { ON, OFF, NULL };
|
||||
private static final String GLOBAL_FLAG_TURN_ON_LOGGING = "all";
|
||||
private static final String SYSTEM_PROP_LOGGER = "icu4j.debug.logging";
|
||||
|
||||
private LOGGER_STATUS currentStatus;
|
||||
|
||||
/**
|
||||
* ICULogger constructor that calls the parent constructor with the desired parameters.
|
||||
*/
|
||||
private ICULogger(String name, String resourceBundleName) {
|
||||
super(name, resourceBundleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the status to either on or off. Set the level of the logger to INFO.
|
||||
*/
|
||||
private void setStatus(LOGGER_STATUS newStatus) {
|
||||
if (currentStatus != newStatus) {
|
||||
/* Default to level INFO */
|
||||
if (currentStatus == LOGGER_STATUS.OFF && newStatus == LOGGER_STATUS.ON) {
|
||||
this.setLevel(Level.INFO);
|
||||
}
|
||||
|
||||
currentStatus = newStatus;
|
||||
|
||||
if (currentStatus == LOGGER_STATUS.OFF){
|
||||
this.setLevel(Level.OFF);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the system property SYSTEM_PROP_LOGGER to see if it is set.
|
||||
* return true if it is otherwise return false.
|
||||
*/
|
||||
private static LOGGER_STATUS checkGlobalLoggingFlag() {
|
||||
String prop = System.getProperty(SYSTEM_PROP_LOGGER);
|
||||
|
||||
if (prop != null) {
|
||||
if (prop.equals(GLOBAL_FLAG_TURN_ON_LOGGING)) {
|
||||
return LOGGER_STATUS.ON;
|
||||
}
|
||||
return LOGGER_STATUS.OFF;
|
||||
}
|
||||
|
||||
return LOGGER_STATUS.NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new ICULogger object with logging turned off by default.
|
||||
*
|
||||
* @param name to be use by the logger (usually is the class name)
|
||||
* @return a new ICULogger object
|
||||
* @draft ICU 4.4
|
||||
*/
|
||||
public static ICULogger getICULogger(String name) {
|
||||
return getICULogger(name, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new ICULogger object with logging turned off by default
|
||||
* unless the system property "icu4j.debug.logging" is set to "all"
|
||||
*
|
||||
* @param name to be use by the logger (usually is the class name)
|
||||
* @param ResourceBundle name to localize messages (can be null)
|
||||
* @return a new ICULogger object
|
||||
* @draft ICU 4.4
|
||||
*/
|
||||
public static ICULogger getICULogger(String name, String resourceBundleName) {
|
||||
LOGGER_STATUS flag = checkGlobalLoggingFlag();
|
||||
if (flag != LOGGER_STATUS.NULL) {
|
||||
ICULogger logger = new ICULogger(name, resourceBundleName);
|
||||
|
||||
/* Add a default handler to logger*/
|
||||
logger.addHandler(new ConsoleHandler());
|
||||
|
||||
/* Turn off logging by default unless SYSTEM_PROP_LOGGER property is set to "all" */
|
||||
if (flag == LOGGER_STATUS.ON) {
|
||||
logger.turnOnLogging();
|
||||
} else {
|
||||
logger.turnOffLogging();
|
||||
}
|
||||
|
||||
return logger;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determined if logging is turned on or off. The return value is true if logging is on.
|
||||
*
|
||||
* @return whether logging is turned on or off.
|
||||
* @draft ICU 4.4
|
||||
*/
|
||||
public boolean isLoggingOn() {
|
||||
if (currentStatus == LOGGER_STATUS.ON) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn logging on.
|
||||
*
|
||||
* @draft ICU 4.4
|
||||
*/
|
||||
public void turnOnLogging() {
|
||||
setStatus(LOGGER_STATUS.ON);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn logging off.
|
||||
*
|
||||
* @draft ICU 4.4
|
||||
*/
|
||||
public void turnOffLogging() {
|
||||
setStatus(LOGGER_STATUS.OFF);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue