7
votes

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>
4

4 Answers

8
votes

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>
1
votes

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

0
votes

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.

0
votes

I added this dependency and it resolved my issue.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>