Summary others (@basiljames
, @daker
, @Jay Taylor
) answers here:
my log4j2.xml
Configuration
my case:
<?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:
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.