java.util.logging.Logger
Note: All methods on Logger are multi-thread safe.
Getting a Logger instance
Get a Logger instance through one of the getLogger factory methods. These will either create a new Logger or return a suitable existing Logger.
Examples:
Get a Logger instance through one of the getLogger factory methods. These will either create a new Logger or return a suitable existing Logger.
Examples:
Logger logger = Logger.getLogger("myLogger"); Logger.getName(); //will return "myLogger"
Logger naming and inheritance tree
Loggers are normally named using a hierarchical dot-separated namespace and each Logger keeps track of a "parent" Logger, which is its nearest existing ancestor in the Logger namespace.
So Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component,
Loggers are normally named using a hierarchical dot-separated namespace and each Logger keeps track of a "parent" Logger, which is its nearest existing ancestor in the Logger namespace.
So Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component,
Logger logger = Logger.getLogger(MyClass.class.getName());
Logger level
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.
A null level will inherit its effective level from its parent.
java.util.logging.Level: The Level class defines a set of standard logging levels that can be used to control logging output. The logging Level objects are ordered and are specified by ordered integers. Enabling logging at a given level also enables logging at all higher levels.
Clients should normally use the predefined Level constants such as Level.SEVERE.
The levels in descending order are:
In addition there is also:
2 ways of setting the logger level:
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.
But it the loggin call passes 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. By default, loggers also publish to their parent's Handlers, recursively up the tree.
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.
A null level will inherit its effective level from its parent.
java.util.logging.Level: The Level class defines a set of standard logging levels that can be used to control logging output. The logging Level objects are ordered and are specified by ordered integers. Enabling logging at a given level also enables logging at all higher levels.
Clients should normally use the predefined Level constants such as Level.SEVERE.
The levels in descending order are:
- SEVERE (highest value)
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST (lowest value)
In addition there is also:
- level OFF that can be used to turn off logging;
- level ALL that can be used to enable logging of all messages;
2 ways of setting the logger level:
- dinamically, ex.:
logger.setLevel(Level.FINER);
- based on the properties from the logging configuration file, as described in the description of the LogManager class. See an example here.
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.
But it the loggin call passes 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. By default, loggers also publish to their parent's Handlers, recursively up the tree.
Logging methods
grouped in five main categories:
grouped in five main categories:
- There are a set of "log" methods that take a log level, a message string, and optionally some parameters to the message string.
ex:Throwable ex = new IllegalArgumentException("Some exception text"); Logger.log(Level.SEVERE, "Some message", ex);
- There are a set of "logp" methods (for "log precise") that are like the "log" methods, but also take an explicit source class name and method name.
- There are a set of "logrb" method (for "log with resource bundle") that are like the "logp" method, but also take an explicit resource bundle name for use in localizing the log message.
- There are convenience methods for tracing method entries (the "entering" methods), method returns (the "exiting" methods) and throwing exceptions (the "throwing" methods).
- Finally, there are a set of convenience methods for use in the very simplest cases, when a developer simply wants to log a simple string at a given log level. These methods are named after the standard Level names ("severe", "warning", "info", etc.) and take a single argument, a message string.
ex:Logger.finest("this is finest"); Logger.finer("this is finer"); Logger.fine("this is fine"); Logger.config("this is config"); Logger.info("this is info"); Logger.warning("this is a warning"); Logger.severe("this is severe");
Formatter (pure txt or xml)
JavaSE includes two standard Formatters:
JavaSE includes two standard Formatters:
- SimpleFormatter: Writes brief "human-readable" summaries of log records.
- XMLFormatter: Writes detailed XML-structured information.
class MyClass { public void myMethod() { // Get a logger Logger logger = Logger.getLogger("com.mycompany"); // Create a new handler that uses the simple formatter try { FileHandler fh = new FileHandler("mylog.txt"); fh.setFormatter(new SimpleFormatter()); logger.addHandler(fh); } catch (IOException e) { } // Create a new handler that uses the XML formatter try { FileHandler fh = new FileHandler("mylog.xml"); fh.setFormatter(new XMLFormatter()); logger.addHandler(fh); } catch (IOException e) { } // Log a few messages logger.severe("my severe message"); logger.warning("my warning message"); logger.info("my info message"); logger.config("my config message"); logger.fine("my fine message"); logger.finer("my finer message"); logger.finest("my finest message"); } }ref: Setting the Formatter of a Logger Handler
Sem comentários:
Enviar um comentário