Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

java - Log4J Custom Fields

Introduction:

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

Progress:

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

Problem:

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

Download Code

customlog.properties

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

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");

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

Solution

Using MDC you can add custom fields like

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

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

log4j.appender.stdout.layout.ConversionPattern=%X{Version}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...