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) { }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.