log4j2 xml configuration - Log to file and console

2019-03-09 16:30发布

I want to do two things:

  1. Log to console with a certain log-level
  2. Log to file with another log-level

Console logging seems to work just fine but the log file keeps beeing empty.

This is my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
  <appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>

    <File name="MyFile" fileName="logs/app.log" immediateFlush="true">
        <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>  

  </appenders>
  <loggers>

    <logger name="filelogger" level="error">
        <appender-ref ref="MyFile"/>
    </logger>

    <root level="info">
      <appender-ref ref="Console"/>
    </root>
  </loggers>
</configuration>

What might be wrong?

标签: xml log4j log4j2
4条回答
ら.Afraid
2楼-- · 2019-03-09 16:56

<logger name="filelogger" level="error" >
This should be the problem. The name of the logger usually is your package name (unless you have specifically named it filelogger).
Try <logger name="com.yourpackage" level="error" additivity="true">

Refer Log4j2 Doc

查看更多
够拽才男人
3楼-- · 2019-03-09 16:58

Although Daker had put the corrected configuration file but he didn't explain it. I would like to add explanation here. As quoted in Log4j2 Documentation here, usage of <Logger> tag was not required for the given requirement. Further when you should use <Logger> tag? Read below explanation form the documentation,

Perhaps it is desired to eliminate all the TRACE output from everything except com.foo.Bar. Simply changing the log level would not accomplish the task. Instead, the solution is to add a new logger definition to the configuration:

<Loggers>
  <Logger name="com.foo.Bar" level="TRACE"/> 
  <Root level="ERROR">  
    <AppenderRef ref="STDOUT"> 
  </Root>
  ...
</Loggers>
查看更多
forever°为你锁心
4楼-- · 2019-03-09 17:01

I figured it out! The <Logger> tag shouldn't be used in this case, see Gaurang Patel's answer for details.

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
  <appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>

    <File name="MyFile" fileName="logs/app.log">
        <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>             
  </appenders>

  <loggers>     
    <root level="debug">
      <appender-ref ref="Console" level="info"/>
      <appender-ref ref="MyFile" level="error"/>
    </root>    
  </loggers>
</configuration>
查看更多
霸刀☆藐视天下
5楼-- · 2019-03-09 17:05

I use <ThresholdFilter /> and <AppenderRef level=""> to do this

  • Console: output all
  • app.log: >= info, except error
  • error.log: >= error

SEE

  • <ThresholdFilter level="error" onMatch="DENY" onMismatch="ACCEPT"/>

  • <AppenderRef ref="fileLogger" level="info" />

<?xml version="1.0" encoding="UTF-8"?>      
<Configuration status="error" monitorInterval="5"> <!-- status:error mean ONLY show log4j kernel's error log in console-->
    <Properties>
        <Property name="APP_LOG_ROOT">Your log's path</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
        <RollingFile name="fileLogger" fileName="${APP_LOG_ROOT}/app.log" filePattern="${APP_LOG_ROOT}/app-%d{yyyy-MM-dd}.log">
            <!-- Except Error -->
            <ThresholdFilter level="error" onMatch="DENY" onMismatch="ACCEPT"/>
            <PatternLayout>
                <pattern>%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %level %logger{36} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
        <RollingFile name="errorLogger" fileName="${APP_LOG_ROOT}/error.log" filePattern="${APP_LOG_ROOT}/error-%d{yyyy-MM-dd}.log">
            <PatternLayout>
                <pattern>%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %level %logger{36} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="trace" >
            <AppenderRef ref="Console" level="trace" />
            <AppenderRef ref="fileLogger" level="info" />
            <AppenderRef ref="errorLogger" level="error" />
        </Root>
    </Loggers>
</Configuration>
查看更多
登录 后发表回答