how to include xml in log-back.xml
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Logback lets you split a large logback.xml file into smaller XML fragments. The mechanism is the <include> element, which can pull configuration from another file, a classpath resource, or a URL.
Include Another XML Fragment with <include>
The main configuration file stays rooted at configuration. Inside it, add an include directive before you reference the included appenders or loggers.
The included XML file must not use configuration as its root. According to the Logback manual, included content must be wrapped in an included element.
That requirement is the detail most people miss. If you put a full second configuration block inside the included file, Logback will not treat it as a valid include fragment.
file, resource, and url Variants
Logback supports three ways to locate the included XML:
- '
filefor a filesystem path' - '
resourcefor a classpath resource' - '
urlfor a remote XML location'
Examples:
In most application code, resource is the cleanest choice because the file travels with the application on the classpath.
Optional Includes
If an included file is environment-specific and may not exist everywhere, mark it optional:
Without optional="true", Logback reports a status warning when it cannot find the target. That warning is useful for required files, but noisy for local-only overrides.
A Practical Layout Pattern
A common way to organize Logback configuration is:
- keep
logback.xmlas the top-level composition file - move shared appenders into one included fragment
- move environment-specific logger levels into another fragment
That keeps the root file readable and reduces copy-paste across services.
It also makes reuse easier across multiple applications. For example, a company-wide console appender pattern can live in one shared classpath resource, while each service keeps only its own logger names and thresholds in the top-level file.
Watch Out for Relative File Paths
When you use the file attribute, relative paths can be misleading. The Logback manual notes that the current directory is defined by the application and is not necessarily related to the location of logback.xml.
That means this:
may work in one launch setup and fail in another. If the file lives in your application resources, resource is usually more portable than file.
Common Pitfalls
The most common mistake is giving the included file a configuration root instead of included. Logback expects a fragment, not a second full top-level configuration document.
Another issue is referencing an appender before the include has loaded it. Keep include statements near the top of the main configuration so later sections can safely refer to the imported names.
Be careful with relative filesystem paths. They depend on the process working directory, which may differ between local runs, tests, and production.
If an include seems ignored, enable Logback status output during startup and check the reported resource path. Most include bugs turn out to be either the wrong root element or the wrong location attribute.
Summary
- Use
<include>insidelogback.xmlto load XML fragments from a file, resource, or URL. - Wrap the target fragment in an
includedroot element, not anotherconfigurationelement. - Prefer
resourcefor bundled classpath configuration files. - Use
optional="true"for environment-specific includes that may be absent. - Be cautious with relative
filepaths because they depend on the application's working directory.
Related reading
- How to increase the maximum size of the AWS lambda deployment package RequestEntityTooLargeException?
- How to inject active spring profile into logback
- How to Inject Environment Variables into Kubernetes Pods Before Deployment
- How to inject kubernetes secret into configuration file
- How to install docker on Amazon Linux2
- How to install git on a docker ubuntu image?
- How to install Hive Metastore in Kubernetes?
- How to install NGINX on AWS EC2 Linux 2

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.