![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e402. Limiting the Size of a Log by Using a Rotating Sequence of FilesThe example e401 Limiting the Size of a Log File shows how to limit the log file size by automatically emptying it when it reaches the limit. However, this approach has the disadvantage of discarding useful information even though the file size might be well within the limit. TheFileHandler allows a more effective approach by allowing
you to use a sequence of files to hold the log information. When a
file fills up, the oldest file is emptied and logging resumes in that
file.
More specifically, if there are The logfile number is called the generation number and ranges
from 0 to the number of logfiles - 1. When specifying the filename
pattern to use for the logfiles, you need to include the location of
the generation number using the try { // Create a file handler that uses 3 logfiles, each with a limit of 1Mbyte String pattern = "my%g.log"; int limit = 1000000; // 1 Mb int numLogFiles = 3; FileHandler fh = new FileHandler(pattern, limit, numLogFiles); // Add to logger Logger logger = Logger.getLogger("com.mycompany"); logger.addHandler(fh); } catch (IOException e) { }
© 2002 Addison-Wesley. |