opentdf / platform

OpenTDF Platform monorepo enabling the development and integration of _forever control_ of data into new and existing applications. The concept of forever control stems from an increasingly common concept known as zero trust.
BSD 3-Clause Clear License
15 stars 4 forks source link

go logging: Have some standards for logging #804

Open dmihalcik-virtru opened 1 month ago

dmihalcik-virtru commented 1 month ago

Alternatives:

  1. prefer LogAttrs for efficiency
  2. Use custom logger type (platform.logger) to enable ease of Audit events. Prefer using Context and Attr structs.
  3. Use default logger with custom Level and no requirements.

LogAttrs

How it looks:

import (
    "log/slog"
    "time"
    "github.com/platform/service/audit"
)

...

func x() {
    slog.LogAttrs(
        ctx,
        audit.Level,
        "x function started",
        slog.Time("now", time.Now()),
    )
}

Custom Logger

Move internal/logger/logger.go to service/logger/logger.go Update linter to look for use of default logger or other brand of logger and block them.

No Rules

Generate a custom handler that logs certain levels as audit or when various attr/context matchers are hit. Register custom handler at service start

jrschumacher commented 1 month ago

Are you suggesting we use this syntax logger.LogAttrs(ctx, slog.LevelInfo, "hello", slog.Int("count", 3)) rather than slog.InfoContext(ctx, "hello", slog.Int("count", 3))?

What kind of efficiency gains do we get? Seems that this could prove challenging to enforce.

dmihalcik-virtru commented 1 month ago

It looks like the efficiency gains are minimal. zap maintains a performance comparison and it looks like we only save 2 allocs (out of about 4x the number of attributes we allocate in a line).