Logging with pear/Log and severity #1961
-
|
We're using frankenPHP as an embedded Go module for our Go/PHP mixed app. As of now, we have to parse the logs output by Something along these lines: import (
"context"
"log/slog"
"strings"
)
type phpLogHandler struct{ slog.Handler }
var levelMap = map[string]slog.Level{
"[emergency]": slog.LevelError,
"[alert]": slog.LevelError,
"[critical]": slog.LevelError,
"[error]": slog.LevelError,
"[warn]": slog.LevelWarn,
"[notice]": slog.LevelInfo,
"[info]": slog.LevelInfo,
"[debug]": slog.LevelDebug,
}
const (
logPrefix string = "[PHP] "
maxLevelLen int = len("[emergency] ")
)
func (h *phpLogHandler) Handle(ctx context.Context, r slog.Record) error {
level := r.Level
msg := r.Message
maxLen := len(msg)
if maxLen > maxLevelLen {
maxLen = maxLevelLen
}
if idx := strings.IndexByte(msg[:maxLen], ' '); idx != -1 {
if lvl, ok := levelMap[msg[:idx]]; ok {
level = lvl
msg = msg[idx+1:] // Skip the space
}
}
return h.Handler.Handle(ctx, slog.NewRecord(r.Time, level, logPrefix+msg, 0))
}
func NewPhpLogger(handler slog.Handler) *slog.Logger {
return slog.New(&phpLogHandler{handler})
}When digging into this, I could not find a way to just relay the severity given to Before I start any work on that, I'd like to have some feedback on that from the community in order to know if it's really just a niche use-case, or if there is broader interest in something like this. In case there is, I'd start drafting a PR to implement this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
After looking a bit into it, there indeed doesn't seem to be a way to pass on log severity to go right now. Probably makes sense to add a function and reuse the existing log constant severities 👍 |
Beta Was this translation helpful? Give feedback.
After looking a bit into it, there indeed doesn't seem to be a way to pass on log severity to go right now. Probably makes sense to add a function and reuse the existing log constant severities 👍