Changing Logging Configuration via Command Line in Java + Bonus

(5 minutes read)

When it comes to managing logs in Java applications, having flexibility in how logging is configured is key. Whether you’re debugging an application or want to increase logging verbosity in production, adjusting the logging configuration from the command line can be a lifesaver. This article explores how to modify logging settings on the fly with popular Java logging frameworks, offering quick examples for each.


1. Log4j2

Apache Log4j2 is a widely used and highly configurable logging framework that supports runtime configuration changes.

Command Line Configuration Example

You can specify a custom configuration file path via the command line:

java -Dlog4j.configurationFile=/path/to/log4j2.xml -jar yourapp.jar

Adjusting Log Level for a Specific Package

To adjust logging for a specific package:

java -Dlog4j2.logger.myPackage.level=DEBUG -jar yourapp.jar

Replace myPackage with the desired package name. This flexibility makes Log4j2 a strong choice when you need runtime configuration control.

Example

Suppose you want to log all database interactions in detail without modifying the XML file:

java -Dlog4j2.logger.com.myapp.database.level=TRACE -jar yourapp.jar

2. Logback

Logback is another popular logging framework used as the default for SLF4J. It’s known for its speed and simplicity.

Command Line Configuration Example

Specify a custom configuration file directly from the command line:

java -Dlogback.configurationFile=/path/to/logback.xml -jar yourapp.jar

Changing Log Level for a Package

To change the log level for a specific package, you’ll typically need to specify it in the configuration file. Unlike Log4j2, Logback doesn’t support changing individual log levels dynamically via command-line arguments. However, you can create different configurations per environment.

Example

For a custom configuration:

java -Dlogback.configurationFile=/path/to/dev-logback.xml -jar yourapp.jar

If dynamic logging adjustment is crucial, you might find Logback’s limitations a dealbreaker.

3. Java Util Logging (JUL)

Java Util Logging (JUL) comes built-in with Java, though it’s generally less powerful compared to Log4j2 and Logback.

Command Line Configuration Example

You can set the configuration file location like so:

java -Djava.util.logging.config.file=/path/to/logging.properties -jar yourapp.jar

Adjusting Log Level for a Specific Package

To change the logging level for a particular package or class, you can update your logging.properties file:

com.myapp.database.level = FINE

JUL doesn’t support command-line level adjustments for packages on the fly. You must alter the properties file and restart the application.

Example

Place this in logging.properties:

handlers = java.util.logging.ConsoleHandler
.level = INFO
com.myapp = FINE

Explanation:

  • handlers=java.util.logging.ConsoleHandler specifies the handler, which in this case is the console handler.
  • .level=INFO sets the global logging level to INFO.
  • com.myapp.level=FINE sets the log level specifically for the com.myapp package to FINE.

4. SLF4J

SLF4J (Simple Logging Facade for Java) is a facade, meaning it’s not a logging implementation itself. Instead, it binds to implementations like Log4j2 or Logback.

Since SLF4J is only a facade, its configuration depends on the backend (e.g., Log4j2 or Logback). You won’t pass configurations directly to SLF4J via the command line, but rather to the underlying framework it’s bound to. For instance, if SLF4J is paired with Log4j2, you can pass Log4j2’s configuration properties as shown earlier.


Spring Boot Logging

Spring Boot applications use Logback by default but also support Log4j2. This framework makes it easy to adjust logging properties directly from the command line, even without explicit logging dependencies.

Command Line Configuration Example

To change logging levels directly:

java -Dlogging.level.com.myapp=DEBUG -jar yourapp.jar

Changing the Default Logback Configuration

Spring Boot allows passing a different configuration file:

java -Dlogging.config=/path/to/custom-logback.xml -jar yourapp.jar

Example

To adjust log levels for multiple packages:

java -Dlogging.level.com.myapp.database=TRACE -Dlogging.level.com.myapp.web=INFO -jar yourapp.jar

Using Environment-Specific Logging in application.properties

For Spring Boot applications, you can set environment-specific logging levels without using the command line:

logging.level.com.myapp.database=TRACE
logging.level.com.myapp.web=INFO

Summary: Command Line Logging Configurations in Java

LibraryCommand Line Configuration FileCommand Line Package Configuration
Log4j2-Dlog4j.configurationFile=/path/to/log4j2.xml-Dlog4j2.logger.myPackage.level= DEBUG
Logback-Dlogback.configurationFile=/path/to/logback.xmlN/A (Requires XML file adjustment)
JUL-Djava.util.logging.config.file= /path/to/logging.propertiesN/A (Requires properties file adjustment)
SLF4JDepends on the bound logging frameworkN/A
Spring Boot-Dlogging.config=/path/to/custom-logback.xml-Dlogging.level.com.myapp=TRACE
Table 1 – Comparison of Command line arguments for each Java logging library

In conclusion, Log4j2 and Spring Boot provide the most flexibility for runtime logging adjustments directly from the command line, while Logback and JUL require configuration file edits. Knowing these commands gives you the power to diagnose issues on the fly, making your logging setup versatile and responsive to different environments.


Bonus Material: Configuration Hierarchy in Logging Libraries

This hierarchy ensures that any urgent configuration changes, especially in production, can be applied immediately without modifying source code or configuration files.

Log4j2 Configuration Hierarchy

In Log4j2, command-line parameters have the highest priority, followed by environment variables, and lastly, properties in configuration files.

Example

  1. Configuration File (log4j2.xml):
   <Configuration>
       <Loggers>
           <Logger name="com.myapp" level="INFO"/>
       </Loggers>
   </Configuration>
  1. Environment Variable:
   export LOG4J_LEVEL_com_myapp=DEBUG
  1. Command Line:
   java -Dlog4j2.logger.com.myapp.level=ERROR -jar yourapp.jar

Effective Configuration: The log level for com.myapp will be ERROR, as command-line arguments override both the environment variable and configuration file.

Spring Boot Configuration Hierarchy

Spring Boot provides flexibility by allowing logging configurations through command-line arguments, environment variables, and configuration files. The command-line arguments hold the highest priority, making Spring Boot ideal for quick logging adjustments.

Example

  1. Configuration File (application.properties):
   logging.level.com.myapp=INFO
  1. Environment Variable:
   export LOGGING_LEVEL_COM_MYAPP=DEBUG
  1. Command Line:
   java -Dlogging.level.com.myapp=TRACE -jar yourapp.jar

Effective Configuration: The log level for com.myapp will be TRACE, with the command-line argument overriding both the environment variable and application.properties setting.