ch.javasoft.util.logging
Class Loggers

java.lang.Object
  extended by ch.javasoft.util.logging.Loggers

public class Loggers
extends Object

Loggers is a class with some static convenience methods to use java logging.

Log configuration is read from a properties file, "ch/javasoft/logging/Loggers.properties" by default if no other files or config classes are registered (see LogManager for details).


Method Summary
static Properties getDefaultConfigurationAsProperties()
          Returns the default configuration for the log manager, see getDefaultConfigurationName(), but as properties object.
static String getDefaultConfigurationName()
          Returns the file name of the default configuration, which is ch/javasoft/util/logging/Loggers.properties
static Logger getLogger(Class clazz, int parts)
          Creates a new logger for the given class.
static Logger getLogger(String groupName)
          Creates a group logger for the group given by name.
static Logger getLogger(String name, int parts)
          Creates a new logger for the given package.
static Logger getLoggerForPackage(Package pkg, int parts)
          Deprecated. Does not work with MATLAB 7.1.0.246 (R14) Service Pack 3
static Logger getRootLogger()
          Returns the root logger, which is at the root of the parent hierarchy of all group loggers.
static void initLogManagerConfiguration(Properties config)
          Initializes the logger configuration with the given configuration properties.
static boolean isLoggable(Logger logger, Level level)
          Returns true if Logger.isLoggable(Level) returns true, the the logger's filter (if any) would log a record with the given log level and if any of the installed handlers would log that record.
static void logToConsole(Level level)
          Takes the default configuration and overrides .level with the specified log level handlers with a standard err and out handler The log manager is initLogManagerConfiguration(Properties) with the resulting properties.
static void logToFile(File file, Level level)
          Takes the default configuration and overrides .level with the specified log level handlers with a standard file handler java.util.logging.FileHandler.pattern with the specified log file The log manager is initLogManagerConfiguration(Properties) with the resulting properties.
static void logToFile(String fileName, Level level)
          Takes the default configuration and overrides .level with the specified log level handlers with a standard file handler java.util.logging.FileHandler.pattern with the specified log file The log manager is initLogManagerConfiguration(Properties) with the resulting properties.
static void removeAllHandlers(Logger logger, boolean close)
          Removes all handlers of the given logger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getRootLogger

public static Logger getRootLogger()
Returns the root logger, which is at the root of the parent hierarchy of all group loggers. It contains the default handlers for other loggers, if no group specific logging is desired.

This is the same as calling Logger.getLogger(String) with an empty string as logger name. This is a convenience method, which also ensures that configuration stuff is read before returning the root logger.


getLoggerForPackage

public static Logger getLoggerForPackage(Package pkg,
                                         int parts)
Deprecated. Does not work with MATLAB 7.1.0.246 (R14) Service Pack 3

Creates a new logger for the given package.

Parameters:
pkg - the package
parts - use 0 for whole package name, positive values for package parts from start (left), negative values for package parts from end (right)
Returns:
the desired logger instance

getLogger

public static Logger getLogger(Class clazz,
                               int parts)
Creates a new logger for the given class.

Parameters:
clazz - the class
parts - use 0 for whole package name, positive values for package parts from start (left), negative values for package parts from end (right)
Returns:
the desired logger instance

getLogger

public static Logger getLogger(String name,
                               int parts)
Creates a new logger for the given package.

Parameters:
name - the package (or logger) name
parts - use 0 for whole package name, positive values for package parts from start (left), negative values for package parts from end (right)
Returns:
the desired logger instance

getLogger

public static Logger getLogger(String groupName)
Creates a group logger for the group given by name.

Parameters:
groupName - the name of the log group
Returns:
the desired logger instance

logToFile

public static void logToFile(File file,
                             Level level)
                      throws IOException
Takes the default configuration and overrides The log manager is initLogManagerConfiguration(Properties) with the resulting properties.

According to the default configuration properties, a LogFormatter is used by default.

Parameters:
file - The file to log to
level - The desired log level
Throws:
SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").
IOException - if there are problems reading the properties
See Also:
LogManager.readConfiguration(InputStream)

logToFile

public static void logToFile(String fileName,
                             Level level)
                      throws IOException
Takes the default configuration and overrides The log manager is initLogManagerConfiguration(Properties) with the resulting properties.

According to the default configuration properties, a LogFormatter is used by default.

Parameters:
fileName - The file to log to
level - The desired log level
Throws:
SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").
IOException - if there are problems reading the properties
See Also:
LogManager.readConfiguration(InputStream)

logToConsole

public static void logToConsole(Level level)
                         throws SecurityException,
                                IOException
Takes the default configuration and overrides The log manager is initLogManagerConfiguration(Properties) with the resulting properties.

According to the default configuration properties, WARNING and more severe messages are now logged on the standard error stream, everything else on the standard output (if at least as severe as the specified level). For formatting, a LogFormatter is used by default.

Parameters:
level - The desired log level
Throws:
SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").
IOException - if there are problems reading the properties
See Also:
LogManager.readConfiguration(InputStream)

isLoggable

public static boolean isLoggable(Logger logger,
                                 Level level)
Returns true if Logger.isLoggable(Level) returns true, the the logger's filter (if any) would log a record with the given log level and if any of the installed handlers would log that record.

Parameters:
logger - the logger to test loggability for
level - the level to check
Returns:
true if the logger logs records for the given level

removeAllHandlers

public static void removeAllHandlers(Logger logger,
                                     boolean close)
Removes all handlers of the given logger

Parameters:
logger - the logger of which all handlers should be removed
close - specifies whether the removed handlers should be closed

getDefaultConfigurationName

public static String getDefaultConfigurationName()
Returns the file name of the default configuration, which is ch/javasoft/util/logging/Loggers.properties


initLogManagerConfiguration

public static void initLogManagerConfiguration(Properties config)
                                        throws SecurityException,
                                               IOException
Initializes the logger configuration with the given configuration properties. Calls LogManager.readConfiguration(InputStream).

Parameters:
config - the configuration values
Throws:
SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").
IOException - if there are problems reading the properties
See Also:
LogManager.readConfiguration(InputStream)

getDefaultConfigurationAsProperties

public static Properties getDefaultConfigurationAsProperties()
                                                      throws IOException
Returns the default configuration for the log manager, see getDefaultConfigurationName(), but as properties object. The properties may be changed and reapplied as logger configuration by calling initLogManagerConfiguration(Properties).

Throws:
IOException