Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To configure Log4j 2 with YAML, put a file named log4j2.yaml or log4j2.yml on the application’s runtime classpath, include Log4j Core and the Jackson YAML dependencies, then define appenders and loggers under a Configuration root. Log4j reads the YAML as a tree of configuration nodes; it is not a flat set of key-value properties.

What dependencies does Log4j 2 need for YAML?

Log4j Core is the logging implementation that loads the configuration. YAML configuration also requires these Jackson modules at runtime:

  • com.fasterxml.jackson.core:jackson-databind
  • com.fasterxml.jackson.dataformat:jackson-dataformat-yaml

Apache’s current Log4j manual examples show Jackson 2.19.2. Treat that as the version in those examples, not a requirement to override your application’s dependency management: use the version supplied by your dependency platform where applicable, and keep the Jackson modules compatible with one another.

Where should log4j2.yaml go?

Place the file on the application’s runtime classpath. In a typical Maven or Gradle project, that means src/main/resources/log4j2.yaml. The filename must use a supported extension such as .yaml or .yml; Log4j infers the configuration format from it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Log4j Core looks for classpath configuration files in this order:

  1. log4j2-test<contextName>.*
  2. log4j2-test.*
  3. log4j2<contextName>.*
  4. log4j2.*

Use a test-specific filename when a test configuration should take precedence over the ordinary application configuration. If the file is outside the classpath, set the system property log4j2.configurationFile to its location; for example, pass -Dlog4j2.configurationFile=/path/to/log4j2.yaml to the JVM.

What does a minimal Log4j YAML configuration look like?

This configuration sends INFO-and-higher root logger events to the console, using a pattern that includes the date, thread, level, abbreviated logger name, and message:

Configuration:
  Appenders:
    Console:
      name: CONSOLE
      PatternLayout:
        pattern: "%d [%t] %5p %c{1.} - %m%n"
  Loggers:
    Root:
      level: INFO
      AppenderRef:
        ref: CONSOLE

The AppenderRef value must match the appender’s name. Without that connection, the root logger is not directed to this console appender.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How does Log4j interpret YAML mappings?

Each YAML mapping represents a Log4j configuration node. The structure mirrors the plugin tree: for example, Configuration contains Appenders and Loggers, while an appender can contain a layout.

  • Scalar properties become plugin attributes. In the example, level: INFO sets the root logger’s level.
  • Collection-valued properties become nested configuration elements. PatternLayout is nested under Console.
  • Log4j determines a plugin’s type from a type key when present; otherwise, it uses the mapping key. For a block sequence, the sequence key supplies the type.

Use a block sequence for repeatable plugins, such as multiple file appenders:

File:
  - name: file1
  - name: file2

How can Log4j reload YAML changes without restarting?

Set monitorInterval to a positive number of seconds in the configuration to have Log4j poll for file changes and reconfigure. A value of 0 disables polling. Reloading is not a guarantee that every edit will be applied: Log4j may ignore a replacement that could lose events, such as changing a file appender’s append mode without changing its file name.

Why is the YAML configuration ignored?

If Log4j cannot find a configuration file, Core falls back to DefaultConfiguration and the status logger prints a warning. Check these causes in order:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Confirm the file is packaged. Inspect the built application artifact or runtime classpath to make sure the YAML file is present.
  2. Check its location and spelling. It must be visible on the runtime classpath and named with a supported pattern, such as log4j2.yaml.
  3. Check YAML support dependencies. Ensure Log4j Core, Jackson Databind, and Jackson YAML are available at runtime.
  4. Check for a higher-priority file. A matching test-specific or context-specific configuration name can be discovered before log4j2.yaml.
  5. Check the explicit configuration property. If log4j2.configurationFile is set, confirm it points to the intended file.

The status configuration attribute is deprecated starting with Log4j 2.24.0. For the status logger level, use the log4j2.statusLoggerLevel configuration property instead.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Should you use YAML instead of XML, JSON, or properties?

Choose YAML when the nested appender, layout, and logger structure is easier for your team to read in an indented tree. Its main operational trade-off is the additional Jackson runtime dependencies. Java properties are less suited to hierarchical configuration; XML or JSON may fit better if they already match your application’s conventions. Regardless of format, Log4j’s configuration is built around its plugin tree, and changing formats does not make Log4j 1 configuration syntax compatible.

Can you reuse a Log4j 1 configuration in Log4j 2?

No. Log4j 1 configuration syntax differs from Log4j 2 syntax and is ignored by default. Convert the old configuration into Log4j 2’s plugin tree—appenders, layouts, loggers, and references—rather than copying it verbatim. Log4j 2 configuration syntax is part of the public API and has remained stable across significant upgrades, but that stability does not make Log4j 1 files interchangeable with Log4j 2 files.

Is YAML configuration the same as YAML log output?

No. A YAML configuration file tells Log4j how to route and format events. YamlLayout, by contrast, serializes log events as YAML. Apache says YamlLayout is planned for removal in the next major release and advises existing users to migrate to JSON Template Layout.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.