RE: How can I write a custom appender to supports Throwable as a second parameter?

2009-09-29 Thread Michael Erskine
If your Appender inherits from  a WriterAppender then it tacks the Throwable on 
the end of the message anyway (because it already has somewhere sensible to put 
it and the PatternLayout doesn't deal with it). If you don't want to do this 
and you want the Throwable info in your Appender (NB: costly to collect) call 
LoggingEvent.getThrowableStrRep() and handle it however you like.

Regards,
Michael Erskine.

 -Original Message-
 From: Alexander Spitzer [mailto:aspit...@gmail.com]
 Sent: 28 September 2009 20:58
 To: Log4J Users List
 Subject: Re: How can I write a custom appender to supports Throwable as a
 second parameter?

 hmmm.. so does this mean that if I want to define my own pattern
 layout, it is not possible?

 -alex spitzer

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



Appender-ref within appender tag

2009-09-29 Thread Bender Heri
Hi all

The appender tag of xml configuration allows formally to be like that:

appender name=STANDARD_APPENDERS class=
appender-ref ref=CONSOLE.ERR/
appender-ref ref=CONSOLE.OUT/
appender-ref ref=FILE/
/appender

According to DTD:

!ELEMENT appender (errorHandler?, param*, layout?, filter*, appender-ref*)
!ATTLIST appender
  name  ID  #REQUIRED
  class CDATA   #REQUIRED


But: how to use this feature?

I would like to use one ref to this STANDARD_APPENDERS in my logger 
definitions, since I have a lot of finetuned logger definitions. But my 
development environment (eclipse) likes the Console-Appender, whereas the 
server does not like it. If I can use such a sampler appender I would have to 
change the included appenders only at one location.

Problem is: the class-Attribute of appender element is mandatory. But what to 
write therein? Left blank (like shown above) leads to a ClassNotFountException. 
Omitting the attribute says 
log4j:WARN Attribute class is required and must be specified for element type 
appender.
Followed by a ClassNotFountException again.

The log4j manual has no hints about this feature.

Any ideas?

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



Configuring log4j with XML

2009-09-29 Thread pagod

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: 
?xml version=1.0 encoding=UTF-8?
!DOCTYPE log4j:configuration PUBLIC -//Apache//log4j LOG4J 1.0//EN
http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd; 
log4j:configuration
appender name=stdout class=org.apache.log4j.ConsoleAppender
layout class=org.apache.log4j.PatternLayout

/layout
/appender
root
level value=fatal /
appender-ref ref=stdout /
/root
/log4j:configuration

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:
logger name=Test
level value=info/
appender-ref ref=stdout /
/logger

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.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= null.
log4j: Ignoring debug attribute.
log4j: reset attribute= false.
log4j: Threshold =null.
log4j: Level value for root is  [fatal].
log4j: root level set to FATAL
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: org.apache.log4j.PatternLayout
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}:%L -
%m%n].
log4j: Adding appender named [stdout] to category [root].
log4j: Could not find root logger information. Is this OK?
log4j: Finished configuring.

What's this message about not finding the root logger information? It is
contained in both the default config file and the config file given to
PropertyConfigurator.configure... 

And where's my info message??

Now however, if I add the same Test logger in my default config file and
carry out exactly the same command, I get a somewhat different output: 
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.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= null.
log4j: Ignoring debug attribute.
log4j: reset attribute= false.
log4j: Threshold =null.
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [Test] additivity to