Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j: how to compose config?

Tags:

java

log4j2

I added Log4j2 to the project, and added config.

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Than, I log code like here:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

...

final static Logger logger = LogManager.getLogger(AbstractEditor.class);

...

logger.info("updated: " + entity);
logger.debug("==> debug");
logger.info("==> info");
logger.warn("==> warn");
logger.error("==> error");
logger.fatal("==> fatal");
logger.trace("==> trace");

As I understand, all logs with level higher than DEBUG must be written to console and file. But only this has been printed into console:

15:08:52.285 [http-nio-8080-exec-1] ERROR ru.example.AbstractEditor - ==> error
15:08:52.292 [http-nio-8080-exec-1] FATAL ru.example.AbstractEditor - ==> fatal

I see this strings not matches my config. And they are not witten into file. When I added this config, all logs disappeared from console, excluding this 2 strings.

Please help to write config to see all logs with level from DEBUG on console and file.

like image 670
Вячеслав Чернышов Avatar asked Oct 13 '25 02:10

Вячеслав Чернышов


1 Answers

You are programmatically using Log4j 2 but using the configuration format of Log4j 1. Log4j 2 is ignoring your configuration and using the default. You have 2 choices.

  1. Convert your configuration to use Log4j 2 syntax (recommended), or
  2. Enable Log4j 2's experimental support for log4j 1 configuration files by setting the system property "log4j1.compatibility=true". This support was added in release 2.13.0 so you would have to be using that version. The property file would also have to be named log4j.properties, not log4j2.properties.
like image 151
rgoers Avatar answered Oct 14 '25 15:10

rgoers