In this tutorial, we’ll explain what the SLF4J logging library is, how to configure it in a Gradle Java project, and why it’s useful in real-world applications.
Logging is the process of recording events, messages or data generated by an application or system during execution to provide information about what the system is doing, any errors or warnings, and how different components interact.
These logs are a critical part of software development and maintenance as they help developers and system administrators monitor, debug and optimize applications.
SLF4J, which stands for Simple Logging Facade for Java is a library that provides a simple and flexible abstraction for logging in applications. Instead of being a logging framework itself, SLF4J is a facade or a bridge that allows you to plug in the logging framework of your choice (e.g. Log4j) at deployment time without changing the application code.
In this tutorial we will implement a simple project to demonstrate how to configure SLF4J using the Log4J framework.
We can start by creating a new Gradle Project. In the build.gradle file, we need to add the following dependencies: SLF4J, Log4j and the bind between SLF4J and Log4J. The ‘application’ plugin is used to simplify the execution of our application. The project will run with a “gradle run” command typed in the command line or through the user interface of your own IDE.
plugins {
id 'java'
id 'application'
}
group 'org.explainjava'
version '1.0'
repositories {
mavenCentral()
}
application {
mainClassName = "org.explainjava.Main"
}
dependencies {
// SLF4J API
implementation 'org.slf4j:slf4j-api:2.0.9'
// Log4j binding for SLF4J
runtimeOnly 'org.slf4j:slf4j-log4j12:2.0.9'
// Log4j core library
runtimeOnly 'log4j:log4j:1.2.17'
}
The next step is to create a log4j.properties configuration file in our project’s src/main/resources directory.
# Root logger configuration
log4j.rootLogger=INFO, console, file
# Console appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# File appender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.file=application.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Now, we can test the log in the Main class of our application:
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
logger.info("Application is starting...");
try {
logger.debug("Attempting risky operation...");
int result = 10 / 0; // This will throw an exception
} catch (Exception e) {
logger.error("An exception occurred: {}", e.getMessage(), e);
}
logger.info("Application is shutting down.");
}
}
If we will run the application using “gradle run” command, the output will be:
2024-11-29 16:35:20 INFO Main:11 - Application is starting...
2024-11-29 16:35:20 ERROR Main:17 - An exception occurred: / by zero
java.lang.ArithmeticException: / by zero
at org.explainjava.Main.main(Main.java:15)
2024-11-29 16:35:20 INFO Main:20 - Application is shutting down.
In this tutorial we had a short introduction in logging and we had a simple example of configuring SLF4J in a Gradle Java application.
Learn advanced concepts, work on real-world projects, and fast-track your journey to becoming a proficient Java developer. Start now and unlock your full potential in the world of Java programming!
Start now and unlock your full potential in the world of Java programming!
The place where you can start your Java journey.
© All Rights Reserved.