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 Gerard Gagliano <gg...@me.com> on 2017/04/03 15:29:02 UTC
Can one class log to different files depending on context?
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/>
Re: Can one class log to different files depending on context?
Posted by Gerard Gagliano <gg...@me.com>.
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 <mi...@magine.com> 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 <ggnm@me.com <ma...@me.com>> 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*
> mikael.staldal@magine.com <ma...@magine.com>
> 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.
Re: Can one class log to different files depending on context?
Posted by Mikael Ståldal <mi...@magine.com>.
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 <gg...@me.com> 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*
mikael.staldal@magine.com
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.