mirror of
https://github.com/syntrex-lab/gomcp.git
synced 2026-04-29 22:36:21 +02:00
Release prep: 54 engines, self-hosted signatures, i18n, dashboard updates
This commit is contained in:
parent
694e32be26
commit
41cbfd6e0a
178 changed files with 36008 additions and 399 deletions
57
internal/infrastructure/logging/logger.go
Normal file
57
internal/infrastructure/logging/logger.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// Package logging provides structured logging via Go's log/slog.
|
||||
// Production: JSON output. Development: text output with colors.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// logger := logging.New("json", "info") // production
|
||||
// logger := logging.New("text", "debug") // development
|
||||
// logger.Info("event ingested", "event_id", id, "source", src)
|
||||
package logging
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// New creates a structured logger.
|
||||
// format: "json" (production) or "text" (development).
|
||||
// level: "debug", "info", "warn", "error".
|
||||
func New(format, level string) *slog.Logger {
|
||||
return NewWithOutput(format, level, os.Stdout)
|
||||
}
|
||||
|
||||
// NewWithOutput creates a logger writing to the given writer.
|
||||
func NewWithOutput(format, level string, w io.Writer) *slog.Logger {
|
||||
lvl := parseLevel(level)
|
||||
opts := &slog.HandlerOptions{Level: lvl}
|
||||
|
||||
var handler slog.Handler
|
||||
switch strings.ToLower(format) {
|
||||
case "json":
|
||||
handler = slog.NewJSONHandler(w, opts)
|
||||
default:
|
||||
handler = slog.NewTextHandler(w, opts)
|
||||
}
|
||||
|
||||
return slog.New(handler)
|
||||
}
|
||||
|
||||
// WithComponent returns a logger with a "component" attribute.
|
||||
func WithComponent(logger *slog.Logger, component string) *slog.Logger {
|
||||
return logger.With("component", component)
|
||||
}
|
||||
|
||||
func parseLevel(s string) slog.Level {
|
||||
switch strings.ToLower(s) {
|
||||
case "debug":
|
||||
return slog.LevelDebug
|
||||
case "warn", "warning":
|
||||
return slog.LevelWarn
|
||||
case "error":
|
||||
return slog.LevelError
|
||||
default:
|
||||
return slog.LevelInfo
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue