gtludwig
gtludwig

Reputation: 5601

how to correctly configure log4j.properties according to my design?

My desktop application log4j.properties file is:

## Log levels
## TRACE < DEBUG < INFO < WARN < ERROR < FATAL
log4j.rootLogger=INFO
#
## Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#
## Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{${datestamp}} %-5p %c{1}:%L - %m%n

And I run this application using java -jar appName.jar > <path-to-log-dir>/logFile.log.

The output for this file is, for instance:

0 [main] INFO br.com.mentium.hrm.agent.Agent  - Thread started at: Wed Nov 30 09:53:03 BRST 2016
3 [main] INFO br.com.mentium.hrm.agent.Agent  - HRM Agent
3 [main] INFO br.com.mentium.hrm.agent.Agent  - 

3 [main] INFO br.com.mentium.hrm.agent.Agent  - Polling server every 1 minute(s).
3 [main] INFO br.com.mentium.hrm.agent.Agent  - 

4 [main] INFO br.com.mentium.hrm.agent.Agent  - ######################
4 [main] INFO br.com.mentium.hrm.agent.Agent  - 

5 [main] INFO br.com.mentium.hrm.agent.Agent  - Execution  at Wed Nov 30 09:53:03 BRST 2016
5 [main] INFO br.com.mentium.hrm.agent.Agent  - Iteration number: 1
5 [main] INFO br.com.mentium.hrm.agent.Agent  - 

Where the first number on each line is the time in milliseconds since the application was started. I guess.

I'd like to format the log's output as:

yyyy-MM-dd hh:mm:sss abbreviatedClassName (ie, b.c.m.h.a.ClassName) - message

I know I need to do it on the ConversionPattern line, but no changes I do to it seem to take effect.

What's wrong here?

Upvotes: 0

Views: 28

Answers (1)

Rajind Ruparathna
Rajind Ruparathna

Reputation: 2255

You need to specify it like this. Note that this is not exactly to your requirement. I hope you can try it and get it to your exact requirement.

You can read more about pattern layout here.

log4j.appender.CONSOLE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-5p %c{1}:%L - %m%n

Upvotes: 2

Related Questions