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 David Thielen <dt...@extendthereach.com> on 2001/05/17 21:16:02 UTC

Asking again: Adding log-time conversion characters

> Hi;
> 
> I need to add three conversion characters that are strings I can resolve
> at log time (by getting data I have tied to the thread). What is the best
> way to do this? (This is for a java server program and one of the pieces
> is who is logged in to the system.)
> 
> thanks - dave

Re: Asking again: Adding log-time conversion characters

Posted by Anders Kristensen <ak...@dynamicsoft.com>.

David Thielen wrote:
> 
> I looked at that - org.apache.log4j.examples.appserver adds it's data at
> Category constructor time. I need to add items at the time the log line item
> is written. I need to log the user - we attach a user token to each session
> and I can get that from the thread. But when an object is created  - it can
> be used by multiple users so I can't set it at construction time.

This is from memory, so bear with me.

You can do what you want by extending Category and LoggingEvent and
adding method(s) to your subclass of Category which takes additional
arguments (whatever custom data you need to log). Those new methods
creates instances of your subclass of LoggingEvent. This subclass has
attributes for holding your additional data. Then you write a subclass
of PatternLayout which recognizes your additional conversion characters
and pulls data out of your custom LoggingEvent accordingly.  This is the
scheme described in Pauls appserver example.

You say that your data is availeble to the thread doing the logging. If
this means it's also available to the PatternLayout subclass then
there's a simpler and better way of doing things. Basically, you just
don't extend Category and LoggingEvent, but rather, in your sublcass of
PatternLayout you just extract data for your custom conversion
characters dircely from the "context" (e.g. ThreadLocals if that's what
you're usign).  This is a good thing because using Category subclasses
can be problematic and is best avoided if possible.

Another approach which also avoids creating subclasses of Catgeory is to
define a "data class" holding your custom app data elements, and then
log instance of this class rather than Strings, e.g.

  public class AppData {
    public Foo foo;
    public Bar bar;
    public Baz baz;

    ...
  }

and then in your logging code:

  category.info(new AppData(...));  

In your PatternLayout subclass you can then cast the result of
LoggingEvent.getMessage() and retrieve the attributes as appropriate.
You could define a custom ObjectRenderer but that has limitations
compared to subclassing PatternLayout. Still, if you define a toString()
on AppData, then the defaultRenderer will use that method be get a
String for %m. Maybe I make all this sound very complicated, but it
really isn't.

Cheers,
Anders

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


Re: Asking again: Adding log-time conversion characters

Posted by Paul Glezen <pg...@atdial.net>.
David Thielen wrote:
> 
> I looked at that - org.apache.log4j.examples.appserver adds it's data at
> Category constructor time. I need to add items at the time the log line item
> is written. I need to log the user - we attach a user token to each session
> and I can get that from the thread. But when an object is created  - it can
> be used by multiple users so I can't set it at construction time.
> 
> Are there any examples or a suggestion of what to go in to? I'd be happy to
> deliver the final code back to the project - but only if I know I'm
> implementing it in a reasonable manner.
> 

Hi David,

It seems that you need to extend Category like Ceki suggested.  Your
converter class will be responsible for determining at runtime what the
user id is.

The problem you describe is certainly a reasonable request.  I've been
working on the next generation of the appserver example - namely some
subclasses that specialize in Servlet logging, JSP logging and EJB
logging.  They will be designed to do what I just described above, to
retrieve context information from the Servlet/Entity/Session context.

Go ahead and write something up.  Perhaps we can merge with/replace what
I'm working on.  I'm expecting the product to be a new package for the
1.2 release of log4j called org.apache.log4j.appserver designed to
address Java application server needs.

Since most of my appserver experience has been with CORBA rather than
Servlet/EBJs, I'm still learning about the various tricks and traps one
encounters in dealing with application contexts between one vendor and
the next.  I'm hoping to base most of my experience on Tomcat and pray
that iPlanet, WebSphere, WebLogic and WebWhatever have similar behavior.

- Paul
-- 
Beware of bugs in the above code.  I have
only proved it correct, not tried it.
-Don Knuth

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


RE: Asking again: Adding log-time conversion characters

Posted by David Thielen <dt...@extendthereach.com>.
I looked at that - org.apache.log4j.examples.appserver adds it's data at
Category constructor time. I need to add items at the time the log line item
is written. I need to log the user - we attach a user token to each session
and I can get that from the thread. But when an object is created  - it can
be used by multiple users so I can't set it at construction time.

Are there any examples or a suggestion of what to go in to? I'd be happy to
deliver the final code back to the project - but only if I know I'm
implementing it in a reasonable manner.

thanks - dave


-----Original Message-----
From: Ceki Gülcü [mailto:cgu@qos.ch]
Sent: Friday, May 18, 2001 3:16 AM
To: LOG4J Users Mailing List
Subject: Re: Asking again: Adding log-time conversion characters



Have you seen Paul Glezen's "Adding Conversion Characters to PatternLayout"?

The URL is http://jakarta.apache.org/log4j/docs/deepExtension.html

Hope this answers the question. Ceki


At 13:16 17.05.2001 -0600, you wrote:

>> Hi;
>>
>> I need to add three conversion characters that are strings I can resolve
>> at log time (by getting data I have tied to the thread). What is the best
>> way to do this? (This is for a java server program and one of the pieces
>> is who is logged in to the system.)
>>
>> thanks - dave
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-user-help@jakarta.apache.org

--
Ceki Gülcü


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



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


Re: Asking again: Adding log-time conversion characters

Posted by Ceki Gülcü <cg...@qos.ch>.
Have you seen Paul Glezen's "Adding Conversion Characters to PatternLayout"? 

The URL is http://jakarta.apache.org/log4j/docs/deepExtension.html

Hope this answers the question. Ceki


At 13:16 17.05.2001 -0600, you wrote:

>> Hi;
>> 
>> I need to add three conversion characters that are strings I can resolve
>> at log time (by getting data I have tied to the thread). What is the best
>> way to do this? (This is for a java server program and one of the pieces
>> is who is logged in to the system.)
>> 
>> thanks - dave
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-user-help@jakarta.apache.org

--
Ceki Gülcü


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