Skip to content
Back to skills

Golang Logging

ASecurity

Pass it explicitly, not as a global. The usual Go pattern is: 1. Create the base logger in `main()` 2. Inject it into top-level dependencies via constructors 3. Derive child loggers per layer with `With(...)` 4. Add request-scoped values like request ID / trace ID at the edge Example with `log/slog`: ```go package main import ( "context" "log/slog" "os" ) type UserRepo struct { logger *slog.Logger } func NewUserRepo(logger *slog.Logger) *UserRepo { return &UserRepo{logger: logger} } func (r *...

  • 584 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 5, 2026
developmentgo

Works with

  • cli

Security analysis

A100/100

Pro scans all 11 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill golang-logging --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Golang Logging?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Golang Logging
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-golang-logging-16193e06/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-golang-logging-16193e06)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
Pass it explicitly, not as a global.

The usual Go pattern is:

1. Create the base logger in `main()`
2. Inject it into top-level dependencies via constructors
3. Derive child loggers per layer with `With(...)`
4. Add request-scoped values like request ID / trace ID at the edge

Example with `log/slog`:

```go
package main

import (
	"context"
	"log/slog"
	"os"
)

type UserRepo struct {
	logger *slog.Logger
}

func NewUserRepo(logger *slog.Logger) *UserRepo {
	return &UserRepo{logger: logger}
}

func (r *UserRepo) Save(ctx context.Context, userID string) error {
	r.logger.InfoContext(ctx, "saving user", "user_id", userID)
	return nil
}

type UserService struct {
	logger *slog.Logger
	repo   *UserRepo
}

func NewUserService(logger *slog.Logger, repo *UserRepo) *UserService {
	return &UserService{
		logger: logger,
		repo:   repo,
	}
}

func (s *UserService) CreateUser(ctx context.Context, userID string) error {
	s.logger.InfoContext(ctx, "creating user", "user_id", userID)
	return s.repo.Save(ctx, userID)
}

func main() {
	logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

	repoLogger := logger.With("layer", "repo", "component", "user_repo")
	svcLogger := logger.With("layer", "service", "component", "user_service")

	repo := NewUserRepo(repoLogger)
	svc := NewUserService(svcLogger, repo)

	ctx := context.Background()
	_ = svc.CreateUser(ctx, "u_123")
}
```

For request-scoped fields, attach them once near the HTTP/gRPC boundary:

```go
func handler(baseLogger *slog.Logger, svc *UserService) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		reqLogger := baseLogger.With(
			"request_id", r.Header.Get("X-Request-ID"),
			"trace_id", r.Header.Get("X-Trace-ID"),
		)

		ctx := context.WithValue(r.Context(), loggerKey{}, reqLogger)
		_ = svc.CreateUser(ctx, "u_123")
	}
}
```

If many deep functions need the logger, two clean options are:

- dependency injection for long-lived components (`service`, `repo`, `client`)
- context for request-scoped enrichment

Good rule of thumb:

- Put the logger on structs that own behavior
- Use `ctx` for per-request metadata
- Derive child loggers with `With(...)`
- Avoid `log.Fatal` in libraries/services; return errors instead

So the short answer is: create once in `main`, inject through constructors, and enrich per layer/request rather than reaching for a global logger.

Files in this skill

  • eval-1.baseline.md1.4 KB
  • eval-1.with-skill.md1.4 KB
  • eval-2.baseline.md1.1 KB
  • eval-2.with-skill.md2.3 KB
  • eval-3.baseline.md744 B
  • eval-3.with-skill.md643 B
  • trigger-1.md152 B
  • trigger-2.md165 B
  • trigger-3.md133 B
  • trigger-4.md139 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.