Learn Java
import com.learnjava.exceptionhandling.Logging;

Logging in Java

@Concept

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.

@HowItWorks
1

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.

2

Severity levels

Like SEVERE, WARNING, and INFO, communicate how serious a given log message is, not just what it says.

3

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.

4

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.

5

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.

@Example
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);
}
}
}
Output
INFO: Application starting
SEVERE: Calculation failed
1logger.info(...) records a routine, informational message about the program's normal progress.
2The division by zero throws an ArithmeticException, caught by the surrounding catch block.
3logger.log(Level.SEVERE, "Calculation failed", e) records both a descriptive message and the caught exception itself, preserving its full stack trace for later diagnosis.
4The real default console format also prints a timestamp and the calling class and method above each message; the output above simplifies that away to focus on the level and message text.
@UseCases
Recording what an application is doing over time in a searchable, structured way, instead of scattered println calls
Filtering out low-priority log messages in production without touching any individual logging call in the code
Preserving a caught exception's full stack trace by logging the exception object itself, not just its message
Routing the same log statements to a console during development and a file or remote service in production

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.