M
M
MaxLich2017-10-30 16:42:41
Java
MaxLich, 2017-10-30 16:42:41

How to simultaneously write messages with different levels to different files in log4j?

Hello. I'm struggling with a problem, I can't solve it. I want to make sure that all messages of the level up to TRACE get into one file, and messages with a level not lower than INFO get into the other file and into the console. I need it to happen at the same time. I can only do one thing. (In general, I can’t understand how the loggers from the configuration file correlate with the loggers from the code (objects)).
Tried threshold and filters but doesn't help. Here is my configuration file:

log4j config file
# Базовый уровень логирования
log4j.rootLogger=INFO, info_log_file, stdout

# Апендер для работы с файлами
log4j.appender.info_log_file=org.apache.log4j.RollingFileAppender
log4j.appender.info_log_file.File=info_log.log
log4j.appender.info_log_file.MaxFileSize=1MB
log4j.appender.info_log_file.MaxBackupIndex=10
log4j.appender.info_log_file.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.info_log_file.layout.ConversionPattern=%d{ISO8601} [%-5p][%20c{-2}] - %m%n

#вывод в консоль
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [%-5p][%20c{-2}] - %m%n

# Уровень логирования (для постоянно работы приложения на сервере)
log4j.logger.info=TRACE, full_log_file

# Апендер для работы с файлами
log4j.appender.full_log_file=org.apache.log4j.RollingFileAppender
log4j.appender.full_log_file.File=full_log.log
#log4j.appender.full_log_file.threshold=TRACE
#log4j.appender.full_log_file.filter.a=org.apache.log4j.varia.LevelRangeFilter
#log4j.appender.full_log_file.filter.a.LevelMin=TRACE
#log4j.appender.full_log_file.filter.a.LevelMax=DEBUG
#log4j.appender.full_log_file.filter.a.AcceptOnMatch=TRUE
log4j.appender.full_log_file.MaxFileSize=1MB
log4j.appender.full_log_file.MaxBackupIndex=10
log4j.appender.full_log_file.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.full_log_file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}:%L] - %m%n

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxLich, 2017-10-30
@MaxLich

So far, removing the level from the logger declaration (in the config; root logger) and adding its own level to each appender (through threshold) has helped. Here is the modified log4j.properties:

content of log4j.properties file
# Базовый уровень логирования
log4j.rootLogger=TRACE, info_log_file, stdout, full_log_file

# Апендер для работы с файлами
log4j.appender.info_log_file=org.apache.log4j.RollingFileAppender
log4j.appender.info_log_file.File=info_log.log
log4j.appender.info_log_file.threshold=INFO
log4j.appender.info_log_file.MaxFileSize=1MB
log4j.appender.info_log_file.MaxBackupIndex=10
log4j.appender.info_log_file.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.info_log_file.layout.ConversionPattern=%d{ISO8601} [%-5p][%20c{-2}] - %m%n

#вывод в консоль
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} [%-5p][%20c{-2}] - %m%n

# Апендер для работы с файлами
log4j.appender.full_log_file=org.apache.log4j.RollingFileAppender
log4j.appender.full_log_file.File=full_log.log
log4j.appender.full_log_file.threshold=TRACE
log4j.appender.full_log_file.MaxFileSize=1MB
log4j.appender.full_log_file.MaxBackupIndex=10
log4j.appender.full_log_file.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.full_log_file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}:%L] - %m%n

But I'm not sure if this is the correct solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question