feat(cmd/joj3): add runID to log
All checks were successful
submodules sync / sync (push) Successful in 41s
build / build (push) Successful in 1m22s
build / trigger-build-image (push) Successful in 7s

This commit is contained in:
张泊明518370910136 2024-10-29 05:56:13 -04:00
parent 028fd11ab5
commit d56e3e5ebc
GPG Key ID: D47306D7062CDA9D

View File

@ -2,8 +2,10 @@ package main
import (
"context"
"fmt"
"log/slog"
"os"
"time"
)
var runningTest bool
@ -12,6 +14,12 @@ type multiHandler struct {
handlers []slog.Handler
}
var runID string
func init() {
runID = fmt.Sprintf("%d-%d", time.Now().UnixNano(), os.Getpid())
}
func (h *multiHandler) Enabled(ctx context.Context, level slog.Level) bool {
for _, handler := range h.handlers {
if handler.Enabled(ctx, level) {
@ -69,7 +77,7 @@ func setupSlog(logPath string) error {
// Stderr handler for info logs and above
stderrHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: stderrLogLevel,
})
}).WithAttrs([]slog.Attr{slog.String("runID", runID)})
handlers = append(handlers, stderrHandler)
// Create a multi-handler
multiHandler := &multiHandler{handlers: handlers}