Appending TimeStamp in log file name of Java util logger
Asked Answered
M

3

8

Currently I am using using Java util for logging logs into the file which can be configured from java.util.logging.FileHandler.pattern. I want to append a timestamp in the log file name. I also have to take the log file path from java.util.logging.FileHandler.pattern property.

Mixer answered 16/11, 2011 at 10:48 Comment(2)
I hope you're using slf4j as a logging facade?Riverside
Use log4j :) and few more chars to satisfy minimum.Mucilage
B
2

You can reuse the FileHandler from Tomcat, it timestamps the filename and rolls it every day:

http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/juli/FileHandler.html

https://github.com/apache/tomcat/blob/7.0.x/java/org/apache/juli/FileHandler.java

Bidwell answered 16/11, 2011 at 10:53 Comment(0)
H
1
public static String currentTimestamp() {
    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
    return f.format(c.getTime());
}

this gives you back a timestamp

Hippopotamus answered 16/11, 2011 at 10:51 Comment(0)
H
-2

Have a look at this post, explain logging in details.

You can use %t for time in your config file.

Hachmin answered 16/11, 2011 at 10:53 Comment(3)
Link no longer exists. java.util.logging.FileHandler does not support %tEmbolden
That's even the temp directory now (/tmp)Oestrogen
docs.oracle.com/javase/7/docs/api/java/util/logging/… says: "%t" the system temporary directoryBarron

© 2022 - 2024 — McMap. All rights reserved.