Log And Return Duplication (ARCH5166)
هذا المحتوى غير متوفر بلغتك بعد.
Key go.logging.log-and-return-error · Severity warning · Category logging
Logging an error and then returning it from the same handler produces duplicate log lines, since the error is logged again as it propagates up the stack. An error should either be logged or returned wrapped - not both.
Incorrect
Section titled “Incorrect”func fetch(log *slog.Logger, id string) (*Doc, error) { d, err := repo.Get(id) if err != nil { log.Error("fetch failed", "err", err) return nil, err } return d, nil}Correct
Section titled “Correct”func fetch(id string) (*Doc, error) { d, err := repo.Get(id) if err != nil { return nil, fmt.Errorf("fetch %s: %w", id, err) } return d, nil}Return the error wrapped and log it only once at the top-level boundary layer.