Logging in Java
Logging records what a program is doing, and what goes wrong, to somewhere more durable and structured than a scattered pile of System.out.println calls — a proper logging framework like java.util.logging, built into the JDK, or the more widely-used third-party SLF4J and Logback, timestamps every message automatically, tags it with a severity level, and can route different levels to different destinations, like a console for development and a file for production. Severity levels, typically SEVERE, WARNING, and INFO, or similarly-named equivalents in other frameworks, let a log statement communicate not just what happened but how serious it was, and let the overall logging configuration filter out lower-priority noise in production without touching any of the actual logging calls in the code.
Logging exceptions specifically means capturing the full stack trace, not just an exception's short message, since the stack trace is what actually shows where in the code a problem originated. A well-configured logging call passed the caught exception object directly preserves that entire trace for later diagnosis, exactly the same detail a crashed, uncaught exception would have printed on its own.
Why logging beats scattered println calls
A proper log records a timestamp, a severity level, and a message together, in one consistent, structured, searchable format.
Severity levels
Like SEVERE, WARNING, and INFO, communicate how serious a given log message is, not just what it says.
Filtering by level
An entire application's logging can be tuned to show only warnings and above in production, without touching any individual logging call in the code.
Routing to different destinations
The same log statement can be configured to write to a console during development and a file, or a remote logging service, in production.
Logging an exception's full stack trace
Passing the caught exception object itself to the logging call, not just its message, preserves the complete trace for diagnosis.
import java.util.logging.Logger;import java.util.logging.Level; public class LoggingDemo { static final Logger logger = Logger.getLogger(LoggingDemo.class.getName()); public static void main(String[] args) { logger.info("Application starting"); try { int result = 10 / 0; } catch (ArithmeticException e) { logger.log(Level.SEVERE, "Calculation failed", e); } }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.