Sync and async sinks, redaction, and a queryable HTTP service keep concurrent logs tied to a trace id. Co-authored-by: Cursor <cursoragent@cursor.com>
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
// Package logsys 是进程内结构化日志核心。
|
|
//
|
|
// 它不绑定具体出口:实现 Sink 即可接到控制台、文件或远端采集器。
|
|
// 错误码通过鸭子接口识别(ErrorCode / ErrorFields),本包不导入错误码模块。
|
|
package logsys
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// Level 是日志级别。数值越大越严重。
|
|
type Level int
|
|
|
|
const (
|
|
LevelTrace Level = iota
|
|
LevelDebug
|
|
LevelInfo
|
|
LevelWarn
|
|
LevelError
|
|
LevelFatal
|
|
)
|
|
|
|
// ParseLevel 解析级别文本。
|
|
func ParseLevel(s string) (Level, error) {
|
|
switch strings.ToLower(strings.TrimSpace(s)) {
|
|
case "trace":
|
|
return LevelTrace, nil
|
|
case "debug":
|
|
return LevelDebug, nil
|
|
case "info", "":
|
|
if strings.TrimSpace(s) == "" {
|
|
return 0, fmt.Errorf("logsys: empty level")
|
|
}
|
|
return LevelInfo, nil
|
|
case "warn", "warning":
|
|
return LevelWarn, nil
|
|
case "error":
|
|
return LevelError, nil
|
|
case "fatal":
|
|
return LevelFatal, nil
|
|
default:
|
|
return 0, fmt.Errorf("logsys: unknown level %q", s)
|
|
}
|
|
}
|
|
|
|
// String 返回级别名。
|
|
func (l Level) String() string {
|
|
switch l {
|
|
case LevelTrace:
|
|
return "trace"
|
|
case LevelDebug:
|
|
return "debug"
|
|
case LevelInfo:
|
|
return "info"
|
|
case LevelWarn:
|
|
return "warn"
|
|
case LevelError:
|
|
return "error"
|
|
case LevelFatal:
|
|
return "fatal"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|