简体   繁体   English

如何根据我的设计正确配置log4j.properties?

[英]how to correctly configure log4j.properties according to my design?

My desktop application log4j.properties file is: 我的桌面应用程序log4j.properties文件是:

## 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 . 然后我使用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, bcmhaClassName) - message yyyy-MM-dd hh:mm:sss abbreviatedClassName(即bcmhaClassName)-消息

I know I need to do it on the ConversionPattern line, but no changes I do to it seem to take effect. 我知道我需要在ConversionPattern行上执行此操作,但是我对其所做的任何更改似乎都不会生效。

What's wrong here? 怎么了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM