log4j2 multiple appenders, multiple loggers in config.xml
Asked Answered
R

4

10

I am trying to setup a log4j2 xml config and am not getting the results I desire. I want my console to get level info and above, and the rolling file appender (standard) to get level debug and above. I then want to restrict 3 classes in the console appender to only receive warn and above. However, when I add these logger entries my standard appender also stops receiving the info and debug levels for these 3 classes. What should I do to only restrict console and not standard?

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <param name="threshold" value="info" />
    <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c [%t] %X{ctx} ~ %m%n" />
    </layout> 
</appender> 

<appender name="standard" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="logs/broadcast.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="threshold" value="debug" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE} %-5p %c{1} [%t] %X{ctx} ~ %m%n" />
    </layout>   
</appender>

<appender name="stderr" class="org.apache.log4j.FileAppender">
    <param name="file" value="logs/broadcast.err" />
    <param name="threshold" value="error" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE} %-5p %c{1} [%t] %X{ctx} ~ %m%n" />
    </layout>
</appender>

 <appender name="jlv" class="org.apache.log4j.net.SocketAppender">
    <param name="remoteHost" value="localhost"/>
    <param name="port" value="4445"/>
    <param name="locationInfo" value="true"/>
  </appender>

<logger name="com.mydetv.broadcast.BroadcastLoop">
    <level value="warn"/>
    <appender-ref ref="console" />
</logger>
<logger name="com.mydetv.broadcast.BroadcastUpdate22">
    <level value="warn"/>
    <appender-ref ref="console" />
</logger>
<logger name="com.mydetv.broadcast.vlc.VlcRequest22">
    <level value="warn"/>
    <appender-ref ref="console" />
</logger>

<root> 
    <priority value ="debug" /> 
    <appender-ref ref="standard" /> 
    <appender-ref ref="stderr"/> 
    <appender-ref ref="console" /> 
    <appender-ref ref="jlv"/>
</root>

</log4j:configuration>
Receptor answered 30/12, 2013 at 11:8 Comment(0)
T
10

First, it looks like your config is for log4j-1.x, not log4j2. You'll need to convert this if you want to use log4j2. (The manual has many examples for the new syntax.)

It originally thought you might need to use the log4j2 ThresholdFilter (http://logging.apache.org/log4j/2.x/manual/filters.html#ThresholdFilter ), but this may be a simpler way:

(BTW, the layout patterns in the example config below still need some work to match your original config.)

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"
                 ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy />
    </RollingFile>
    <Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
      <PatternLayout pattern="%m%n"/>
    </Console>
  </Appenders>
  <Loggers>
     <Logger name="com.mysetv.broadcast.BroadcastLoop" level="trace" additivity="false">
      <AppenderRef ref="STDOUT" level="warn" />
      <AppenderRef ref="RollingFile"/>
    </Logger>
... (add other named loggers here)...
   <Root level="trace">
      <AppenderRef ref="STDOUT" level="info" />
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>
Tiossem answered 31/12, 2013 at 0:18 Comment(1)
A word of caution to beginner users: Setting the level on the Logger node acts as a cap or limit. for example, <Logger name="..." level="info"><appender-ref ref="common-log" level="trace" /><Logger>, will cap all your log entries to the common log to INFO. To remove this, you can set the level on the Logger node to all or simply remove it and declare explicitly by the appender.Ives
M
1

If by any chance you want to restrict to use appender to specific LogLevel. Probably the right answer is to use filters. Kindly take a look at specific logger for levels

Maitund answered 31/12, 2013 at 11:20 Comment(0)
K
0

Solved it using GEventEvaluator with custom Groovy scripting: http://logback.qos.ch/manual/filters.html#GEventEvaluator

<configuration>
    <property name="LAYOUT" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n" />

    <root>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator">
                    <expression>e.level.toInt() >= WARN.toInt()</expression>
                </evaluator>
                <OnMatch>ACCEPT</OnMatch>
                <OnMismatch>DENY</OnMismatch>
            </filter>
            <encoder>
                <pattern>${LAYOUT}</pattern>
            </encoder>
        </appender>

        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
                <evaluator class="ch.qos.logback.classic.boolex.GEventEvaluator">
                    <expression>
                        e.level.toInt() >= WARN.toInt() ||
                        (e.level.toInt() >= TRACE.toInt() &amp;&amp; e.getLoggerName().startsWith("my.package"))
                    </expression>
                </evaluator>
                <OnMatch>ACCEPT</OnMatch>
                <OnMismatch>DENY</OnMismatch>
            </filter>
            <encoder>
                <pattern>${LAYOUT}</pattern>
            </encoder>
            <File>target/log/test-log</File>
            <append>false</append>
        </appender>
    </root>
</configuration>

I can't believe this so simple usecase is not supported by default.

Kikelia answered 15/11, 2016 at 21:23 Comment(0)
P
0

I added this dependency and it resolved my issue.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Protector answered 16/11, 2019 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.