Reduce multiple appenders and loggers

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Reduce multiple appenders and loggers

M Sazzadul Hoque
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
Reply | Threaded
Open this post in threaded view
|

Re: Reduce multiple appenders and loggers

Matt Sicker
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 <[hidden email]>
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 <[hidden email]>