You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ashish Naik <as...@mahindrabt.com> on 2004/06/02 17:47:32 UTC

configuring Logging

I am not able to understand how to confifigure Log4J for Commons logging
API.

I have added log4j.jar to my classpath. How do I set
log4j.configuration=log4j.properties

I am refering to Logging User Guide at
http://jakarta.apache.org/commons/logging/guide.html


Cheers,
Ashish Naik


*********************************************************
Disclaimer:          

This message (including any attachments) contains 
confidential information intended for a specific 
individual and purpose, and is protected by law. 
If you are not the intended recipient, you should 
delete this message and are hereby notified that 
any disclosure, copying, or distribution of this
message, or the taking of any action based on it, 
is strictly prohibited.

*********************************************************
Visit us at http://www.mahindrabt.com

Re: configuring Logging

Posted by José Antonio Pérez Testa <ja...@indra.es>.
Commons-Logging defaults to Log4j logging when log4j.jar is in the 
application CLASSPATH.
Log4j can be configured by means of  the properties file  
log4j.properties  that  also  must be in the CLASSPATH.
You don't need to configure any property, but if you want to must be in 
the calling of  java runnable:
 > java -Dlog4jconfiguration=log4j.properties -cp <yourClasspath> 
your.main.classFileName

Ashish Naik wrote:

>Its not a web application. I donot understand how the properties are set
>e.g. log4j.configuration=log4j.properties as mentioned in the user guide. Is
>it to configure Log4J?
>
>Cheers,
>Ashish Naik
>
>
>-----Original Message-----
>From: Jose Antonio Perez Testa [mailto:japtesta@indra.es]
>Sent: 02 June 2004 16:59
>To: Jakarta Commons Users List
>Subject: Re: configuring Logging
>
>
>Hi Ashish,
>If it is a WebApp, both log4j and commons-logging jars must be in
>WEB-INF/lib
>
>
>Ashish Naik wrote:
>
>  
>
>>I am not able to understand how to confifigure Log4J for Commons logging
>>API.
>>
>>I have added log4j.jar to my classpath. How do I set
>>log4j.configuration=log4j.properties
>>
>>I am refering to Logging User Guide at
>>http://jakarta.apache.org/commons/logging/guide.html
>>
>>
>>Cheers,
>>Ashish Naik
>>
>>
>>*********************************************************
>>Disclaimer:
>>
>>This message (including any attachments) contains
>>confidential information intended for a specific
>>individual and purpose, and is protected by law.
>>If you are not the intended recipient, you should
>>delete this message and are hereby notified that
>>any disclosure, copying, or distribution of this
>>message, or the taking of any action based on it,
>>is strictly prohibited.
>>
>>*********************************************************
>>Visit us at http://www.mahindrabt.com
>>
>>
>>
>>    
>>
>----------------------------------------------------------------------------
>---------------------------------------
>Este correo electrsnico y, en su caso, cualquier fichero anexo al mismo,
>contiene informacisn de caracter confidencial exclusivamente dirigida a su
>destinatario o destinatarios. Queda prohibida su divulgacisn, copia o
>distribucisn a terceros sin la previa autorizacisn escrita de Indra. En el
>caso de haber recibido este correo electrsnico por error, se ruega notificar
>inmediatamente esta circunstancia mediante reenvmo a la direccisn
>electrsnica del remitente.
>
>The information in this e-mail and in any attachments is confidential and
>solely for the attention and use of the named addressee(s). You are hereby
>notified that any dissemination, distribution or copy of this communication
>is prohibited without the prior written consent of Indra. If you have
>received this communication in error, please, notify the sender by reply
>e-mail
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>
>*********************************************************
>Disclaimer:          
>
>This message (including any attachments) contains 
>confidential information intended for a specific 
>individual and purpose, and is protected by law. 
>If you are not the intended recipient, you should 
>delete this message and are hereby notified that 
>any disclosure, copying, or distribution of this
>message, or the taking of any action based on it, 
>is strictly prohibited.
>
>*********************************************************
>Visit us at http://www.mahindrabt.com
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>  
>
-------------------------------------------------------------------------------------------------------------------
Este correo electrónico y, en su caso, cualquier fichero anexo al mismo, contiene información de carácter confidencial exclusivamente dirigida a su destinatario o destinatarios. Queda prohibida su divulgación, copia o distribución a terceros sin la previa autorización escrita de Indra. En el caso de haber recibido este correo electrónico por error, se ruega notificar inmediatamente esta circunstancia mediante reenvío a la dirección electrónica del remitente.

The information in this e-mail and in any attachments is confidential and solely for the attention and use of the named addressee(s). You are hereby notified that any dissemination, distribution or copy of this communication is prohibited without the prior written consent of Indra. If you have received this communication in error, please, notify the sender by reply e-mail

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


RE: configuring Logging

Posted by Florian Kiebel <fl...@matamore.com>.
Hello,

So log4j.properties is a text file which contains all the config for log4j
with logger and appenders...
Like this file (Be careful it's for a webapp...):
#
# Définition des Logger "log4j.logger." + nom de la classe + " = " + level +
nom du logger
#
# log4j.rootLogger=debug, stdout, R
log4j.logger.org.apache.jsp.testRuntime_jsp = debug, Runtime
log4j.logger.CRON = debug, CRON

# log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
# log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.Runtime=org.apache.log4j.RollingFileAppender
log4j.appender.Runtime.File=c:/example.log
log4j.appender.Runtime.MaxFileSize=100KB
# Keep one backup file
log4j.appender.Runtime.MaxBackupIndex=1

log4j.appender.Runtime.layout=org.apache.log4j.PatternLayout
log4j.appender.Runtime.layout.ConversionPattern=%p %t %c - %m%n


log4j.appender.CRON=org.apache.log4j.RollingFileAppender
log4j.appender.CRON.File=c:/cron.log
log4j.appender.CRON.MaxFileSize=100KB
# Keep one backup file
log4j.appender.CRON.MaxBackupIndex=1

log4j.appender.CRON.layout=org.apache.log4j.PatternLayout
log4j.appender.CRON.layout.ConversionPattern=%p %t %c - %m%n

So as an example, you have a logger "log4j.logger.CRON = debug, CRON" so
CRON is a java class in this class I created an instance of a Logger object
called CRON. As you can see in the line before, I have put the priority to
Debug level.
In my CRON class, I have "Logger cronLog = Logger.getLogger("CRON");"
This is a reference towards my logger definition in log4j.properties

Then I define an appender with the lines "log4j.appender.CRON ..."
This appender permits to send log messages in the target here a
RollingFileAppender (file with daily save) named cron.log 

I define also a format of log messages with the two last lines (layout)
All of this is detailed in the doc of log4j Home and the excellent "short
introduction" of Ceki Gülcü and I have another link for you if you want :
http://www.vipan.com/htdocs/log4jhelp.html
which is a good demo of log4j

All I do is for a webapp which needs an initialization of log4j by a
servlet, I give you the init() method:
public void init() {
		String prefix =  getServletContext().getRealPath("/");
		String file = getInitParameter("log4j-init-file");
		// if the log4j-init-file is not set, then no point in
trying
		if(file != null) {
			PropertyConfigurator.configure(prefix+file);
		}
	}
Int this, you see "log4j-init-file" which points towards log4j.properties in
the web.xml of my webapp, don't forget to import the packages and to put
them in your classpath.

You can set a xml config file and at this point you must change
PropertyConfigurator as DOMConfigurator

I hope I help you

Florian Kiebel

-----Message d'origine-----
De : Ashish Naik [mailto:ashishn@mahindrabt.com] 
Envoyé : mercredi 2 juin 2004 18:06
À : Jakarta Commons Users List
Objet : RE: configuring Logging


Its not a web application. I donot understand how the properties are set
e.g. log4j.configuration=log4j.properties as mentioned in the user guide. Is
it to configure Log4J?

Cheers,
Ashish Naik


-----Original Message-----
From: Jose Antonio Perez Testa [mailto:japtesta@indra.es]
Sent: 02 June 2004 16:59
To: Jakarta Commons Users List
Subject: Re: configuring Logging


Hi Ashish,
If it is a WebApp, both log4j and commons-logging jars must be in
WEB-INF/lib


Ashish Naik wrote:

>I am not able to understand how to confifigure Log4J for Commons logging
>API.
>
>I have added log4j.jar to my classpath. How do I set
>log4j.configuration=log4j.properties
>
>I am refering to Logging User Guide at
>http://jakarta.apache.org/commons/logging/guide.html
>
>
>Cheers,
>Ashish Naik
>
>
>*********************************************************
>Disclaimer:
>
>This message (including any attachments) contains
>confidential information intended for a specific
>individual and purpose, and is protected by law.
>If you are not the intended recipient, you should
>delete this message and are hereby notified that
>any disclosure, copying, or distribution of this
>message, or the taking of any action based on it,
>is strictly prohibited.
>
>*********************************************************
>Visit us at http://www.mahindrabt.com
>
>
>
----------------------------------------------------------------------------
---------------------------------------
Este correo electrsnico y, en su caso, cualquier fichero anexo al mismo,
contiene informacisn de caracter confidencial exclusivamente dirigida a su
destinatario o destinatarios. Queda prohibida su divulgacisn, copia o
distribucisn a terceros sin la previa autorizacisn escrita de Indra. En el
caso de haber recibido este correo electrsnico por error, se ruega notificar
inmediatamente esta circunstancia mediante reenvmo a la direccisn
electrsnica del remitente.

The information in this e-mail and in any attachments is confidential and
solely for the attention and use of the named addressee(s). You are hereby
notified that any dissemination, distribution or copy of this communication
is prohibited without the prior written consent of Indra. If you have
received this communication in error, please, notify the sender by reply
e-mail

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



*********************************************************
Disclaimer:          

This message (including any attachments) contains 
confidential information intended for a specific 
individual and purpose, and is protected by law. 
If you are not the intended recipient, you should 
delete this message and are hereby notified that 
any disclosure, copying, or distribution of this
message, or the taking of any action based on it, 
is strictly prohibited.

*********************************************************
Visit us at http://www.mahindrabt.com


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


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


RE: configuring Logging

Posted by Ashish Naik <as...@mahindrabt.com>.
Its not a web application. I donot understand how the properties are set
e.g. log4j.configuration=log4j.properties as mentioned in the user guide. Is
it to configure Log4J?

Cheers,
Ashish Naik


-----Original Message-----
From: Jose Antonio Perez Testa [mailto:japtesta@indra.es]
Sent: 02 June 2004 16:59
To: Jakarta Commons Users List
Subject: Re: configuring Logging


Hi Ashish,
If it is a WebApp, both log4j and commons-logging jars must be in
WEB-INF/lib


Ashish Naik wrote:

>I am not able to understand how to confifigure Log4J for Commons logging
>API.
>
>I have added log4j.jar to my classpath. How do I set
>log4j.configuration=log4j.properties
>
>I am refering to Logging User Guide at
>http://jakarta.apache.org/commons/logging/guide.html
>
>
>Cheers,
>Ashish Naik
>
>
>*********************************************************
>Disclaimer:
>
>This message (including any attachments) contains
>confidential information intended for a specific
>individual and purpose, and is protected by law.
>If you are not the intended recipient, you should
>delete this message and are hereby notified that
>any disclosure, copying, or distribution of this
>message, or the taking of any action based on it,
>is strictly prohibited.
>
>*********************************************************
>Visit us at http://www.mahindrabt.com
>
>
>
----------------------------------------------------------------------------
---------------------------------------
Este correo electrsnico y, en su caso, cualquier fichero anexo al mismo,
contiene informacisn de caracter confidencial exclusivamente dirigida a su
destinatario o destinatarios. Queda prohibida su divulgacisn, copia o
distribucisn a terceros sin la previa autorizacisn escrita de Indra. En el
caso de haber recibido este correo electrsnico por error, se ruega notificar
inmediatamente esta circunstancia mediante reenvmo a la direccisn
electrsnica del remitente.

The information in this e-mail and in any attachments is confidential and
solely for the attention and use of the named addressee(s). You are hereby
notified that any dissemination, distribution or copy of this communication
is prohibited without the prior written consent of Indra. If you have
received this communication in error, please, notify the sender by reply
e-mail

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



*********************************************************
Disclaimer:          

This message (including any attachments) contains 
confidential information intended for a specific 
individual and purpose, and is protected by law. 
If you are not the intended recipient, you should 
delete this message and are hereby notified that 
any disclosure, copying, or distribution of this
message, or the taking of any action based on it, 
is strictly prohibited.

*********************************************************
Visit us at http://www.mahindrabt.com


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


Re: configuring Logging

Posted by José Antonio Pérez Testa <ja...@indra.es>.
Hi Ashish,
If it is a WebApp, both log4j and commons-logging jars must be in 
WEB-INF/lib


Ashish Naik wrote:

>I am not able to understand how to confifigure Log4J for Commons logging
>API.
>
>I have added log4j.jar to my classpath. How do I set
>log4j.configuration=log4j.properties
>
>I am refering to Logging User Guide at
>http://jakarta.apache.org/commons/logging/guide.html
>
>
>Cheers,
>Ashish Naik
>
>
>*********************************************************
>Disclaimer:          
>
>This message (including any attachments) contains 
>confidential information intended for a specific 
>individual and purpose, and is protected by law. 
>If you are not the intended recipient, you should 
>delete this message and are hereby notified that 
>any disclosure, copying, or distribution of this
>message, or the taking of any action based on it, 
>is strictly prohibited.
>
>*********************************************************
>Visit us at http://www.mahindrabt.com
>
>  
>
-------------------------------------------------------------------------------------------------------------------
Este correo electr�nico y, en su caso, cualquier fichero anexo al mismo, contiene informaci�n de car�cter confidencial exclusivamente dirigida a su destinatario o destinatarios. Queda prohibida su divulgaci�n, copia o distribuci�n a terceros sin la previa autorizaci�n escrita de Indra. En el caso de haber recibido este correo electr�nico por error, se ruega notificar inmediatamente esta circunstancia mediante reenv�o a la direcci�n electr�nica del remitente.

The information in this e-mail and in any attachments is confidential and solely for the attention and use of the named addressee(s). You are hereby notified that any dissemination, distribution or copy of this communication is prohibited without the prior written consent of Indra. If you have received this communication in error, please, notify the sender by reply e-mail

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


Re: configuring Logging

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
Hi Ashish,

On Thu, 2004-06-03 at 03:47, Ashish Naik wrote:
> I am not able to understand how to confifigure Log4J for Commons logging
> API.
> 
> I have added log4j.jar to my classpath. How do I set
> log4j.configuration=log4j.properties
> 
> I am refering to Logging User Guide at
> http://jakarta.apache.org/commons/logging/guide.html

Unfortunately, some of the other replies to your question are not quite
correct, or more complex than is needed.

Firstly, to use log4j as the "back end" for an application that uses
commons-logging, you normally just:
a) put log4j.jar in the classpath, and
b) put a file named "log4j.xml" or "log4j.properties" in the classpath.

On startup, commons-logging detects that the log4j libraries are
present, and initialises log4j. Log4j then searches for its config files
in the classpath. Note that it is not *commons-logging* that configures
log4j. All commons-logging does is find and invoke the log4j
initialisation method; the rest is done by log4j.


The commons-logging documentation page you refer to above talks about
"system properties". These are values that application code can retrieve
using the API call:
  String value = System.getProperty("who");
These are set by running java like this:  
  java -Dwho=me  ......

So what the page referred to is saying is that if your log4j.properties
file is not in the classpath, or is not named "log4j.properties"  then
you can still tell log4j where to find it by running your application
as:
  java -Dlog4j.configuration=/var/myconfigfile.props .....

As a side-note, I *much* prefer to use xml-format log4j configuration
files. They are easier to read, and have features not available when
using the properties format configuration files.



Re Fredrick's email, which states that a commons-logging configuration
file is necessary. He is correct that such a file *can* be used. But
normally it is not necessary. If you have more than one logging library
in your classpath and you don't like the one that commons-logging
selects by default, you could use this to force your preferred lib to be
used. And you can use this to configure your own custom logging
implementation (which is not at all common). Otherwise, it is not
needed.

Re Shrihas's email: this code is technically correct. But it means that
the application is now directly linked to the log4j libraries, which
rather spoils the goal of commons-logging. This solution (and the one
described out by Florian) can be appropriate in some situations where
you want to force log4j to retrieve configuration from some non-standard
place. But in general it is not necessary. And by the way, this is a
log4j-specific technique, and is not related to commons-logging.

For most users, just placing log4j.jar and log4j.xml (or
log4j.properties) in the classpath is all that is needed.



I hope this helps,


Simon


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


Re: configuring Logging

Posted by Fredrik Westermarck <fr...@mdh.se>.
Ashish Naik wrote:
> I am not able to understand how to confifigure Log4J for Commons logging
> API.
> 
> I have added log4j.jar to my classpath. How do I set
> log4j.configuration=log4j.properties
> 
> I am refering to Logging User Guide at
> http://jakarta.apache.org/commons/logging/guide.html
> 

Hi!

You need two separate configuration files - one for commons-logging and 
one for log4j.

The configuration file for commons-logging needs to be named 
commons-logging.properties and in your case, since you want to use 
log4j, the content of the file should be:
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Log4jFactory

That will tell commons-logging that you want to use log4j as your 
logging implementation.

You can then configure log4j as you normally do in the file 
log4j.properties.


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