java.util.logging Usage


The key elements in java.util.logging are Logger, Handler, Filter, Formatter.

Logger
The main entity on which applications make logging calls.
Each logger keeps track of a log level that it is interested in, and discards log requests that are below this level.

Loggers keep track of their parent loggers in the logging namespace. A logger's parent is its nearest extant ancestor in the logging namespace. The root Logger (named "") has no parent. Anonymous loggers are all given the root logger as their parent. Loggers may inherit various attributes from their parents in the logger namespace. In particular, a logger may inherit: Logging level, Handlers, Resource bundle names.

Handler
Exports LogRecord objects to a variety of destinations. J2SE provides the following Handlers:
StreamHandler, FileHandler, SocketHandler
ConsoleHandler: A simple handler for writing formatted records to System.err
MemoryHandler: A handler that buffers log records in memory.

Additional Handlers may be developed and attached to a logger.

Filter
Provides fine-grained control over what gets logged, beyond the control provided by log levels.

Formatter
Provides support for formatting LogRecord objects. J2SE already includes two standard Formatters: SimpleFormatter and XMLFormatter


Configuration Options

java.util.logging.FileHandler.[level|filter|formatter|encoding|limit|count|pattern|append]
special components in pattern:
"%t" the system temporary directory
"%h" the value of the "user.home" system property
"%g" the generation number to distinguish rotated logs
"%u" a unique number to resolve conflicts

Configuration Example
# Specify the handlers to create in the root logger
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
# Set the default logging level for the root logger
.level = ALL

java.util.logging.ConsoleHandler.level = FINER
# java.util.logging.ConsoleHandler.filter (defaults to no Filter).
java.util.logging.ConsoleHandler.formatter =java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.encoding = UTF-8

java.util.logging.FileHandler.level = INFO
java.util.logging.FileHandler.formatter = org.codeexample.logging.MyFormatter
java.util.logging.FileHandler.encoding = UTF-8
#1 Mb
java.util.logging.FileHandler.limit = 1000000
java.util.logging.FileHandler.count = 3

org.codeexample.level = INFO
org.codeexample.handler = org.codeexample.logging.MyHandler

# config option can be used to specify a class whose constructor is responsible for setting the initial configuration.
# config = org.codeexample.logging.LogConfiguer

Preventing a Logger from Forwarding Log Records to Its Parent
# Stop/Start forwarding log records to ancestor handlers
logger.setUseParentHandlers(false|true);

Customization - see the documentation of LogManager class
Custom logging properties file can be loaded by
System property "java.util.logging.config.file" can be specified tp load custom logging properties file.
System property "java.util.logging.config.class" can be used to specify a class whose constructor is responsible for setting the initial configuration.
"java.util.logging.manager" can be used to sepcify your LogManager implementation.

Logging Example:
public class LoggingConfig  
 {  
   /**  
    * This example demonstrate how to add a handler and filter  
    */  
   public void setFilter()  
 {  
   try  
     {  
       FileHandler handler = new FileHandler("");  
       // Set the filter  
       handler.setFilter(new Filter()  
       {  
         public boolean isLoggable(LogRecord record)  
         {  
           // return true if the record should be logged, false otherwise.  
           // for example, only log if the method name starts with audit.  
           return (record.getSourceMethodName().startsWith("audit"));  
         }  
       });  
       // Add the handler to a logger  
       Logger logger = Logger.getLogger(LoggingConfig.class.getName());  
       logger.addHandler(handler);  
     }  
     catch (IOException e)  
     {  
       e.printStackTrace();  
     }  
   }  
   public void useMemeoryHandler()  
   {  
     // Create a memory handler with a memory of 100 records  
     // and dumps the records into the file my.log when a  
     // SEVERE message is logged  
     FileHandler fhandler;  
     try  
     {  
       fhandler = new FileHandler("my.log", true); // set append mode to true  
       int numRec = 100;  
       MemoryHandler mhandler = new MemoryHandler(fhandler, numRec, Level.SEVERE);  
       // Add to the desired logger  
       Logger logger = Logger.getLogger("com.mycompany");  
       logger.addHandler(mhandler);  
     }  
     catch (IOException e)  
     {  
       e.printStackTrace();  
     }  
   }  
   public static void main(String[] args)  
   {  
     Logger logger = Logger.getLogger(LoggingConfig.class.getName());  
     logger.setLevel(Level.SEVERE);  
   }  
 }  
Resources:

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)