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 Avi Zohary <av...@oracle.com> on 2013/11/27 15:48:48 UTC

Modify how log4J takes location from the stack

Hey everyone.

I'm looking into creating a logger framework that will use Log4J2 - I 
need this for various reasons.

I know that in some cases, I will want the debug logs to include the 
location data (what method and in what line the Logger.debug(...) method 
was called).

The problem is, that if I wrap the Log4J2's logger, the location will 
always show the wrapping class's call to the logger, as the location.

I read here ( 
http://logging.apache.org/log4j/2.x/manual/layouts.html#LocationInformation) 
that to get the location information, the Log4J2 logger will take a 
stack and walk a certain depth in it to find the caller.

Is it possible to make Log4J2 change that depth so it will find the 
method that called my wrapper class instead of finding my wrapper class?

-- 
Thanks
Avi Zohary


Re: Modify how log4J takes location from the stack

Posted by Avi Zohary <av...@oracle.com>.
Thanks Nick.

I'll try that option as well.

Avi


On 11/28/2013 05:44 PM, Nick Williams wrote:
> Avi, there is a simple solution to this:
>
> What other components do (the tag library, the Commons adapter, and the JBoss Logging project) is, when they create their wrapping logger, they make sure the Log4j logger actually extends org.apache.logging.log4j.spi.AbstractLogger. That's part of the contract: every implementation of Logger must extend AbstractLogger. AbstractLogger specifies a method: log(Marker marker, String fqcn, Level level, Message data, Throwable t). Every log event ultimately goes through that method. Instead of your wrapping logger calling debug(), info(), etc., it needs to call this method. Pass the fully-qualified class name of your wrapping logger to the "String fqcn" parameter.
>
> For an example of this, take a look at JBoss Logging's org.jboss.logging.Log4j2Logger [1] and how it is used by the DelegatingBasicLogger [2].
>
> Hope this helps,
>
> Nick
>
> [1] https://github.com/beamerblvd/jboss-logging/blob/JBLOGGING-95/src/main/java/org/jboss/logging/Log4j2Logger.java
> [2] https://github.com/beamerblvd/jboss-logging/blob/JBLOGGING-95/src/main/java/org/jboss/logging/DelegatingBasicLogger.java
>
> On Nov 28, 2013, at 1:41 AM, Avi Zohary wrote:
>
>> I was actually considering extending the Log4JLogEvent, and overriding the calcLocation method according to my needs, and setting my instance of LogEventFactory which will return the extended Log4JLogEvent instead of the original.
>>
>> Makes sense?
>>
>> Thanks.
>> Avi
>>
>>
>> On 11/27/2013 04:52 PM, Gary Gregory wrote:
>>> Well, I think this would require either for you to compute the location
>>> yourself, or, provide a patch to change Log4j to be able to parametrize it
>>> and surface it to the API.
>>>
>>> Gary
>>>
>>>
>>> On Wed, Nov 27, 2013 at 9:48 AM, Avi Zohary <av...@oracle.com> wrote:
>>>
>>>> Hey everyone.
>>>>
>>>> I'm looking into creating a logger framework that will use Log4J2 - I need
>>>> this for various reasons.
>>>>
>>>> I know that in some cases, I will want the debug logs to include the
>>>> location data (what method and in what line the Logger.debug(...) method
>>>> was called).
>>>>
>>>> The problem is, that if I wrap the Log4J2's logger, the location will
>>>> always show the wrapping class's call to the logger, as the location.
>>>>
>>>> I read here ( http://logging.apache.org/log4j/2.x/manual/layouts.html#
>>>> LocationInformation) that to get the location information, the Log4J2
>>>> logger will take a stack and walk a certain depth in it to find the caller.
>>>>
>>>> Is it possible to make Log4J2 change that depth so it will find the method
>>>> that called my wrapper class instead of finding my wrapper class?
>>>>
>>>> --
>>>> Thanks
>>>> Avi Zohary
>>>>
>>>>
>> -- 
>> Thanks
>> Avi Zohary
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>

-- 
Thanks
Avi Zohary


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


Re: Modify how log4J takes location from the stack

Posted by Nick Williams <ni...@nicholaswilliams.net>.
Avi, there is a simple solution to this:

What other components do (the tag library, the Commons adapter, and the JBoss Logging project) is, when they create their wrapping logger, they make sure the Log4j logger actually extends org.apache.logging.log4j.spi.AbstractLogger. That's part of the contract: every implementation of Logger must extend AbstractLogger. AbstractLogger specifies a method: log(Marker marker, String fqcn, Level level, Message data, Throwable t). Every log event ultimately goes through that method. Instead of your wrapping logger calling debug(), info(), etc., it needs to call this method. Pass the fully-qualified class name of your wrapping logger to the "String fqcn" parameter.

For an example of this, take a look at JBoss Logging's org.jboss.logging.Log4j2Logger [1] and how it is used by the DelegatingBasicLogger [2].

Hope this helps,

Nick

[1] https://github.com/beamerblvd/jboss-logging/blob/JBLOGGING-95/src/main/java/org/jboss/logging/Log4j2Logger.java
[2] https://github.com/beamerblvd/jboss-logging/blob/JBLOGGING-95/src/main/java/org/jboss/logging/DelegatingBasicLogger.java

On Nov 28, 2013, at 1:41 AM, Avi Zohary wrote:

> I was actually considering extending the Log4JLogEvent, and overriding the calcLocation method according to my needs, and setting my instance of LogEventFactory which will return the extended Log4JLogEvent instead of the original.
> 
> Makes sense?
> 
> Thanks.
> Avi
> 
> 
> On 11/27/2013 04:52 PM, Gary Gregory wrote:
>> Well, I think this would require either for you to compute the location
>> yourself, or, provide a patch to change Log4j to be able to parametrize it
>> and surface it to the API.
>> 
>> Gary
>> 
>> 
>> On Wed, Nov 27, 2013 at 9:48 AM, Avi Zohary <av...@oracle.com> wrote:
>> 
>>> Hey everyone.
>>> 
>>> I'm looking into creating a logger framework that will use Log4J2 - I need
>>> this for various reasons.
>>> 
>>> I know that in some cases, I will want the debug logs to include the
>>> location data (what method and in what line the Logger.debug(...) method
>>> was called).
>>> 
>>> The problem is, that if I wrap the Log4J2's logger, the location will
>>> always show the wrapping class's call to the logger, as the location.
>>> 
>>> I read here ( http://logging.apache.org/log4j/2.x/manual/layouts.html#
>>> LocationInformation) that to get the location information, the Log4J2
>>> logger will take a stack and walk a certain depth in it to find the caller.
>>> 
>>> Is it possible to make Log4J2 change that depth so it will find the method
>>> that called my wrapper class instead of finding my wrapper class?
>>> 
>>> --
>>> Thanks
>>> Avi Zohary
>>> 
>>> 
>> 
> 
> -- 
> Thanks
> Avi Zohary
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 


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


Re: Modify how log4J takes location from the stack

Posted by Avi Zohary <av...@oracle.com>.
I was actually considering extending the Log4JLogEvent, and overriding 
the calcLocation method according to my needs, and setting my instance 
of LogEventFactory which will return the extended Log4JLogEvent instead 
of the original.

Makes sense?

Thanks.
Avi


On 11/27/2013 04:52 PM, Gary Gregory wrote:
> Well, I think this would require either for you to compute the location
> yourself, or, provide a patch to change Log4j to be able to parametrize it
> and surface it to the API.
>
> Gary
>
>
> On Wed, Nov 27, 2013 at 9:48 AM, Avi Zohary <av...@oracle.com> wrote:
>
>> Hey everyone.
>>
>> I'm looking into creating a logger framework that will use Log4J2 - I need
>> this for various reasons.
>>
>> I know that in some cases, I will want the debug logs to include the
>> location data (what method and in what line the Logger.debug(...) method
>> was called).
>>
>> The problem is, that if I wrap the Log4J2's logger, the location will
>> always show the wrapping class's call to the logger, as the location.
>>
>> I read here ( http://logging.apache.org/log4j/2.x/manual/layouts.html#
>> LocationInformation) that to get the location information, the Log4J2
>> logger will take a stack and walk a certain depth in it to find the caller.
>>
>> Is it possible to make Log4J2 change that depth so it will find the method
>> that called my wrapper class instead of finding my wrapper class?
>>
>> --
>> Thanks
>> Avi Zohary
>>
>>
>

-- 
Thanks
Avi Zohary


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


Re: Modify how log4J takes location from the stack

Posted by Gary Gregory <ga...@gmail.com>.
Well, I think this would require either for you to compute the location
yourself, or, provide a patch to change Log4j to be able to parametrize it
and surface it to the API.

Gary


On Wed, Nov 27, 2013 at 9:48 AM, Avi Zohary <av...@oracle.com> wrote:

> Hey everyone.
>
> I'm looking into creating a logger framework that will use Log4J2 - I need
> this for various reasons.
>
> I know that in some cases, I will want the debug logs to include the
> location data (what method and in what line the Logger.debug(...) method
> was called).
>
> The problem is, that if I wrap the Log4J2's logger, the location will
> always show the wrapping class's call to the logger, as the location.
>
> I read here ( http://logging.apache.org/log4j/2.x/manual/layouts.html#
> LocationInformation) that to get the location information, the Log4J2
> logger will take a stack and walk a certain depth in it to find the caller.
>
> Is it possible to make Log4J2 change that depth so it will find the method
> that called my wrapper class instead of finding my wrapper class?
>
> --
> Thanks
> Avi Zohary
>
>


-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory