Can one class log to different files depending on context?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Can one class log to different files depending on context?

Gerard Gagliano
I have a structure as follows: Class A is started via main method Class A starts Class B using ProcessBuilder (via main method of course) Class B starts Class C using its Constructor
I want each class to have its own log and to use a single log4j2.xml file.

Appender has the following:

<RollingFile
    name="file"
    fileName="${sys:loggingFileName}.log"
    …
</RollingFile>

Each Class has code similar to this:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.slf4j.LoggerFactory;

private static final Logger log = LoggerFactory.getLogger (A.class);

System.setProperty ("loggingFileName", “A”);
LoggerContext context = (LoggerContext) LogManager.getContext (false);

If there were only these classes involved, everything would work, but there are many other classes that do logging.

For example, Class D is referenced by Classes A, B, and C and only has this code:

private static final Logger log = LoggerFactory.getLogger (D.class);

I would like the output from Class D referenced from Class A to be in log A, but it may wind up in log A, B, or C.

Is there a way to construct the logs so that output for Class D winds up in: log A when referenced from A log B when referenced from B log C when referenced from C.




--
Gerard Gagliano
505-792-3331 Office
505-463-3999 Mobile

Prodentity
Corrales, NM 87048-6935
http://www.prodentity.com <http://www.prodentity.com/>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can one class log to different files depending on context?

Mikael Ståldal-3
Have you tried to use RoutingAppender (
https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender)
together with thread context?

On Mon, Apr 3, 2017 at 5:29 PM, Gerard Gagliano <[hidden email]> wrote:

> I have a structure as follows: Class A is started via main method Class A
> starts Class B using ProcessBuilder (via main method of course) Class B
> starts Class C using its Constructor
> I want each class to have its own log and to use a single log4j2.xml file.
>
> Appender has the following:
>
> <RollingFile
>     name="file"
>     fileName="${sys:loggingFileName}.log"
>     …
> </RollingFile>
>
> Each Class has code similar to this:
>
> import org.apache.logging.log4j.LogManager;
> import org.apache.logging.log4j.core.LoggerContext;
> import org.slf4j.LoggerFactory;
>
> private static final Logger log = LoggerFactory.getLogger (A.class);
>
> System.setProperty ("loggingFileName", “A”);
> LoggerContext context = (LoggerContext) LogManager.getContext (false);
>
> If there were only these classes involved, everything would work, but
> there are many other classes that do logging.
>
> For example, Class D is referenced by Classes A, B, and C and only has
> this code:
>
> private static final Logger log = LoggerFactory.getLogger (D.class);
>
> I would like the output from Class D referenced from Class A to be in log
> A, but it may wind up in log A, B, or C.
>
> Is there a way to construct the logs so that output for Class D winds up
> in: log A when referenced from A log B when referenced from B log C when
> referenced from C.
>
>
>
>
> --
> Gerard Gagliano
> 505-792-3331 Office
> 505-463-3999 Mobile
>
> Prodentity
> Corrales, NM 87048-6935
> http://www.prodentity.com <http://www.prodentity.com/>
>



--
[image: MagineTV]

*Mikael Ståldal*
Senior software developer

*Magine TV*
[hidden email]
Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com

Privileged and/or Confidential Information may be contained in this
message. If you are not the addressee indicated in this message
(or responsible for delivery of the message to such a person), you may not
copy or deliver this message to anyone. In such case,
you should destroy this message and kindly notify the sender by reply
email.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Can one class log to different files depending on context?

Gerard Gagliano
I didn’t know about the Routing appender.  I’ll check it out next week.

Thanks.

> On Apr 3, 2017, at 9:45 AM, Mikael Ståldal <[hidden email]> wrote:
>
> Have you tried to use RoutingAppender (
> https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender <https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender>)
> together with thread context?
>
> On Mon, Apr 3, 2017 at 5:29 PM, Gerard Gagliano <[hidden email] <mailto:[hidden email]>> wrote:
>
>> I have a structure as follows: Class A is started via main method Class A
>> starts Class B using ProcessBuilder (via main method of course) Class B
>> starts Class C using its Constructor
>> I want each class to have its own log and to use a single log4j2.xml file.
>>
>> Appender has the following:
>>
>> <RollingFile
>>    name="file"
>>    fileName="${sys:loggingFileName}.log"
>>    …
>> </RollingFile>
>>
>> Each Class has code similar to this:
>>
>> import org.apache.logging.log4j.LogManager;
>> import org.apache.logging.log4j.core.LoggerContext;
>> import org.slf4j.LoggerFactory;
>>
>> private static final Logger log = LoggerFactory.getLogger (A.class);
>>
>> System.setProperty ("loggingFileName", “A”);
>> LoggerContext context = (LoggerContext) LogManager.getContext (false);
>>
>> If there were only these classes involved, everything would work, but
>> there are many other classes that do logging.
>>
>> For example, Class D is referenced by Classes A, B, and C and only has
>> this code:
>>
>> private static final Logger log = LoggerFactory.getLogger (D.class);
>>
>> I would like the output from Class D referenced from Class A to be in log
>> A, but it may wind up in log A, B, or C.
>>
>> Is there a way to construct the logs so that output for Class D winds up
>> in: log A when referenced from A log B when referenced from B log C when
>> referenced from C.
>>
>>
>>
>>
>> --
>> Gerard Gagliano
>> 505-792-3331 Office
>> 505-463-3999 Mobile
>>
>> Prodentity
>> Corrales, NM 87048-6935
>> http://www.prodentity.com <http://www.prodentity.com/> <http://www.prodentity.com/ <http://www.prodentity.com/>>
>>
>
>
>
> --
> [image: MagineTV]
>
> *Mikael Ståldal*
> Senior software developer
>
> *Magine TV*
> [hidden email] <mailto:[hidden email]>
> Grev Turegatan 3  | 114 46 Stockholm, Sweden  |   www.magine.com <http://www.magine.com/>
>
> Privileged and/or Confidential Information may be contained in this
> message. If you are not the addressee indicated in this message
> (or responsible for delivery of the message to such a person), you may not
> copy or deliver this message to anyone. In such case,
> you should destroy this message and kindly notify the sender by reply
> email.

Loading...