Advanced 25 min read

Monitor and Log

By the end of this lesson, you'll be able to:

  • Add structured logging for rejected transactions and ledger-callout failures
  • Apply Module 33's debugging discipline to ongoing production monitoring
  • Distinguish between what should be logged versus what should raise an alert

Prerequisites: "Write a Full Test Suite"

Logging without disrupting the transaction

public class TransactionLogger {
    public static void logRejection(Id accountId, String reason) {
        System.debug(LoggingLevel.WARN, 'Transaction rejected for account ' + accountId + ': ' + reason);
        // in a real system: insert a Rejected_Transaction_Log__c record here
    }
}

Calling this from TransactionValidation's addError() branch (Lesson 6) doesn't change the rejection's behavior at all — it simply records why rejections are happening over time, so a spike in overdraft attempts, say, would be visible in aggregate rather than only ever seen one addError() message at a time.

Logging a ledger-callout failure without losing data

public void execute(QueueableContext qc) {
    for (Transaction__c t : [SELECT Id, Amount__c, Type__c FROM Transaction__c WHERE Id IN :transactionIds]) {
        try {
            // ... build and send the HttpRequest from Lesson 4
        } catch (CalloutException e) {
            System.debug(LoggingLevel.ERROR, 'Ledger callout failed for transaction ' + t.Id + ': ' + e.getMessage());
            // in a real system: enqueue a retry, following Module 39's exact retry pattern
        }
    }
}

This directly reuses Module 39's payment-gateway error-handling shape — a caught, logged failure that doesn't lose the underlying record, with a retry strategy as the natural next step rather than a silent failure.

Log versus alert: not everything needs a human

A single overdraft rejection is normal, expected system behavior — worth a debug log entry, nothing more. A sustained spike in rejections, or a ledger-callout failure rate crossing some threshold, is what should actually reach a human via Module 41's incident-response process. Distinguishing routine logging from genuine alerting keeps the signal-to-noise ratio usable — logging everything as if it's urgent trains people to ignore the alerts entirely.

Exercise

As a comment, explain why a single overdraft rejection should just be logged, while a sustained spike in overdraft rejections should trigger an actual alert to a human.

Show hint

Think about what happens if every routine event pages someone.

APEX

Monitor and Log Quiz

1. What is the risk of logging every routine, expected event (like a single overdraft rejection) as an urgent alert?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

This lesson adds structured logging around the banking system's failure points — rejected transactions and ledger-callout failures — applying Module 33's debugging mindset proactively, before an issue occurs, rather than reactively after one is reported.