log4j2 xml configuration - Log to file and console (with different levels)
Asked Answered
U

5

53

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?

Ultimately answered 2/7, 2013 at 6:7 Comment(0)
U
82

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>
Ultimately answered 2/7, 2013 at 11:35 Comment(3)
so what was the problem? posting a solution without an explanation doesn't helpBadderlocks
@Ultimately Hi, is it possible to specify a path where I want to save the file if I am running a war file on a tomcat server?Nosh
@Nosh I would assume you just change the path in the fileName tag but I don't know.Ultimately
K
20

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>
Katrinka answered 19/5, 2015 at 9:39 Comment(0)
C
9

Summary others (@basiljames, @daker, @Jay Taylor) answers here:

my log4j2.xml Configuration

my case:

  • log4j2 version: 2.13.0

  • log4j2.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="FILEOUT" fileName="your_log_filename.log" append="false">
      <PatternLayout>
        <Pattern>%d{yyyyMMdd HH:mm:ss} %-5p [%t] %C{2} %F%L - %m%n</Pattern>
      </PatternLayout>
    </File>

    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p %F:%L - %m%n"/>
    </Console>
  </Appenders>

  <Loggers>
    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>
  </Loggers>
</Configuration>

Q: how to implement File and Console with different level ?

A: as official doc also mentioned, the core part:

    <Root level="debug">
      <AppenderRef ref="FILEOUT" level="debug"/>
      <AppenderRef ref="STDOUT" level="info"/>
    </Root>

can achieve:

based on:

  • root level is DEBUG

set:

  • File level to DEBUG
  • Console level to INFO

Q: @Stealth Rabbi: so what was the problem?

A: original main problem is:

the logger name syntax is wrong

that is name="filelogger" in:

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

for normally the logger name is your class name, like

    <Logger name="com.foo.bar" level="error">
        <AppenderRef ref="MyFile"/>
    </Logger>

another possible minor problem is:

log content level is below your file level error, so file created but empty

when set file level to error, but your log code is low level, such as

logger.debug("output something");

that is:

logger lever in code(debug) < file level(error)

so debug content will NOT output to log file, log file keep empty.

Q: @Bendemann is it possible to specify a path where I want to save the file if I am running a war file on a tomcat server?

A: yes. Just set the relative or absolute log file path to File's fileName.

just like:

  <Appenders>
    <File name="FILEOUT" fileName="/your/path/your_log_filename.log" append="false">
...

is ok.

Coulometer answered 1/2, 2020 at 4:5 Comment(0)
H
7

<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

Harrod answered 2/7, 2013 at 6:41 Comment(3)
Thanks. It worked for the file logging but now the console output disappeared. Something wrong there as well?Ultimately
Edited my answer added 'additivity="true"`Harrod
additivity is true by default in log4j2Livingstone
R
1

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>
Recount answered 27/7, 2018 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.