You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Marepalli, Somesh" <So...@ca.com> on 2004/06/03 23:33:20 UTC

Using log4j for writing a library...

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. I am planning to include a logging .properties file
within my .jar file for the library.

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

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

       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.

            

   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.

 

   Hope the user community can help me with these questions.

 

regards

Somesh Marepalli

 


Re: Using log4j for writing a library...

Posted by WJCarpenter <bi...@carpenter.ORG>.
>> I am writing a Java based library for which I am using the log4j
>> package (version 1.2.8)
>> a) Are there any special things we need to worry about
>> configuration.

Since you are writing a library, you have to be aware of what log4j
does by default if nobody does any configuration at all.  Here a
message thread that discusses it:

http://marc.theaimsgroup.com/?l=log4j-user&m=108326596313116&w=2
-- 
bill@carpenter.ORG (WJCarpenter)    PGP 0x91865119
38 95 1B 69 C9 C6 3D 25    73 46 32 04 69 D6 ED F3


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Using log4j for writing a library...

Posted by Jacob Kjome <ho...@visi.com>.
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
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org