DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Configuring SLF4J/Logback for a Standalone App

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.

Michael Schnell user avatar by
Michael Schnell
·
Dec. 29, 16 · Java Zone · Tutorial
Like (4)
Save
Tweet
7.99K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<dependency>
    <groupId>org.fuin</groupId>
    <artifactId>ext4logback</artifactId>
    <version>0.2.0</version>
</dependency>


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...
        System.exit(0);
    } catch (RuntimeException ex) {
        ex.printStackTrace(System.err);
        System.exit(1);
    }
}


app application Home directory JavaFX Dependency Directory Command (computing) Apache Maven Java (programming language)

Published at DZone with permission of Michael Schnell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java’s Encapsulation - When the Getter and Setter Became Your Enemy
  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole
  • Making Your Own Express Middleware
  • Reactive Kafka With Streaming in Spring Boot

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo