SLF4J Logback Console & File

Prerequisites

Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

Maven dependencies

In order to run the example; simply follow steps below:

Step 1 Create Maven Project

  • Launch Eclipse IDE; Create new Java Project by going to File -> New -> Others… Select Maven Project under Maven category; then click Next.
  • Click Next.
  • Click Next.
  • Choose any Group Id & Artifact Id then click Finish.

Step 2 Create Java Class

Copy the code below to clipboard; Select the project or src/main/java folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 3 Logback Configuration file

Under src\main create folder resources, create logback.xml file under the resources folder with following content.

Step 3 Clean & Build

Go to Project -> Clean to clean & build the project; maven dependencies will be downloaded & added to the project build path.

Step 4 Run the application

Right click to the class; select Run As -> Java Application.

Output

The below information will be printed in both Console & log file. In this example, you can locate the log file at c:\log as configured in logback.xml.