Question Details

No question body available.

Tags

java logging configuration log4j2 levels

Answers (2)

Accepted Answer Available
Accepted Answer
January 13, 2026 Score: 2 Rep: 26 Quality: High Completeness: 80%
property.pattern = %d \[%t\] %F %-5L %-5p %c - %m%n

--- Appender 0: Console (All levels) ---

appender.0.type = Console appender.0.name = ALLLOGCONSOLE appender.0.layout.type = PatternLayout appender.0.layout.pattern = ${pattern}

--- Appender 1: All Log File (All levels) ---

appender.1.type = File appender.1.name = ALLLOGFILE appender.1.fileName = logs/all.log appender.1.layout.type = PatternLayout appender.1.layout.pattern = ${pattern}

--- Appender 4: ERROR ONLY ---

appender.4.type = File appender.4.name = ERRORLOGFILE appender.4.fileName = logs/error.log appender.4.layout.type = PatternLayout appender.4.layout.pattern = ${pattern} appender.4.filter.threshold.type = LevelMatchFilter appender.4.filter.threshold.level = ERROR appender.4.filter.threshold.onMatch = ACCEPT appender.4.filter.threshold.onMismatch = DENY

--- Appender 5: WARN ONLY ---

appender.5.type = File appender.5.name = WARNLOGFILE appender.5.fileName = logs/warn.log appender.5.layout.type = PatternLayout appender.5.layout.pattern = ${pattern} appender.5.filter.threshold.type = LevelMatchFilter appender.5.filter.threshold.level = WARN appender.5.filter.threshold.onMatch = ACCEPT appender.5.filter.threshold.onMismatch = DENY

--- Appender 6: INFO ONLY ---

appender.6.type = File appender.6.name = INFOLOGFILE appender.6.fileName = logs/info.log appender.6.layout.type = PatternLayout appender.6.layout.pattern = ${pattern} appender.6.filter.threshold.type = LevelMatchFilter appender.6.filter.threshold.level = INFO appender.6.filter.threshold.onMatch = ACCEPT appender.6.filter.threshold.onMismatch = DENY

--- Appender 7: DEBUG ONLY ---

appender.7.type = File appender.7.name = DEBUGLOGFILE appender.7.fileName = logs/debug.log appender.7.layout.type = PatternLayout appender.7.layout.pattern = ${pattern} appender.7.filter.threshold.type = LevelMatchFilter appender.7.filter.threshold.level = DEBUG appender.7.filter.threshold.onMatch = ACCEPT appender.7.filter.threshold.onMismatch = DENY

--- Appender 8: TRACE ONLY ---

appender.8.type = File appender.8.name = TRACELOGFILE appender.8.fileName = logs/trace.log appender.8.layout.type = PatternLayout appender.8.layout.pattern = ${pattern} appender.8.filter.threshold.type = LevelMatchFilter appender.8.filter.threshold.level = TRACE appender.8.filter.threshold.onMatch = ACCEPT appender.8.filter.threshold.onMismatch = DENY

--- Root Logger Configuration ---

Set level to TRACE so all events are captured, then filtered by appenders

rootLogger.level = TRACE rootLogger.appenderRef.0.ref = ALLLOGCONSOLE rootLogger.appenderRef.1.ref = ALLLOGFILE rootLogger.appenderRef.4.ref = ERRORLOGFILE rootLogger.appenderRef.5.ref = WARNLOGFILE rootLogger.appenderRef.6.ref = INFOLOGFILE rootLogger.appenderRef.7.ref = DEBUGLOGFILE rootLogger.appenderRef.8.ref = TRACELOGFILE

Key Changes Explained

  1. LevelMatchFilter: This is the magic ingredient.

    • onMatch = ACCEPT means if the log level is exactly what we specified, write it.

    • onMismatch = DENY means if it is anything else (even a "higher" level like ERROR in an INFO file), ignore it.

  2. RootLogger Level: I changed the rootLogger.level to TRACE. If the root level is set to something higher (like ERROR), then INFO and DEBUG messages would be discarded before they ever reach the appenders, regardless of the filters.

  3. Removed individual Loggers: In your original snippet, you had specific loggers for names like de.hoelzer.info.log.file. Unless you are specifically creating loggers in Java with those exact names (LogManager.getLogger("de.hoelzer.info.log.file")), they aren't doing anything. It is much cleaner to let the rootLogger handle the flow and let the Appenders decide which file the message goes into based on the level.

January 13, 2026 Score: 0 Rep: 21,494 Quality: Low Completeness: 20%

This is normal behavior for many logging systems.

Setting log level to INFO is asking for that and all higher levels up to FATAL.

Setting log level to WARN is asking for that and all higher levels up to FATAL. Its reduced verbosity level means it's easier for people and programs to pay attention to unfortunate unusual events as they happen, or post mortem. Plus, the size of the log file conveniently consumes fewer resources.

A big advantage of having higher priority messages mixed in with e.g. INFO messages is you see the context, you see the order in which things happened. Trying to tease out a causal event order from just timestamps can be difficult or impossible, depending on stamp resolution and clock drift across several micro-services.