java.util.logging Code Samples
- How do I create a rolling log files?
- How do I write log to a file?
- How do I set the Logger log level?
- How do I create a custom logger Formatter?
- How do I use logger ConsoleHandler?
- How do I limit the size of log file?
- How do I log an exception?
- How do I set the formatter of logger handlers?
- How do I obtain or create a Logger?
- How do I use Logger's MemoryHandler class?
- How do I do a conditional logging?
- How do I compare Logger Level severity?
- How do I prevent the logger send log messages to its parent logger?
- How do I check if a message is loggable?
How do I create a rolling log files?
In this example we create a rolling or a sequenced of log files. Instead of just limiting the file size (see. How do I limit the size of log file) we can also make the log file to roll. This will prevent a lost to an important log message if we use a single log file.
When using more that one file the log file name will have a sequence number in it
starting from 0 to N-1. If we set the count to 5 then we'll
have log files such as myapp.log.0, myapp.log.1 up to
myapp.log.5.
If the first log file (myapp.log.0) is about to full, it will
be renamed to (myapp.log.1) before the log is written to the first
log file. The log is always written to the first file (myapp.log.0).
To read the log messages in sequence you need to start from the highest to the lowest sequence number.
By running this program multiple times you'll see the creation of the log file one by one.
