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 M Sazzadul Hoque <sa...@gmail.com> on 2017/06/12 10:20:26 UTC
Reduce multiple appenders and loggers
Hello,
My application has several dependencies, some of which have other
dependencies. I am using Log4j2 for logging and wanted to separate
application logs from dependency logs.
Below is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<property name="rollSuffix">%d{yyyyMMdd}</property>
<property name="commonPattern">%d %level %class %method -
%msg%n</property>
</Properties>
<Appenders>
<RollingFile name="xLog"
fileName="x.log"
filePattern="x-${rollSuffix}.log"
immediateFlush="true" >
<PatternLayout pattern="${commonPattern}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="yLog"
fileName="y.log"
filePattern="y-${rollSuffix}.log"
immediateFlush="true" >
<PatternLayout pattern="${commonPattern}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="zLog"
fileName="z.log"
filePattern="z-${rollSuffix}.log"
immediateFlush="true" >
<PatternLayout pattern="${commonPattern}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="appLog"
fileName="app.log"
filePattern="app-${rollSuffix}.log"
immediateFlush="false" >
<PatternLayout pattern="%d %-5p - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.x" level="error" additivity="false" >
<AppenderRef ref="xLog" />
</Logger>
<Logger name="io.y" level="error" additivity="false" >
<AppenderRef ref="yLog" />
</Logger>
<Logger name="org.z" level="error" additivity="false" >
<AppenderRef ref="zLog" />
</Logger>
<Root level="all">
<AppenderRef ref="appLog" />
</Root>
</Loggers>
</Configuration>
As you can see, for each x, y, z, etc modules, I have defined an appender
and a logger. This made the xml too long and maintenance has become more
and more difficult.
I have some questions:
1. I had found out that multiple loggers should not refer to same file
appender (e.g. FileAppender, RollingFileAppender etc). Is it still valid
for latest versions?
2. Is there a way to shrink the xml by reducing both appenders and loggers?
3. Is it a feasible feature for log4j2 so that a logger can have multiple
names? (I am thinking about requesting the feature)
For example:
<Logger level="error" additivity="false" >
<Names>
<Name name="com.x" />
<Name name="io.y" />
<Name name="org.z" />
</Names>
<AppenderRef ref="xLog" />
</Logger>
Regards,
sazzad
Re: Reduce multiple appenders and loggers
Posted by Matt Sicker <bo...@gmail.com>.
Take a look at filters:
https://logging.apache.org/log4j/2.x/manual/filters.html
Also probably helpful if you're trying to break it down into multiple
files:
https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender
On 12 June 2017 at 05:20, M Sazzadul Hoque <sa...@gmail.com>
wrote:
> Hello,
>
> My application has several dependencies, some of which have other
> dependencies. I am using Log4j2 for logging and wanted to separate
> application logs from dependency logs.
>
> Below is my log4j2.xml
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Configuration>
> <Properties>
> <property name="rollSuffix">%d{yyyyMMdd}</property>
> <property name="commonPattern">%d %level %class %method -
> %msg%n</property>
> </Properties>
> <Appenders>
> <RollingFile name="xLog"
> fileName="x.log"
> filePattern="x-${rollSuffix}.log"
> immediateFlush="true" >
> <PatternLayout pattern="${commonPattern}" />
> <Policies>
> <TimeBasedTriggeringPolicy interval="1" modulate="true" />
> </Policies>
> </RollingFile>
> <RollingFile name="yLog"
> fileName="y.log"
> filePattern="y-${rollSuffix}.log"
> immediateFlush="true" >
> <PatternLayout pattern="${commonPattern}" />
> <Policies>
> <TimeBasedTriggeringPolicy interval="1" modulate="true" />
> </Policies>
> </RollingFile>
> <RollingFile name="zLog"
> fileName="z.log"
> filePattern="z-${rollSuffix}.log"
> immediateFlush="true" >
> <PatternLayout pattern="${commonPattern}" />
> <Policies>
> <TimeBasedTriggeringPolicy interval="1" modulate="true" />
> </Policies>
> </RollingFile>
> <RollingFile name="appLog"
> fileName="app.log"
> filePattern="app-${rollSuffix}.log"
> immediateFlush="false" >
> <PatternLayout pattern="%d %-5p - %m%n" />
> <Policies>
> <TimeBasedTriggeringPolicy interval="1" modulate="true" />
> </Policies>
> </RollingFile>
> </Appenders>
> <Loggers>
> <Logger name="com.x" level="error" additivity="false" >
> <AppenderRef ref="xLog" />
> </Logger>
> <Logger name="io.y" level="error" additivity="false" >
> <AppenderRef ref="yLog" />
> </Logger>
> <Logger name="org.z" level="error" additivity="false" >
> <AppenderRef ref="zLog" />
> </Logger>
> <Root level="all">
> <AppenderRef ref="appLog" />
> </Root>
> </Loggers>
> </Configuration>
>
> As you can see, for each x, y, z, etc modules, I have defined an appender
> and a logger. This made the xml too long and maintenance has become more
> and more difficult.
>
> I have some questions:
>
> 1. I had found out that multiple loggers should not refer to same file
> appender (e.g. FileAppender, RollingFileAppender etc). Is it still valid
> for latest versions?
>
> 2. Is there a way to shrink the xml by reducing both appenders and loggers?
>
> 3. Is it a feasible feature for log4j2 so that a logger can have multiple
> names? (I am thinking about requesting the feature)
> For example:
> <Logger level="error" additivity="false" >
> <Names>
> <Name name="com.x" />
> <Name name="io.y" />
> <Name name="org.z" />
> </Names>
> <AppenderRef ref="xLog" />
> </Logger>
>
> Regards,
> sazzad
>
--
Matt Sicker <bo...@gmail.com>