Java Logging
Principle advantages
- easy to suppress all logs or just those below a certain level, and just as easy to turn them back on
- Suppressed logs are very cheap, so there is only a minimal penalty for leaving the logging code in your app
- Logs can be directed to different handlers
= Both loggers and handlers can filter records.
- Log records can be formatted in different ways. Example, in plain text or XML
- App can use multiple loggers
- By default, the logging configuration is controlled by a configuration file. App can replace it if desired
To get global logger, Logger.getGloabl()
To find or create a logger, Logger.getLogger("com.mycompany.myapp")
. Logger names(com.mycompnay.myapp) are
hierarchical, and logger parents and children share certain properties, while no semantic relationship between
a package and its parent. If you set the log level on a logger, then the child loggers inherit that level.
Seven logging levels:
- SEVERE
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST
There are logging method for all levels, such as
logger.warning(message);
, logger.info(message)
.
The default logging configuration logs all records with the level of INFO or higher, you can set a different level,
logger.setLevel(Level.FINE);
to log all records with the level of FINE or higher. Also you can use LEVEL.ALL
or LEVEL.OFF
to turn all logging on or off.
Handlers have a logging level. For record to be logged. its logging level must be above the threshold of both
the logger and the handler. The logger sends the records to both the parent handler and its own handlers.
The ultimate ancestor(with name "") of the parent handler has a ConsoleHanlder
which prints records with level
INFO and above to the System.err stream.
Logger logger = Logger.getLogger("com.mycompany.myapp"); logger.setLevel(Level.FINE); logger.setUseParentHandlers(false); //to avoid printing logs twice on console Handler handler = new ConsoleHandler(); handler.setLevel(Level.FINE); logger.addHandler(handler);
To send log records elsewhere, add another handler, like FileHandler and SocketHandler
FileHandler filehandler = new FileHandler(); logger.addHandler(filehandler);
then the records are sent to file javaN.log in the user's home directory, where N is a number to make the file unique. The default behavior of the file handler can be changed by setting parameters in the Log Manager Configuration.
The default Log Manager Configuration locates at jre/lib/logging.properties. To change the default logging level, edit the file and modify the line .level=INFO. To specify the logging level for your own loggers by adding lines such as com.mycompany.myapp.level=FINE. To set the logging level of the default console handler, edit and modify the line java.util.logging.ConsoleHandler.level=INFO.
To use another file as Log Manager Configuration, start your application like java -Djava.util.logging.config.file=configFile MainClass.
Records are filtered according to their logging levels. Each logger and handler can have an optional filter to
perform additional filtering, but you can have at most one filter each time. To define a filter, implement the
Filter
interface and define the method boolean isLoggable(LogRecord record)
. To add a filter, call
setFilter
method on logger or handler.