/*
 * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
 * Copyright (C) 2005 - Javolution (http://javolution.org/)
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software is
 * freely granted, provided that this notice is preserved.
 */
package javolution37.javolution.util;

import javolution37.javolution.lang.Text;
import javolution37.javolution.realtime.LogContext;

import java.lang.CharSequence;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogRecord;

/**
 * 

This class represents a specialized logging context forwarding events * to a standard logger (java.util.logging.Logger).

* *

This class leverages the capabilities of the standard logging facility * and extends it to support specialized {@link LogContext logging} on a * thread or object basis. For example:[code] * StandardLog remoteLog = new StandardLog(Logger.getLogger("remote")); * StandardLog.enter(remoteLog); * try { * StandardLog.fine("Current thread uses a remote logger"); * ... * } finally { * StandardLog.exit(remoteLog); // Reverts to previous logging context. * }[/code]

* * * @author Jean-Marie Dautelle * @version 3.6, November 16, 2005 */ public class StandardLog extends LogContext { /** * The logger associated to this logging context. */ private Logger _logger; /** * Creates a logging context forwarding events to the root logger * (Logger.getLogger("")). */ public StandardLog() { this(Logger.getLogger("")); } /** * Creates a standard log context forwarding events to the specified * logger. * * @param logger the logger to which log events are forwarded to. */ public StandardLog(Logger logger) { _logger = logger; } /** * Returns the logger to which this context forwards the events to. * * @return the logger for this standard logging context. */ public final Logger getLogger() { return _logger; } /** * Checks if a message of the given level would actually be logged * by this logger. * * @param level the message logging level * @return true if a message of specified level would actually * be logged;false otherwise. */ public static boolean isLoggable(Level level) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { return ((StandardLog) log)._logger.isLoggable(level); } else if (level.intValue() >= Level.WARNING.intValue()) { return log.isWarningLogged(); } else if (level.intValue() >= Level.INFO.intValue()) { return log.isInfoLogged(); } else { return false; } } /** * Log a specific LogRecord. If the current logging context is not a * {@link StandardLog}, an {@link LogContext#logError error}, * {@link LogContext#logWarning warning} or * {@link LogContext#logInfo info} is possibly logged. * * @param record the LogRecord to be published. */ public static void log(LogRecord record) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.log(record); } else { Throwable error = record.getThrown(); if (error != null) { log.logError(error, toCharSeq(record.getMessage())); } else if (record.getLevel().intValue() > Level.WARNING.intValue()) { log.logWarning(toCharSeq(record.getMessage())); } else if (record.getLevel().intValue() > Level.INFO.intValue()) { log.logInfo(toCharSeq(record.getMessage())); } } } /** * Logs a {@link Level#SEVERE SEVERE} message. If the current logging * context is not a {@link StandardLog} a {@link LogContext#logWarning * warning} message is logged. * * @param msg the severe message. */ public static void severe(String msg) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.severe(msg); } else { log.logWarning(toCharSeq(msg)); } } /** * Logs a {@link Level#WARNING WARNING} message. * * @param msg the warning message. * @see LogContext#logWarning(CharSequence) */ public static void warning(String msg) { LogContext log = (LogContext) LogContext.current(); log.logWarning(toCharSeq(msg)); } /** * Logs an {@link Level#INFO INFO} message. * * @param msg the informative message. * @see LogContext#logInfo(CharSequence) */ public static void info(String msg) { LogContext log = (LogContext) LogContext.current(); log.logInfo(toCharSeq(msg)); } /** * Logs a {@link Level#CONFIG CONFIG} message. If the current logging * context is not a {@link StandardLog} no message is logged. * * @param msg the config message. */ public static void config(String msg) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.config(msg); } } /** * Logs a {@link Level#FINE FINE} message. If the current logging * context is not a {@link StandardLog} no message is logged. * * @param msg the fine message. */ public static void fine(String msg) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.fine(msg); } } /** * Logs a {@link Level#FINER FINER} message. If the current logging * context is not a {@link StandardLog} no message is logged. * * @param msg the finer message. */ public static void finer(String msg) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.finer(msg); } } /** * Logs a {@link Level#FINEST FINEST} message. If the current logging * context is not a {@link StandardLog} no message is logged. * * @param msg the finest message. */ public static void finest(String msg) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.finest(msg); } } /** * Logs throwing an exception. If the current logging context is not a * {@link StandardLog} an {@link LogContext#logError error} is logged. * * @param sourceClass name of class that issued the logging request. * @param sourceMethod name of the method. * @param thrown the error that is being thrown. */ public static void throwing(String sourceClass, String sourceMethod, Throwable thrown) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.throwing(sourceClass, sourceMethod, thrown); } else { log.logError(thrown, null); } } /** * Log a method entry. If the current logging context is not a * {@link StandardLog} no entry is logged. * * @param sourceClass name of class that issued the logging request. * @param sourceMethod name of method that is being entered. */ public static void entering(String sourceClass, String sourceMethod) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.entering(sourceClass, sourceMethod); } } /** * Log a method return. If the current logging context is not a * {@link StandardLog} no return is logged. * * @param sourceClass name of class that issued the logging request. * @param sourceMethod name of method that is being returned. */ public static void exiting(String sourceClass, String sourceMethod) { LogContext log = (LogContext) LogContext.current(); if (log instanceof StandardLog) { ((StandardLog) log)._logger.exiting(sourceClass, sourceMethod); } } // Implements LogContext abstract method. public boolean isInfoLogged() { return _logger.isLoggable(Level.INFO); } // Implements LogContext abstract method. public boolean isWarningLogged() { return _logger.isLoggable(Level.WARNING); } // Implements LogContext abstract method. public boolean isErrorLogged() { return _logger.isLoggable(Level.SEVERE); } // Implements LogContext abstract method. public void logInfo(CharSequence message) { _logger.info(message.toString()); } // Implements LogContext abstract method. public void logWarning(CharSequence message) { _logger.warning(message.toString()); } // Implements LogContext abstract method. public void logError(Throwable error, CharSequence message) { String msg = (message == null) ? error.getMessage() : message .toString(); msg = (msg == null) ? error.getClass().getName() : error.getClass() .getName() + " - " + msg; _logger.severe(msg); } /** * Converts a String to a CharSequence (for J2ME compatibility) * * @param str the string to convert. * @return the corresponding CharSequence instance. */ private static CharSequence toCharSeq(Object str) { if (str instanceof CharSequence) return (CharSequence) str; return Text.valueOf((String) str); } }

Ads help maintain this website.