forked from baron/baron-sso
레포 업데이트
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -8,18 +9,28 @@ import (
|
||||
|
||||
// Config holds the logger configuration
|
||||
type Config struct {
|
||||
ServiceName string
|
||||
Environment string // "dev", "local", "production"
|
||||
ServiceName string
|
||||
Environment string // APP_ENV 기준
|
||||
LevelOverride string
|
||||
Output io.Writer
|
||||
}
|
||||
|
||||
func IsProductionLikeEnv(appEnv string) bool {
|
||||
env := strings.ToLower(strings.TrimSpace(appEnv))
|
||||
return env == "prod" || env == "production" || env == "stage" || env == "staging"
|
||||
}
|
||||
|
||||
// Init initializes the global logger with slog.
|
||||
// It detects the environment to switch between TextHandler (dev) and JSONHandler (prod).
|
||||
func Init(cfg Config) {
|
||||
var handler slog.Handler
|
||||
output := cfg.Output
|
||||
if output == nil {
|
||||
output = os.Stdout
|
||||
}
|
||||
|
||||
opts := &slog.HandlerOptions{
|
||||
// Default level
|
||||
Level: slog.LevelInfo,
|
||||
Level: ResolveBackendLogLevel(cfg.Environment, cfg.LevelOverride),
|
||||
// Customize attributes (Time format)
|
||||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
@@ -32,11 +43,10 @@ func Init(cfg Config) {
|
||||
// Adjust level and format based on environment
|
||||
env := strings.ToLower(cfg.Environment)
|
||||
if env == "dev" || env == "local" || env == "development" {
|
||||
opts.Level = slog.LevelDebug
|
||||
handler = slog.NewTextHandler(os.Stdout, opts)
|
||||
handler = slog.NewTextHandler(output, opts)
|
||||
} else {
|
||||
// Production defaults to JSON
|
||||
handler = slog.NewJSONHandler(os.Stdout, opts)
|
||||
handler = slog.NewJSONHandler(output, opts)
|
||||
}
|
||||
|
||||
// Create logger with common attributes
|
||||
@@ -47,3 +57,22 @@ func Init(cfg Config) {
|
||||
// Set as global default logger
|
||||
slog.SetDefault(logger)
|
||||
}
|
||||
|
||||
func ResolveBackendLogLevel(appEnv, override string) slog.Level {
|
||||
switch strings.ToLower(strings.TrimSpace(override)) {
|
||||
case "debug":
|
||||
return slog.LevelDebug
|
||||
case "info":
|
||||
return slog.LevelInfo
|
||||
case "warn", "warning":
|
||||
return slog.LevelWarn
|
||||
case "error":
|
||||
return slog.LevelError
|
||||
}
|
||||
|
||||
env := strings.ToLower(strings.TrimSpace(appEnv))
|
||||
if env == "dev" || env == "local" || env == "development" {
|
||||
return slog.LevelDebug
|
||||
}
|
||||
return slog.LevelInfo
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user