Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending TimeStamp in log file name of Java util logger

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.

like image 560
ProblemSolver Avatar asked Nov 16 '11 10:11

ProblemSolver


People also ask

Does Java Util logging use Log4j?

The JDK Logging Adapter is a custom implementation of java. util. logging. LogManager that uses Log4j.

What is Java Util logger?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.


2 Answers

may this example will helps you.

String timeStamp = new SimpleDateFormat().format( new Date() );
FileHandler fh = new FileHandler("./jay_log_%u.%g_" +timeStamp +".log", 30000,4);  
logger.addHandler(fh);
like image 77
Sumit Singh Avatar answered Sep 29 '22 22:09

Sumit Singh


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

like image 28
Emmanuel Bourg Avatar answered Sep 29 '22 23:09

Emmanuel Bourg