Java Logging (Log4j/SLF4J)
Why Logging?
Logging is the practice of recording program events as they happen during runtime. It's a far more robust alternative to scattering System.out.println() calls throughout your code, because it lets you categorize messages by severity, route them to different destinations, and turn categories on or off without editing source code.
Example: Why Logging?
import java.util.logging.Logger;
public class Main {
static final Logger logger = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
logger.info("Categorized, routable log message"); // more robust than System.out.println
}
}
Login to try C/C++/Java/PHP code in the editor
Java Native Logger
The built-in java.util.logging package (often called JUL) provides logging features out of the box with zero external dependencies, which makes it a reasonable starting point for small applications, even though larger projects often prefer a more feature-rich third-party framework.
Example: Java Native Logger
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Logger logger = Logger.getLogger("com.example.app"); // java.util.logging, zero external dependencies
logger.info("Application started");
}
}
Login to try C/C++/Java/PHP code in the editor
Log Levels
Log levels categorize messages by how important or severe they are -- INFO for routine informational events, WARNING for potential issues that aren't yet failures, and SEVERE for critical errors -- letting you filter output to just the severity level you actually care about at a given time.
Example: Log Levels
import java.util.logging.*;
public class Main {
static final Logger logger = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
logger.info("Routine event");
logger.warning("Potential issue");
logger.severe("Critical error");
}
}
Login to try C/C++/Java/PHP code in the editor
File Handlers
FileHandlers redirect log output to physical files on the server's disk instead of just the console, ensuring you have a persistent, searchable history of what happened even after a console session or terminal has closed.
Example: File Handlers
import java.util.logging.*;
public class Main {
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger(Main.class.getName());
FileHandler fileHandler = new FileHandler("app.log"); // persists to disk, not just console
logger.addHandler(fileHandler);
logger.info("Written to app.log");
fileHandler.close();
new java.io.File("app.log").delete();
}
}
Login to try C/C++/Java/PHP code in the editor
SLF4J and Log4j Wrappers
Enterprise applications commonly use SLF4J as a logging facade -- a standardized API that decouples your code from any specific logging implementation, letting you plug in Log4j, Logback, or another backend framework later without rewriting every logging call in your codebase.
Example: SLF4J and Log4j Wrappers
public class Main {
interface Logger { void info(String msg); } // a facade like SLF4J -- decoupled from any specific backend
static class ConsoleLogger implements Logger {
public void info(String msg) { System.out.println("[INFO] " + msg); }
}
public static void main(String[] args) {
Logger logger = new ConsoleLogger(); // could be swapped for Log4j/Logback later
logger.info("Facade decouples code from the logging backend");
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing