6 messages in org.apache.logging.log4j-userRE: Using log4j for writing a library...
FromSent OnAttachments
Marepalli, SomeshJun 3, 2004 2:32 pm 
Jacob KjomeJun 3, 2004 5:33 pm 
WJCarpenterJun 3, 2004 10:26 pm 
Marepalli, SomeshJun 4, 2004 6:05 am 
Marepalli, SomeshJun 4, 2004 6:11 am 
Jacob KjomeJun 4, 2004 5:05 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:RE: Using log4j for writing a library...Actions...
From:Marepalli, Somesh (Some@ca.com)
Date:Jun 4, 2004 6:05:48 am
List:org.apache.logging.log4j-user

Hi: Thanks for the replies. I am planning to use the .properties file internal to my .jar file (not using log4j default configuration).

Also, as suggested below, I plan to write a config utility class with static method. It is in this class I plan to use PropertyConfigurator class

However, instead of user invoking explicit API to configure with the library provided logging .properties file, I would still like to decide automatically when to use it. I would like to configure my library using this internal .properties file when no other logging has been configured. That's the use case which suits my library.

From another user's e-mail, there is a thread talking about detecting if log4j is configured - is that advisable for a library?

But from your mail - I am now sure of one thing. If by mistake, log4j is configured a second time inside the library, the user's root logger and other loggers would lose the settings from configuring the first time.

Is this a correct conclusion?

Thanks for all the replies...

Regards -Somesh

At 05:33 PM 6/3/2004 -0400, you wrote:

Hi:

I am writing a Java based library for which I am using the log4j package (version 1.2.8)

I am new to log4j. I am confused about the following points:

a) Are there any special things we need to worry about configuration.

Currently, I am considering configuration from a properties file using the PropertyConfigurator class when the library

initializes.

Sure. You can use a properties file or XML file

I am planning to include a logging .properties file within my .jar file for the library.

Bad idea. Logging is for your users, not for you (unless you are *using* your library, of course)

The case I am considering I when users (read as applications) of my library may also be using log4j.

Exactly! You can't know what your users want to log and if you provide a default log4j.properties file in the classpath, then you may end up overriding a users own properties file causing the user excessive confusion as to why Log4j isn't logging the way they have it configured.

I am not sure if I

use PropertyConfigurator class without resetting logging configuration of a user in that case.

This is an even worse idea than providing a properties file which *might* override a users. In this case, you are programmatically configuring Log4j and will almost certainly override a users logging configuration, again,

causing excessive confusion for the user.

If you want a compromise, you could maybe provide a default log4j.properties file, but don't put it in the root of the jar where Log4j is looking for it. Then provide a utility class with a static method such as Log4jUtility.configureMyLibrary() which the user can call, if desired. configureMyLibrary() would load up your properties file and programmatically configure Log4j. Since this will be used only with the

user's clear intent, then there should be no confusion, keeping everyone happy.

Specifically, I would like all loggers within my library to be at least at the WARN level. Is there a way I can ensure that

even if I am writing a library.

The simple truth is that you shouldn't force your own concept of what you want logged upon your users. If users want to see logging from your app, they can specify your package to be logged (as long as you stick to the standard naming conventions for loggers, eg.... "com.mypackage.MyClass").

b) For RollingFileAppender, is there a nice way to specify location of log file without hard-coding the path. I am interested in knowing what other library writers do in such cases.

Reference the name of a system property and make sure to provide that system property either programmatically (before any configuration of Log4j happens) or via the command line such as:

java -Dlog_location=/path/to/log/directory MyClass

In the config file, have something like...

${log_location}/mylog.log

Hope the user community can help me with these questions.

I hope I helped!

Jake

regards

Somesh Marepalli