The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.logging  [20 examples]

e385. The Quintessential Logging Program

To log a message, you first need to obtain a Logger object and then use it to log the message. Loggers have names that resemble a fully qualified class name. Typically, log messages generated from a class would use a Logger with the same name as the class.

The logger contains one or more handlers that are responsible for processing the log records. A handler could write to a file or print to a console. Typically, the code that generates log records should not concern itself with where those log records end up. It only needs to decide the name of the logger to use.

This example logs a few messages to a logger. For examples of controlling the destination of the log messages, see e391 Writing Log Records to a Log File and e392 Writing Log Records to Standard Error.

    import java.io.*;
    import java.util.logging.*;
    
    package com.mycompany;
    public class BasicLogging {
        public static void main(String[] args) {
            // Get a logger; the logger is automatically created if
            // it doesn't already exist
            Logger logger = Logger.getLogger("com.mycompany.BasicLogging");
    
            // Log a few message at different severity levels
            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");
         }
    }

 Related Examples
e386. Determining If a Message Will Be Logged
e387. Logging a Method Call
e388. Logging an Exception
e389. Minimizing the Impact of Logging Code
e390. Preventing a Logger from Forwarding Log Records to Its Parent
e391. Writing Log Records to a Log File
e392. Writing Log Records to Standard Error
e393. Writing Log Records Only After a Condition Occurs
e394. Setting a Filter on a Logger Handler

See also: Configuration    File Size    Formatters    Levels   


© 2002 Addison-Wesley.