Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j with timestamp per log entry

this is my log output

INFO main digestemails - process inbox INFO main digestemails - checking for emails in c:\development\DCMail\email\KN-Source INFO main digestemails - digesting [email protected] INFO main digestemails - extracting attachments INFO main digestemails - no attachments or no attachments supported INFO main digestemails - updating database INFO main digestemails - email -> COMPLETED folder INFO main digestemails -   

I would like a time stamp per log message ie

INFO 2008-12-25 13:14:00 digestemails - email -> COMPLETED folder 

here is my log4j config file

log4j.rootLogger=debug, stdout, R  log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=c:\\development\\DCMail\\logs\\digestlogfolder\\digest-logfile.log  log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1  log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n  

How do I do it?

like image 872
Setori Avatar asked Dec 11 '08 01:12

Setori


People also ask

How do you create a new log file for each time the application runs log4j?

File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

What are the three most important components of log4j?

log4j has three main components: loggers: Responsible for capturing logging information. appenders: Responsible for publishing logging information to various preferred destinations. layouts: Responsible for formatting logging information in different styles.

Which is better log4j or Log4j2?

Log4j, Logback, and Log4j2 are good logging frameworks that are broadly used. So which one should you use? I recommend using Log4j2 because it's the fastest and most advanced of the three frameworks. Logback is still a good option, if performance is not your highest priority.


1 Answers

Use %d in your PatternLayout.

Also %d can take a format pattern as in %d{dd MMM yyyy HH:mm:ss,SSS} you can pick and choose the elements that you want. When the format pattern is omitted the date will be in ISO8601 format.

like image 88
joshperry Avatar answered Sep 20 '22 17:09

joshperry