AW: Configuring log4j with XML

2009-09-30 Thread Bender Heri
First: 
For simple configuration needs (in fact for most configuration needs) you do 
not need to call a configurator explicitely. Just put your config file in the 
classpath, and log4j finds it for you and autoconfigures itself the first time 
the LogManager is loaded (e.g. on a static Logger.getLogger()). Log4j looks 
first for a file "log4j.xml", and if not found, for "log4j.properties". That's 
all. (you can overwrite the name of the file by the commandline argument or by 
system property).

Second:
You ran into some subtle problems which misleaded you to wrong assumptions.
- If for some reasons you must load the configuration programmatically: a XML 
configuration should be loaded by the DOMConfigurator, a properties file by the 
PropertiesConfigurator. I suppose in your second experiment the (default) 
configurator finds the default.xml (given by commandline param), but the 
PropertyConfigurator cannot load the second xml file (as in all further 
experiments).
- the double output in your third experiment was due you have then two loggers 
in the hierarchy (both from the default.xml). In code you are calling the 
logger "Test" which outputs to its appender, then climbing up to the root 
logger which outputs the same log message to its appender again (which happens 
to be the same one). To avoid this, you should declare your Test-Logger with 
additivity set to false (default true):





Third: 
Multiple calls to XXXConfigurator.configure append new configuration items to 
the already loaded ones unless calling LogManager.resetConfiguration(). When 
you did this, the first configuration from the default.xml was purged and the 
second one did not succeed (see above). That's the reason for the last WARN 
messages.

Heri

-Ursprüngliche Nachricht-
Von: pagod [mailto:dvergn...@yahoo.com] 
Gesendet: Dienstag, 29. September 2009 18:56
An: log4j-user@logging.apache.org
Betreff: Configuring log4j with XML


Hi everyone,

I seem to have a solid understanding problem with the way log4j loads its 
configuration, perhaps someone can take a few minutes to explain it to me? I 
only started a few hours ago, but I'm already startled... :-|

Basically, what I want to do is create a simple logger and configure it using 
an XML file. 
So my questions are: 
- does log4j have to load a default configuration file (log4j.xml or whatever I 
set log4j.configuration to) even if I call the PropertyConfigurator.configure( 
String filename ) function?? 
- This wouldn't be such a problem if I understood clearly how the default 
configuration file interacts with another configuration file I might be trying 
to load: are the loggers/appenders simply added to the default configuration, 
or are there any risks when overwriting parts of it? 

A concrete example: 
Here's the Java code I'm testing with:

import org.apache.log4j.*;

public class Test {
static Logger   
__logger;
public static void main( String args[] ) throws Exception {
// 1
//PropertyConfigurator.configure( "~/log4j_config.xml" );
// 2
BasicConfigurator.configure();
__logger = Logger.getLogger( Test.class );
__logger.info( "bonjour" );
}
}

Pretty basic. Now I have the following file which is used as the default 
configuration by specifying -Dlog4j.configuration=log4j_default.xml on the 
command line when starting java: 

http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd"; > 












Again pretty easy, and when I execute the program I get the expected output 
(i.e. nothing). 

Now suppose I have a second file called log4j_config.xml, which is the same as 
log4j_default.xml, except that it has an additional entry:





After uncommenting the line after // 1 in my code and commenting out the one 
after // 2 (i.e., as I understand it, switching from default configuration to 
"named" configuration), I find that the normal output hasn't changed a single 
bit, although I'd have expected a message "bonjour" to be dumped to the console 
(appender "stdout" is registered for logger Test with level output, which 
matches the code, doesn't it?). However, the debug output (enabled with 
-Dlog4j.debug=1) has changed, below is the output after the first call (default 
config) and, in bold, the two lines added after the second call (file config):
log4j: Trying to find [log4j_default.xml] using context classloader 
sun.misc.launcher$appclassloa...@61ba34f2.
log4j: Using URL [file:/home/pagod/projects/Test/log4j_default.xml] for 
automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is:
com.sun.org.apache.xerces

Re: AW: Configuring log4j with XML

2009-09-30 Thread pagod

Hi!

Thanks a lot for the reply, it did help *a lot* :-). I still have a few
concerns, though they're just things that surprise me rather than real
problems. 


Bender Heri wrote:
> 
> - If for some reasons you must load the configuration programmatically: a
> XML configuration should be loaded by the DOMConfigurator, a properties
> file by the PropertiesConfigurator. I suppose in your second experiment
> the (default) configurator finds the default.xml (given by commandline
> param), but the PropertyConfigurator cannot load the second xml file (as
> in all further experiments).
> 
I guess the real problem here (apart from my being unable to find the proper
class by myself) might be that absolutely no error message regarding the
incorrect syntax of the expected property file is displayed when trying to
load the XML file using the PropertyConfigurator, even in the debug output.
There not being such an error message made me think there was no problem on
this front. That might be a thing they might want to improve in the next
release. 



Bender Heri wrote:
> 
> - the double output in your third experiment was due you have then two
> loggers in the hierarchy (both from the default.xml). In code you are
> calling the logger "Test" which outputs to its appender, then climbing up
> to the root logger which outputs the same log message to its appender
> again (which happens to be the same one). To avoid this, you should
> declare your Test-Logger with additivity set to false (default true):
> 
I'd have thought having the level value of root set to "fatal" would prevent
"info" messages from being displayed by the root logger, even with the same
appender? Or is this not taken into account when displaying messages coming
from child loggers? 

anyway, thanks again, now it's gonna work like a charm :-)

cheers

pagod
-- 
View this message in context: 
http://www.nabble.com/Configuring-log4j-with-XML-tp25666820p25677104.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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



Diff between Logger and Log

2009-09-30 Thread SkyMe

Hello! I faced with a such situation when Logger and Log act differently.
I configure my log4j with

DOMConfigurator.configureAndWatch(xmlFileName);

In the configure xml file there is defined only one appender - FileAppender.
So, when I'm using

Log log = LogFactory.getLog(name);

and then

log.info("I'm a log!");

it writes this log to file.

But when I'm using

Logger logger.getLogger()
or
LogManager.getLogger()

they write logs only to the console! Why don't they use configurations from
xml file?

context: I deploy my app to JBoss AS, I use my owm log4j.xml (not JBoss'
one), I added log4j.jar to the lib of my .ear project.

Thank you!
-- 
View this message in context: 
http://www.nabble.com/Diff-between-Logger-and-Log-tp25679656p25679656.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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



RE: Diff between Logger and Log

2009-09-30 Thread Matt Brown
Log and LogFactory are not log4j classes 
(http://logging.apache.org/log4j/1.2/apidocs/index.html).

However they are both Commons Logging classes ( 
http://commons.apache.org/logging/commons-logging-1.1.1/apidocs/index.html ).

Are you using Commons Logging on accident?

Or are you trying to find out the differences between Commons Logging and 
Log4j? Take a look at 
http://commons.apache.org/logging/commons-logging-1.1.1/guide.html.



-Original Message-
From: SkyMe [mailto:gs_v...@mail.ru] 
Sent: Wednesday, September 30, 2009 8:03 AM
To: log4j-user@logging.apache.org
Subject: Diff between Logger and Log


Hello! I faced with a such situation when Logger and Log act differently.
I configure my log4j with

DOMConfigurator.configureAndWatch(xmlFileName);

In the configure xml file there is defined only one appender - FileAppender.
So, when I'm using

Log log = LogFactory.getLog(name);

and then

log.info("I'm a log!");

it writes this log to file.

But when I'm using

Logger logger.getLogger()
or
LogManager.getLogger()

they write logs only to the console! Why don't they use configurations from xml 
file?

context: I deploy my app to JBoss AS, I use my owm log4j.xml (not JBoss'
one), I added log4j.jar to the lib of my .ear project.

Thank you!
--
View this message in context: 
http://www.nabble.com/Diff-between-Logger-and-Log-tp25679656p25679656.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


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



RE: Diff between Logger and Log

2009-09-30 Thread SkyMe

I know that they are from different packages, Log is a wrapper for log4j or
other.
I'm trying to figure out why log4j's Logger doesn't work, while common's Log
works fine (in described situation). I want to use Logger instead of Log.
-- 
View this message in context: 
http://www.nabble.com/Diff-between-Logger-and-Log-tp25679656p25685436.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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



Log session session information (Web)

2009-09-30 Thread Renan Vinícius Mozone
I'm planning to use log4j on a JSP based application. However, every log
entry must have a session identification. This session identification is in
a session variable.
How can I configure the formatter to achieve this?

The log entry must be something like this:

[TIMESTAMP] [THREAD] [LOG LEVEL] [SESSION ID] [MESSAGE]

Thanks in advance.


RE: Log session session information (Web)

2009-09-30 Thread Matt Brown
I've implemented this before by writing a javax.servlet.Filter to add the 
SessionID to the MDC of every request. 

-Original Message-
From: Renan Vinícius Mozone [mailto:renan.moz...@gmail.com] 
Sent: Wednesday, September 30, 2009 2:13 PM
To: log4j-user@logging.apache.org
Subject: Log session session information (Web)

I'm planning to use log4j on a JSP based application. However, every log 
entry must have a session identification. This session identification is in a 
session variable.
How can I configure the formatter to achieve this?

The log entry must be something like this:

[TIMESTAMP] [THREAD] [LOG LEVEL] [SESSION ID] [MESSAGE]

Thanks in advance.

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



Re: Log session session information (Web)

2009-09-30 Thread Renan Vinícius Mozone
Dear Matt Brown,
My JSP application also uses some Java classes. Your solution is
suitable for use inside those Java classes?

On Wed, Sep 30, 2009 at 15:40, Matt Brown wrote:

> I've implemented this before by writing a javax.servlet.Filter to add the
> SessionID to the MDC of every request.
>
> -Original Message-
> From: Renan Vinícius Mozone [mailto:renan.moz...@gmail.com]
> Sent: Wednesday, September 30, 2009 2:13 PM
> To: log4j-user@logging.apache.org
> Subject: Log session session information (Web)
>
>I'm planning to use log4j on a JSP based application. However, every log
> entry must have a session identification. This session identification is in
> a session variable.
>How can I configure the formatter to achieve this?
>
>The log entry must be something like this:
>
>[TIMESTAMP] [THREAD] [LOG LEVEL] [SESSION ID] [MESSAGE]
>
>Thanks in advance.
>
> -
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>
>


AW: Log session session information (Web)

2009-09-30 Thread Bender Heri
Of course. MDC content is kept on a per thread basis. Since each session is 
handled in its own thread, you can put a value into the MDC  somewhere at the 
entry point of a session (and remove it again when session is finished). The 
formatter of log4j (at least the PatternLayout) can retrieve this value by 
"%X{sessionID}" where "sessionID" is the name of the variable you have putted 
to MDC.
Heri

-Ursprüngliche Nachricht-
Von: Renan Vinícius Mozone [mailto:renan.moz...@gmail.com] 
Gesendet: Mittwoch, 30. September 2009 21:08
An: log4j-user@logging.apache.org
Betreff: Re: Log session session information (Web)

Dear Matt Brown,
My JSP application also uses some Java classes. Your solution is suitable 
for use inside those Java classes?

On Wed, Sep 30, 2009 at 15:40, Matt Brown wrote:

> I've implemented this before by writing a javax.servlet.Filter to add 
> the SessionID to the MDC of every request.
>
> -Original Message-
> From: Renan Vinícius Mozone [mailto:renan.moz...@gmail.com]
> Sent: Wednesday, September 30, 2009 2:13 PM
> To: log4j-user@logging.apache.org
> Subject: Log session session information (Web)
>
>I'm planning to use log4j on a JSP based application. However, 
> every log entry must have a session identification. This session 
> identification is in a session variable.
>How can I configure the formatter to achieve this?
>
>The log entry must be something like this:
>
>[TIMESTAMP] [THREAD] [LOG LEVEL] [SESSION ID] [MESSAGE]
>
>Thanks in advance.
>
> -
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org
>
>

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