跪拜 Guibai
← All articles
Backend · Architecture

A Logging Decorator for Go Notifiers: One Wrapper, Any Sender

By vlans ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— The decorator pattern adds behavior to an object by wrapping it in another object that implements the same interface.
— LoggingNotifier holds a `next Notifier` and writes to an `io.Writer`, printing start and end logs around the inner Send call.
— The same LoggingNotifier can wrap EmailNotifier, SMSNotifier, or any type that satisfies the Notifier interface.
— Testing uses bytes.Buffer to capture logs and verifies both the returned message and the exact log lines.
— Go's io.TeeReader uses the same wrapping idea: it reads from an io.Reader and writes to an extra writer.
— Decorators can be layered, and the order affects what is included in each decorator's measurements.
— The adapter pattern changes the public method (for example, Send to SendSMS), while the decorator preserves the same method signature.
Conclusions

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.

Concepts & terms
Decorator pattern
A structural design pattern that adds behavior to an object without changing its interface. In Go, it is implemented by wrapping a concrete type in another type that implements the same interface, delegating the core work to the wrapped object and adding extra logic before or after.
Adapter pattern
A structural pattern that lets two incompatible interfaces work together. It converts the interface of an object to one a client expects, often by changing method signatures. In contrast to a decorator, an adapter changes the public API rather than preserving it.
From the discussion
Featured comments
小范真是一把好手

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.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗