Over a million developers have joined DZone.

Configuring SLF4J/Logback for a Standalone App

DZone's Guide to

Configuring SLF4J/Logback for a Standalone App

Configuring SLF4J/Logback for a Java SE app doesn't have to be a nightmare. In fact, there's already a library out there that does the initialization for you.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Most examples of SLF4J/Logback you can find on the Internet are about how web applications are configured. But if you have a command line or JavaFX application, you’ll require a different setup. Most probably, you want a local ‘logback.xml’ that is located in your application’s folder or in the home directory of the user. But how do you configure SLF4J/Logback for a Java SE application?

The good news is that there is already a tiny library that does the trick: ext4logback. It allows you simply to specify the location of your ‘logback.xml’ and does all the initialization for you. It also creates a default logback XML configuration if no file exists at startup.

Simply add the dependency to your Maven POM:


Then you can just initialize the log in your applications ‘main’ method:

 * Starts the application.
 * @param args
 *            Only optional argument is the 'logback.xml' file path and
 *            name. If no argument is provided it's assumed that the file name 
 *            is 'logback.xml' and it's in the current directory.
public static void main(final String[] args) {
    try {
        // Initializes Logback by reading the XML config file.
        // If the file does not exist, it will be created with some defaults.
        // This is a convenience method that directly uses the main method's arguments.
        new LogbackStandalone().init(args, new NewLogConfigFileParams("your.app.package", "myapp"));
        LOG.info("Application running...");
        // Your code goes here...
    } catch (RuntimeException ex) {

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

java ,logging ,jvm ,sl4j ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}