Join our Newsletter — 33% off our NHI Course
Home› FAQ› Foundations & NHI Taxonomy› What is the difference between logging an error…
Foundations & NHI Taxonomy

What is the difference between logging an error and wrapping it in Go?

← Back to all FAQ
By NHI Mgmt Group Editorial Team Updated September 27, 2026 Domain: Foundations & NHI Taxonomy

Logging records the error for humans, while wrapping preserves the error for code and callers to inspect later. Good logging can explain what happened at the time of failure, but wrapping keeps the original error type and stack context available for later decisions. Mature Go code often uses both, but for different purposes.

Logging and wrapping solve different problems

Logging is for observability: it creates a human-readable record that something failed, often with enough surrounding detail to reconstruct the event. Wrapping is for program flow: it preserves the underlying error so later code can inspect, compare, unwrap, or return it again without losing the original cause.

That distinction matters in Go because an error can be both a diagnostic signal and a control-flow value. If you only log it, you may help operators but starve callers of structure. If you only wrap it, you preserve semantics but may leave operations blind unless something upstream logs the failure at the right boundary.

Why wrapping keeps more technical meaning than logging

When you wrap an error, you are extending it rather than replacing it. The original error value still exists inside the chain, so code can use errors.Is and errors.As to detect sentinel errors or extract concrete types later. That is what makes wrapping useful for retries, fallback logic, HTTP status mapping, and domain-specific handling.

Logging, by contrast, turns the failure into text. Once you print a message, the structured error value is no longer available to the caller unless you also return it. That is why logging alone is usually the wrong boundary decision inside reusable functions: it can duplicate noise, hide causality, and make upstream handling harder.

In practice, wrapping also preserves context in a way that scales better than ad hoc log messages. A well-wrapped chain can tell you which operation failed, while still keeping the underlying cause intact for later inspection by tests, middleware, or top-level request handlers.

How mature Go code uses both without mixing responsibilities

The clean pattern is to wrap when you are propagating the error up the stack, and log when you are reaching a boundary where the error should be observed or acted on. Library code usually wraps and returns. Application entry points, request handlers, job runners, and worker loops usually log after they have enough context to make the message useful.

A useful rule is: if the next caller may need to branch on the error, wrap it; if the error has reached the place where it will be handled operationally, log it there. That keeps the error chain intact for code while avoiding repeated logging at every layer.

Go’s standard error-wrapping behavior is now part of normal practice, and the ecosystem around it assumes that errors may be unwrapped rather than merely printed. For that reason, choose wording and wrapping points that preserve the original cause while adding only the context the caller truly needs.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

NIST SP 800-53 Rev 5, OWASP ASVS and CIS Controls v8 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
NIST SP 800-53 Rev 5AU-2 — Event LoggingLogging and error handling both depend on recording useful operational evidence.
AU-12 — Audit Record GenerationThe question contrasts human-visible logging with preserved error context.
Recommendation — Define when errors must be logged and ensure the log captures enough context for later review. Generate audit records at the handling boundary instead of logging repeatedly at every layer.
OWASP ASVSV16 — Security Logging and Error HandlingThe distinction between logging and preserving errors is an error-handling concern.
Recommendation — Separate diagnostic logging from returned error values so callers can still inspect root causes.
CIS Controls v8CIS-8 — Audit Log ManagementLogging is the human-facing side of the error-handling decision.
Recommendation — Centralize logging so failures are recorded once with the right operational context.

Practitioner Guidance

What to verify: Check whether the function is a reusable boundary or an operational boundary. Reusable code should normally return wrapped errors; only the outermost handler should decide whether the failure is worth logging.

Common mistake: Avoid logging and returning the same error at every layer. That produces duplicate noise, obscures the root cause, and makes it harder to tell which component actually owns the failure.

Decision rule: If downstream code may need to test the error type or cause, wrap it. If no later decision depends on the error value, and you are at the point of user-visible handling, log it with enough context to be actionable.

Practitioner takeaway: In Go, wrapping preserves meaning for code, logging preserves visibility for humans, and good design keeps those responsibilities separated until the final handling boundary.

Deepen Your Knowledge

Sign up to our weekly newsletter — get 33% off our NHI Foundation Level Course

    NHIMG Editorial Note
    Reviewed and updated by the NHIMG editorial team on September 27, 2026.
    NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org