Oracle Java ME Embedded

com.oracle.util.logging
Class Logger

java.lang.Object
  extended by com.oracle.util.logging.Logger

public class Logger
extends java.lang.Object

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.swing. In addition it is possible to create "anonymous" Loggers that are not stored in the Logger namespace.

Logger objects may be obtained by calls on one of the getLogger factory methods. These will either create a new Logger or return a suitable existing Logger. It is important to note that the Logger returned by one of the getLogger factory methods may be garbage collected at any time if a strong reference to the Logger is not kept.

Logging messages will be forwarded to registered Handler objects, which can forward the messages to a variety of destinations, including consoles, files, OS logs, etc.

Each Logger has a "Level" associated with it. This reflects a minimum Level that this logger cares about. If a Logger's level is set to null, then its effective level is inherited from its parent, which may in turn obtain it recursively from its parent, and so on up the tree.

The log level can be dynamically changed by calls on the Logger.setLevel method.

On each logging call the Logger initially performs a cheap check of the request level (e.g., SEVERE or FINE) against the effective log level of the logger. If the request level is lower than the log level, the logging call returns immediately.

After passing this initial (cheap) test, the Logger will allocate a LogRecord to describe the logging message. It will then call a Filter (if present) to do a more detailed check on whether the record should be published. If that passes it will then publish the LogRecord to its output Handlers.

The CLDC version of this class does not support Resource bundles.

Formatting is the responsibility of the output Handler, which will typically call a Formatter.

Note that formatting need not occur synchronously. It may be delayed until a LogRecord is actually written to an external sink.

The logging methods are grouped in the following main categories:

For the methods that do not take an explicit source name and method name, the Logging framework will make a "best effort" to determine which class and method called into the logging method. However, it is important to realize that this automatically inferred information may only be approximate (or may even be quite wrong!). Virtual machines are allowed to do extensive optimizations when JITing and may entirely remove stack frames, making it impossible to reliably locate the calling class and method.

All methods on Logger are multi-thread safe.

Subclassing Information: Note that a LogManager class may provide its own implementation of named Loggers for any point in the namespace. Therefore, any subclasses of Logger (unless they are implemented in conjunction with a new LogManager class) should take care to obtain a Logger instance from the LogManager class and should delegate operations such as "isLoggable" and "log(LogRecord)" to that instance. Note that in order to intercept all logging output, subclasses need only override the log(LogRecord) method. All the other logging methods are implemented as calls on this log(LogRecord) method.


Field Summary
static java.lang.String GLOBAL_LOGGER_NAME
          GLOBAL_LOGGER_NAME is a name for the global logger.
 
Method Summary
 void addHandler(Handler handler)
          Add a log Handler to receive logging messages.
static Logger getAnonymousLogger()
          Create an anonymous Logger.
 Filter getFilter()
          Get the current filter for this Logger.
static Logger getGlobal()
          Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
 Handler[] getHandlers()
          Get the Handlers associated with this logger.
 Level getLevel()
          Get the log Level that has been specified for this Logger.
 java.lang.String getName()
          Get the name for this logger.
 boolean isLoggable(Level level)
          Check if a message of the given level would actually be logged by this logger.
 void log(Level level, java.lang.String msg)
          Log a message, with no arguments.
 void log(Level level, java.lang.String msg, java.lang.Object param1)
          Log a message, with one object parameter.
 void log(Level level, java.lang.String msg, java.lang.Object[] params)
          Log a message, with an array of object arguments.
 void log(Level level, java.lang.String msg, java.lang.Throwable thrown)
          Log a message, with associated Throwable information.
 void log(LogRecord record)
          Log a LogRecord.
 void logp(Level level, java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg)
          Log a message, specifying source class and method, with no arguments.
 void logp(Level level, java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg, java.lang.Object param1)
          Log a message, specifying source class and method, with a single object parameter to the log message.
 void logp(Level level, java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg, java.lang.Object[] params)
          Log a message, specifying source class and method, with an array of object arguments.
 void logp(Level level, java.lang.String sourceClass, java.lang.String sourceMethod, java.lang.String msg, java.lang.Throwable thrown)
          Log a message, specifying source class and method, with associated Throwable information.
 void removeHandler(Handler handler)
          Remove a log Handler.
 void setFilter(Filter newFilter)
          Set a filter to control output on this Logger.
 void setLevel(Level newLevel)
          Set the log level specifying which message levels will be logged by this logger.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GLOBAL_LOGGER_NAME

public static final java.lang.String GLOBAL_LOGGER_NAME
GLOBAL_LOGGER_NAME is a name for the global logger.

Since:
1.6
See Also:
Constant Field Values
Method Detail

getGlobal

public static final Logger getGlobal()
Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.

Returns:
global logger object
Since:
1.7

getAnonymousLogger

public static Logger getAnonymousLogger()
Create an anonymous Logger. There will be no access checks on updates to the logger.

Even although the new logger is anonymous, it is configured to have the root logger ("") as its parent. This means that by default it inherits its effective level and handlers from the root logger.

Returns:
a newly created private Logger

setFilter

public void setFilter(Filter newFilter)
               throws java.lang.SecurityException
Set a filter to control output on this Logger.

After passing the initial "level" check, the Logger will call this Filter to check if a log record should really be published.

Parameters:
newFilter - a filter object (may be null)
Throws:
java.lang.SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").

getFilter

public Filter getFilter()
Get the current filter for this Logger.

Returns:
a filter object (may be null)

log

public void log(LogRecord record)
Log a LogRecord.

All the other logging methods in this class call through this method to actually perform any logging. Subclasses can override this single method to capture all log activity.

Parameters:
record - the LogRecord to be published

log

public void log(Level level,
                java.lang.String msg)
Log a message, with no arguments.

If the logger is currently enabled for the given message level then the given message is forwarded to all the registered output Handler objects.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
msg - The string message (or a key in the message catalog)

log

public void log(Level level,
                java.lang.String msg,
                java.lang.Object param1)
Log a message, with one object parameter.

If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
msg - The string message (or a key in the message catalog)
param1 - parameter to the message

log

public void log(Level level,
                java.lang.String msg,
                java.lang.Object[] params)
Log a message, with an array of object arguments.

If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
msg - The string message (or a key in the message catalog)
params - array of parameters to the message

log

public void log(Level level,
                java.lang.String msg,
                java.lang.Throwable thrown)
Log a message, with associated Throwable information.

If the logger is currently enabled for the given message level then the given arguments are stored in a LogRecord which is forwarded to all registered output handlers.

Note that the thrown argument is stored in the LogRecord thrown property, rather than the LogRecord parameters property. Thus is it processed specially by output Formatters and is not treated as a formatting parameter to the LogRecord message property.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
msg - The string message (or a key in the message catalog)
thrown - Throwable associated with log message.

logp

public void logp(Level level,
                 java.lang.String sourceClass,
                 java.lang.String sourceMethod,
                 java.lang.String msg)
Log a message, specifying source class and method, with no arguments.

If the logger is currently enabled for the given message level then the given message is forwarded to all the registered output Handler objects.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
sourceClass - name of class that issued the logging request
sourceMethod - name of method that issued the logging request
msg - The string message (or a key in the message catalog)

logp

public void logp(Level level,
                 java.lang.String sourceClass,
                 java.lang.String sourceMethod,
                 java.lang.String msg,
                 java.lang.Object param1)
Log a message, specifying source class and method, with a single object parameter to the log message.

If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
sourceClass - name of class that issued the logging request
sourceMethod - name of method that issued the logging request
msg - The string message (or a key in the message catalog)
param1 - Parameter to the log message.

logp

public void logp(Level level,
                 java.lang.String sourceClass,
                 java.lang.String sourceMethod,
                 java.lang.String msg,
                 java.lang.Object[] params)
Log a message, specifying source class and method, with an array of object arguments.

If the logger is currently enabled for the given message level then a corresponding LogRecord is created and forwarded to all the registered output Handler objects.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
sourceClass - name of class that issued the logging request
sourceMethod - name of method that issued the logging request
msg - The string message (or a key in the message catalog)
params - Array of parameters to the message

logp

public void logp(Level level,
                 java.lang.String sourceClass,
                 java.lang.String sourceMethod,
                 java.lang.String msg,
                 java.lang.Throwable thrown)
Log a message, specifying source class and method, with associated Throwable information.

If the logger is currently enabled for the given message level then the given arguments are stored in a LogRecord which is forwarded to all registered output handlers.

Note that the thrown argument is stored in the LogRecord thrown property, rather than the LogRecord parameters property. Thus is it processed specially by output Formatters and is not treated as a formatting parameter to the LogRecord message property.

Parameters:
level - One of the message level identifiers, e.g., SEVERE
sourceClass - name of class that issued the logging request
sourceMethod - name of method that issued the logging request
msg - The string message (or a key in the message catalog)
thrown - Throwable associated with log message.

setLevel

public void setLevel(Level newLevel)
              throws java.lang.SecurityException
Set the log level specifying which message levels will be logged by this logger. Message levels lower than this value will be discarded. The level value Level.OFF can be used to turn off logging.

If the new level is null, it means that this node should inherit its level from its nearest ancestor with a specific (non-null) level value.

Parameters:
newLevel - the new value for the log level (may be null)
Throws:
java.lang.SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").

getLevel

public Level getLevel()
Get the log Level that has been specified for this Logger.

Returns:
this Logger's level

isLoggable

public boolean isLoggable(Level level)
Check if a message of the given level would actually be logged by this logger. This check is based on the Loggers effective level.

Parameters:
level - a message logging level
Returns:
true if the given message level is currently being logged.

getName

public java.lang.String getName()
Get the name for this logger.

Returns:
logger name. Will be null for anonymous Loggers.

addHandler

public void addHandler(Handler handler)
                throws java.lang.SecurityException
Add a log Handler to receive logging messages.

Typically the root Logger is configured with a set of Handlers that essentially act as default handlers for all loggers.

Parameters:
handler - a logging Handler
Throws:
java.lang.SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").

removeHandler

public void removeHandler(Handler handler)
                   throws java.lang.SecurityException
Remove a log Handler.

Returns silently if the given Handler is not found or is null

Parameters:
handler - a logging Handler
Throws:
java.lang.SecurityException - if a security manager exists and if the caller does not have LoggingPermission("control").

getHandlers

public Handler[] getHandlers()
Get the Handlers associated with this logger.

Returns:
an array of all registered Handlers

Oracle Java ME Embedded

Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.