Log4J自定义字段 [英] Log4J Custom Fields

查看:143
本文介绍了Log4J自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用log4j来记录其他字段,并且它的工作正常但是只有当我在代码中而不是在log4j中创建一个appender时.properties

I'm trying to get additional fields to log with log4j, and its working but only when I create an appender in code and not in the log4j.properties


  1. 使用本文将转换字符添加到PatternLayout for log4j 1.1.3

  2. 制作 log4j 1.2的示例应用程序

  1. Used this article Adding Conversion Characters to PatternLayout for log4j 1.1.3
  2. Made a sample app for log4j 1.2



问题:



使用它将运行但不会运行的属性文件使用AppServerPatternLayout以便不显示自定义字段。

Problem:

using the properties file it will run but won't use AppServerPatternLayout so the custom fields aren't displayed.

log4j.rootLogger=FATAL
log4j.logger.some.log=INFO,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=logging.AppServerPatternLayout
log4j.appender.stdout.layout.ConversionPattern=-----------------using log file------------------------%nTime:      %d%nHost:      %h%nServer:    %s%nComponent: %b%nVersion:   %v%nPriority:  %p%nThread Id: %t%nContext:   %x%nMessage:   %m%n



没有log4j属性文件的Main.java日志记录



Main.java logging without a log4j properties file

AppServerLoggerFactory factory;
factory = new AppServerLoggerFactory("MyServer", "MyComponent", "1.0");
AppServerLogger.setFactory(factory);
Logger logger = AppServerLogger.getLogger("some.log");
PatternLayout layout = new AppServerPatternLayout( formatString );
logger.addAppender( new ConsoleAppender(layout) );
logger.info("Hello");



使用log4j属性文件记录Main.java



Main.java logging with a log4j properties file

PropertyConfigurator.configure("customlog.properties");
AppServerLoggerFactory factory;
factory = new AppServerLoggerFactory("MyServer", "MyComponent", "1.0");
AppServerLogger.setFactory(factory);
Logger logger = AppServerLogger.getLogger("some.log");
logger.info("Hello");



预期产出



Expected output

----------------using in code appender----------------------
Time:      2009-11-06 12:55:05,785
Host:      M1330
Server:    MyServer
Component: MyComponent
Version:   1.0
Priority:  INFO
Thread Id: main
Context:   
Message:   logging config from code



实际输出



Actual output

-----------------using log file------------------------
Time:      2009-11-06 12:56:17,983
Host:      
Server:    
Component: 
Version:   
Priority:  INFO
Thread Id: main
Context:   
Message:   logging config from customlog.properties



解决方案



使用MDC可以添加自定义字段,如

Solution

Using MDC you can add custom fields like

MDC.put("Version", versionName);
Logger log = LogManager.getLogger("some.log");        
log.info("Hello");

并使用UPPER案例X在log4j.properties中将其拉出来

and pull it out in the log4j.properties with a UPPER case X

log4j.appender.stdout.layout.ConversionPattern=%X{Version}


推荐答案

从您发布的示例中,我只能猜测 AppServerPatternLayout 不在package logging 。其他一切看起来都找不到。添加

From the example you posted, I can only guess that AppServerPatternLayout is not in the package logging. Everything else looks find. Add

log4j.DEBUG=true

到您的属性文件。然后log4j将在读取属性时转储它所执行的操作。也许这会让你知道出了什么问题。

to your properties file. log4j will then dump what it does while reading the properties. Maybe that gives you an idea what's wrong.

如果这没有帮助,可以考虑使用嵌套诊断上下文

If that doesn't help, consider to use Nested Diagnostic Contexts.

这篇关于Log4J自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆