Part 10 / Topic 11 of 12
Basic logging with java.util.logging
System.out.println is fine for a Stage 1 program, but it cannot be turned off, has no severity, and cannot be redirected without changing the code. java.util.logging (nicknamed JUL) is the JDK's own logger — no external jar, available everywhere the JDK is.
import java.util.logging.Logger;
public class LoggingDemo {
private static final Logger LOGGER = Logger.getLogger(LoggingDemo.class.getName());
public static void main(String[] args) {
LOGGER.info("Opening account for Asha with balance 1000.0");
BankAccount account = new BankAccount("Asha", 1000.0, new ConsoleAlertService());
try {
account.withdraw(600.0);
LOGGER.warning("Balance after withdrawal is low: " + account.getBalance());
} catch (InsufficientFundsException e) {
LOGGER.severe("Withdrawal failed: " + e.getMessage());
}
LOGGER.fine("This FINE message will not print at the default level");
}
}
$ javac LoggingDemo.java
$ java LoggingDemo
Dec 01, 2026 1:58:46 PM LoggingDemo main
INFO: Opening account for Asha with balance 1000.0
ALERT: Asha's balance is low: Rs 400.00
Dec 01, 2026 1:58:46 PM LoggingDemo main
WARNING: Balance after withdrawal is low: 400.0
(The date/time stamp is (varies) — it is whenever you run it.) Levels, from least to most severe, with the JDK's default cutoff marked:
FINEST < FINER < FINE < CONFIG | INFO < WARNING < SEVERE
^ default: INFO and above print; FINE and below are silent
That is exactly what the transcript shows: LOGGER.fine(...) produced no output at all — not a bug, the default configuration simply does not print anything below INFO. Reach for WARNING/SEVERE for things worth a human's attention, INFO for normal milestones, and FINE/FINER/FINEST for detail you only want turned on while debugging a specific problem.
🔍 Going deeper: real projects almost always use SLF4J + Logback instead of
java.util.loggingdirectly — more configuration options, better performance at scale. This course stays with the JDK's own logger here because it needs no external jar, the same "stdlib first" choice Stage 1–2 have made throughout; SLF4J is worth knowing the name of, not worth an external dependency for a 5-hour lesson.
Full program: examples/bank-and-discount/LoggingDemo.java.
Naming, style, and the rest of "code quality"
This part's code has been following a few habits worth naming explicitly, because "code quality" otherwise stays vague:
- Names say what, not how:
withdraw,getBalance,wasAlertSentFor— a reader should rarely need to open a method to guess what it returns. - One
public classper file, file name matching the class name exactly — the same hard rule from Part 1, still true in every file in this lesson. - Constants are
UPPER_SNAKE_CASEand declared once, near the top:LOW_BALANCE_THRESHOLD,GST_RATE— a number used in two places by accident, with one of them forgotten during a change, is a bug that naming the constant once prevents entirely. - Validate at the boundary, fail loudly: every public method on
BankAccountandDiscountCalculatorthat takes a number checks it immediately and throws, rather than silently producing a wrong answer three steps later — this is also exactly what makes those methods easy to write exception tests for (Sections 5, 6). - Braces, indentation, one statement per line: every file in this course (including this one) uses the same brace-on-the-same-line style and 4-space indentation throughout — consistency inside one project matters far more than which specific style is picked.
Debugging in an IDE, briefly
System.out.println scattered through a method to see what is happening is a debugger with extra typing and extra cleanup. Both VS Code (with the Java extension) and IntelliJ IDEA do the same four things, from the same kind of click:
| Step | What it does |
|---|---|
Click in the gutter next to a line (e.g. the if (amount > balance) line in BankAccount.withdraw) |
Sets a breakpoint — the line gets a red dot; the program will pause right before running it |
Run the test (or main) in debug mode, not plain run |
The IDE's green "Run" button has a matching bug-shaped "Debug" button next to it |
| Program pauses at the breakpoint | The IDE shows every local variable's current value — amount, balance, owner — without a single println |
| Step over / step into / step out, then continue | Step over runs the current line and stops at the next one; step into follows a call inside another method (e.g. into alertService.sendLowBalanceAlert); step out finishes the current method and pauses back in its caller; continue runs to the next breakpoint or the end |
A breakpoint set on the line inside withdraw's if (balance < LOW_BALANCE_THRESHOLD) check, with a test like withdraw_takesBalanceBelowThreshold_sendsOneLowBalanceAlert run in debug mode, is the fastest way to actually watch balance become 400.0 and the condition evaluate true — far faster than adding a println, running, reading, then deleting the println.
Git and GitHub, just enough to save this
This course never runs these commands for you — they are something to type yourself, once, in the folder holding your own copy of this part's exercises.
| Command | What it does |
|---|---|
git init |
Turns the current folder into a Git repository (once, per project) |
git add BankAccountTest.java (or git add . for everything) |
Stages a file — marks it to be included in the next commit |
git commit -m "Add BankAccount tests" |
Saves a snapshot of every staged file, with a message describing why |
git status |
Shows what is staged, changed, or untracked right now |
git remote add origin <your-github-repo-url> |
Points your local repository at an empty repository you created on GitHub |
git push -u origin main |
Uploads your commits to GitHub, the first time with -u to remember the link for later plain git push |
A .gitignore file (this course's own root .gitignore is a real example) lists files Git should never track — *.class and *.jar are the two that matter most for this part, so compiled output and the downloaded JUnit jar never get committed by accident. Commit after each exercise rather than once at the very end — small, named commits ("Add DiscountCalculator validation", "Fix withdraw threshold test") are far easier to read back later than one giant "finished everything" commit.
Done here? The practice set for Part 10 — including the solutions — is on the part page.