Trace: » rotaryslicer » jai » cdm » neurospy » playground » nodes » devel » imagej » motion » logging
Java Logging
A logger allows you to do what System.out.println(“i arrived at this line of code and crashed”) lets you do, but it's much better.
The logger lets you
* set the priority, or strength of the log entry
* look at the log at your own pace instead of on the output of the NetBeans IDE
* you can easily keep the log after you've closed the IDE
* you can turn off logging from the command line
Logging levels
A logger has a certain minimum level it is interested in. It will NOT log anything (even if you ask it) below that level.
FINEST (use for debugging)
FINER (use for debugging)
FINE (use for debugging)
CONFIG
INFO
WARNING
SEVERE
What's happening?
You tell a logger to log a message. The logger checks that the message is important enough, and then calls a Filter to check in more detail. If the message passes, the logger creates a LogRecord which has additional information about the source of the message. The logger then sends this LogRecord to its Handler(s) and also to its parent logger. The Handler(s) sent the message to a Formatter and then publish the formatted version.
If you want, you can use a ResourceBundle associated with your logger, so instead of passing it a message you pass it a localization key, which can also be a string but the ResourceBundle has a mapping between localization key strings and other strings. You can use this for easy Internationalization, perhaps especially for important errors.
java.util.logging :)
is the package that has all the logging goodies.
Making and Using your own logger
You can make a logger like this :
private static final Logger my_precious_little_logger = Logger.getLogger("the name of my logger");
You have to decide what logger to log to in any particular point. You could just make a separate logger for every class, and give it the class name like this :
private static final Logger logger_for_class_foo = Logger.getLogger(ClassFoo.class.getName());
You can check if your logger accepts logging at the level you're trying to use:
if(my_loger.isLoggable(Level.INFO)){ my_logger.info("put this text into the logger"); }
or
if(my_loger.isLoggable(Level.INFO)){ my_logger.log(Level.INFO, "put this text into the logger"); }
You can log much more than messages. use logp(@args) and logrp(@args) to log more stuff like the source of the message, the method name, other parameters you might care about etc.
Syntax
You can use two kinds of syntax
my_logger.log(level.LEVELTYPE, "message");
or
my_logger.leveltype("message");
How can I see all my logged stuff?
You need a handler or a viewer. The handler “handles” loggers and outputs their logged stuff to files, streams, whatever.
FileHandler
FileHandler API(Java 2 Platform SE v1.4.2)
The file handler will store your logged stuff in a file, so you have to tell it a filename (see Java file naming ) like this :
log_file = new File("myfilename.log"); if (!log_file.exists()){ log_file.createNewFile(); }; Handler my_handler = new FileHandler(log_file.getPath()); my_logger.setLevel(Level.ALL) // this sets all the levels to be logged my_logger.addHandler(my_handler);
Making log files legible
The FileHandler can format your log in multiple ways, depending on what Formatter you give it. The API has more details). By default, FileHandler handlers store your logged stuff in xml, so it's not too legible. Here is an example XML log format
Formatter (Java 2 Platform SE v1.4.2)
Existing Formatters:
*Simple Formatter
*XML Formatter
External programs that prettily display log files
Who takes care of all the loggers?
The LogManager. there is only one of it. you can get the log manager with
LogManager.getLogManager
Logging links :