A Logging Decorator for Go Notifiers: One Wrapper, Any Sender
One logging wrapper lets notification services add observability to any sender without touching existing code or duplicating logs. The pattern also makes it easy to turn logging on or off per sender at assembly time.
Email sending already works in a Go service, but each send needs to be followed by start and end logs. SMS and push are next, and they need the same two log lines. Handcoding those logs into every sender means maintaining three copies when the format changes.
The LoggingNotifier solves that by implementing the same Notifier interface as the real senders. It holds the underlying sender in a next field, prints start, delegates to that sender, prints end, and returns the result. Callers keep using Send, and the same wrapper can be applied to any type that satisfies the interface.
The tutorial includes a full main.go and a test that captures logs in a bytes.Buffer to verify both the message and the exact log lines. It then contrasts the decorator with the adapter pattern: an adapter changes the method signature, a decorator preserves it. Stacking decorators is covered too, and the order changes what gets measured.
The distinction between decorator and adapter is visible in the method body: a decorator calls the same method name on its wrapped object, while an adapter calls a different one. That simple check resolves what field inspection cannot.
Because the decorator depends on the Notifier interface, future senders can be added and wrapped without modifying the wrapper or the senders, so logging is a pure composition concern.
The test substitutes bytes.Buffer for the terminal, proving that a decorator's side effects can be verified without hitting real I/O, which keeps behavior tests fast and deterministic.
Did you type this code out yourself in Cursor? Did you jump to see how each standard library function is implemented? While writing code, did you wonder why the receiver is a value instead of a pointer? In various programming languages, there are interface definitions - what problem does that solve? Why does Go only have structs but no classes, while other languages have classes? You should look into these with questions in mind. Although AI understands these things better than you, it is necessary for beginners to have some understanding of the above.