Log4j2 configuration issues, does noy make sense to me anymmore

Viewed 26

Now I gave up. I have been struggling with this issue for far too long. Configuring log4j makes logical sense and I thought I understand it, but apparently not. Searching for help has not yielded any results and it feels like I always get the same explanations and they do not work. What I am trying to do is control the level and layout of logs I am not really interested in for example everything from org.springframework package would be logged at fatal level using pattern layout X and everything from my own package would be logged at trace level with pattern layout Y. Simple right? So I thought, but this just simply doesn’t seem to work as logic would dictate. It is very possible that there is a very simple error staring at me, but I just cannot see it. I am using Spring Boot (2.6.1 at the moment updating is on my todo list) and AFAIK log4j2 2.17.0. Here is my log4j.properties:

# Extra logging related to initialization of Log4j
# Set to debug or trace if log4j initialization is failing
status = warn
# Name of the configuration
name = ConsoleLogConfigDemo

# Predefine appenders
appenders=FNETC, console

# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ================================ ROOT ========================= %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Console appender configuration
appender.FNETC.type = Console
appender.FNETC.name = fnetLogger
appender.FNETC.layout.type = PatternLayout
appender.FNETC.layout.pattern =[FNet [%-pl]] [c{1} - %m%n]

#log4j.logger.com.orbital3d.server.fnet=trace, fnetc
logger.com.level = TRACE
logger.com.name=com.orbital3d
logger.com.appenderRef = fnetLogger

# Root logger level
rootLogger.level = warn
rootLogger.appenderRef.stdout.ref = consoleLogger

The root logger seems to work, but anything from com.orbital3d.server.fnet does not appear on the console.

1 Answers

Thank you for your comment, it helped to set me on the right track. It appears that at least one of the problems was that due to confusing (at least to me) dependencies caused the problem that I ended up mixing log4j2 and spring boot configuration and searching for help for log4j2 configuration when I should have looked at spring boot configuration. The field of logging can be quite confusing at times and I think it has become too bloated. And while I am ranting on the subject I personally think that one of the reasons for lof4j vulnerability was because of not following SPR and logging utility tries to do more than it should. It is a bummer that it doesn’t do my morning coffee for me.. Yet. And I think this is quite often a problem in other utilities and libraries, not just logging. While I was trying to fix this problem I was able to fix another issue which was double logging. I just needed to add logger.com.additivity=false and figure out that it was not working because of a visit from my good friend Topy! (yes it is a wordplay and it might be funnier if you say it aloud). Just in case it might help someone else (there are a lot of questions why log4j does not work= I’ll include my working solution here:

# Extra logging related to initialization of Log4j
# Set to debug or trace if log4j initialization is failing
status = warn
# Name of the configuration
name = ConsoleLogConfigDemo

# Predefine appenders and loggers
appenders=fnetAppender, console
loggers=fnet,sboot

#####################################
####    APPENDERS   #################
#####################################

# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [root] %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Console appender configuration
appender.fnetAppender.type = Console
appender.fnetAppender.name = fnetLogger
appender.fnetAppender.layout.type = PatternLayout
appender.fnetAppender.layout.pattern = [FNet [%-5p]] [%-25c{1}] - %m]%n

#################################################################################
############### *** Loggers, configured in Spring logging syntex ***  ###########
#################################################################################

#####################################################
####    com.orbital3d.server Logger #################
#####################################################
logger.fnet.level=trace
logger.fnet.name=com.orbital3d.server
logger.fnet.additivity=false
logger.fnet.appenderRef.fnetAppender.ref = fnetLogger

#########################################################
####    org.springframework.boot Logger #################
#########################################################
logger.sboot.level=debug
logger.sboot.name=org.springframework.boot
logger.sboot.additivity=false
logger.sboot.appenderRef.console.ref = consoleLogger

# Root logger level
rootLogger.level = warn
rootLogger.appenderRef.stdout.ref = consoleLogger
Related