Reputation: 2932
I'm using Tomcat 8.0.21 on RHEL 7. In my Java code I'm logging to a text file with java.util.logging.Logger
.
There is always only one log file. If I restart Tomcat the logging starts again from that moment and all previous logs are gone.
I added %g
to file name as instructed here but it only adds 0 to file name and no rotation occurs.
Here is my code to create the FileHandler
. strFilePath
value is for example "/tmp/mylog.log". LogFormatter
is my own class.
// Need to set format with own formatter class to get plain text to one line (default format is XML).
FileHandler file_handler = new FileHandler(strFilePath);
file_handler.setFormatter(new LogFormatter());
logger.addHandler(file_handler);
On my Windows 7 laptop logs rotate fine using the same code and Tomcat version.
How can I enable Java Logger
log file rotation on my RHEL server?
Edit: I guess I could just add timestamp to file name when constructing FileHandler
. I'm going to try that.
Upvotes: 1
Views: 1248
Reputation: 11045
The FileHandler only rotates when the limit is exceeded (or unable to lock the file). If you want to rotate files by time then you have to code for that. If you want to just trigger a rotation then just create a throw away file handler with a limit of zero bytes before you open your actual file handler.
new FileHandler(pattern, 0, 1, false).close();
Upvotes: 1
Reputation: 2932
This is not a perfect solution but I think I won't lose old logs when I construct the FileHandler
this way:
FileHandler file_handler = new FileHandler(strFilePath.replace("<timestamp>", new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date())));
I have "<timestamp>
" string in the file name and that gets replaced here. I think a new log file is only created when I restart Tomcat so the log files might get big. But this is the best I've come up so far.
Upvotes: 0