The Java Developers Almanac 1.4


Order this book from Amazon.

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

e391. Writing Log Records to a Log File

To make a logger write log records to a file, you need to add a file handler to the logger.
    try {
        // Create a file handler that write log record to a file called my.log
        FileHandler handler = new FileHandler("my.log");
    
        // Add to the desired logger
        Logger logger = Logger.getLogger("com.mycompany");
        logger.addHandler(handler);
    } catch (IOException e) {
    }
By default, a file handler overwrites the contents of the log file each time it is created. This example creates a file handler that appends.
    try {
        // Create an appending file handler
        boolean append = true;
        FileHandler handler = new FileHandler("my.log", append);
    
        // Add to the desired logger
        Logger logger = Logger.getLogger("com.mycompany");
        logger.addHandler(handler);
    } catch (IOException e) {
    }

 Related Examples
e385. The Quintessential Logging Program
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
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.