MODULE 1 · TOPIC 15 · VISUAL PROGRAMMING LAB

Logging in Java

Understand logger levels, filtering, message formatting, handlers and useful diagnostics.

Browser-based simulations: change the inputs, trace the Java statements and inspect results. No arbitrary Java execution, real database, threads or file changes occur here.

1. Log levels — filter event messages

Change the minimum accepted level and see which events reach the console.

Java example
Program / simulation outputConsole
Data operationsCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

java.util.logging levels run from detailed FINE to INFO, WARNING and SEVERE. Both logger and handler filters matter. Output here omits timestamp and formatter details.

Try and observe

Select WARNING and see INFO and FINE disappear.

2. Logger, handler and formatter

Observe the same event delivered to different destinations.

Java example
Program / simulation outputConsole
Live state / data blocksCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

A logger creates records, a handler publishes them, a formatter controls presentation. FileHandler can rotate files; structured output requires an appropriate formatter.

Try and observe

Choose both then structured. No real file is written.

3. Debugging an operation using contextual logs

Trace validation, success and failure without exposing sensitive information.

Java example
Program / simulation outputConsole
Live execution stateCompact blocks
CurrentResultError / rejected

Inputs above update the example. Highlighted line indicates the current operation. Fragments marked “inside main” or requiring setup belong in the indicated Java project.

What this animation explains

Log context helps reconstruct an operation. Use appropriate levels and avoid secrets, tokens and personal data. Use an exception overload when a safe stack trace is useful.

Try and observe

Compare invalid input with database failure; different severity communicates different issues.

4. Hands-on practice and assessment

Programming challenge

Configure a Logger with ConsoleHandler and FileHandler in a local app. Log start, success, invalid input and caught exceptions. Test multiple thresholds.

Edit the scratchpad below. Copy your code into an actual Java project or IDE to compile and test it.

Quick assessment

Key points and real Java notes

Prefer parameterized logging in larger projects (e.g. SLF4J with a configured provider). Avoid duplicate logs from parent handlers and do not use logging as a replacement for proper exception handling.