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
489 views
in Technique[技术] by (71.8m points)

java - Best Practice on Configuring Log4j Properties Path

I am using Log4j in my applications. log4j.properties is placed in a Jar which is in classpath. This configuration file is being used and works fine most of the times.

But sometimes, the logging statements that are getting generated are not as per the configuration file.

My understanding is, this is because the properties file is fetched from the class path. Whatever first properties file is, is fetched and used by log4j. Thus sometimes we get the logging statements different from what is configured in properties file.

In case, the above reason is true, then I think, we need to specify the configuration file specifically by one of the following ways:

  1. Specify using System Properties : -Dlog4j.configuration=log4j.properties
  2. Initializing Log4jInit servlet.

Please suggest which could be the better approach.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

In my application I am using ${user.home} dir to store log4j.properties and using ant we are reading it. it will be platform independent. also you can pass one with build in classpath but reading from ${user.home} is better approach, You or any non technical person can easily access it.

also you can use

Dynamic Log File Location

Many people complain that Log4j forces you to hard-code the location where your logs will be kept. Actually, it is possible to dynamically choose the log-file location, especially if you use the ${log.dir} property substitution technique above. Here's how:

String dynamicLog = // log directory somehow chosen...
Properties p = new Properties( Config.ETC + "/log4j.properties" );
p.put( "log.dir", dynamicLog ); // overwrite "log.dir"
PropertyConfigurator.configure( p );

Also See


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

...