优化结构
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type contextFieldsKey struct{}
|
||||
|
||||
// ContextFields contains request metadata attached to logs emitted with a
|
||||
// request context.
|
||||
type ContextFields struct {
|
||||
RequestID string
|
||||
TraceID string
|
||||
SpanID string
|
||||
ParentSpanID string
|
||||
DeviceID string
|
||||
ClientIP string
|
||||
HTTPMethod string
|
||||
HTTPPath string
|
||||
}
|
||||
|
||||
func WithContextFields(ctx context.Context, fields *ContextFields) context.Context {
|
||||
return context.WithValue(ctx, contextFieldsKey{}, fields)
|
||||
}
|
||||
|
||||
func ContextFieldsFrom(ctx context.Context) *ContextFields {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
fields, _ := ctx.Value(contextFieldsKey{}).(*ContextFields)
|
||||
return fields
|
||||
}
|
||||
|
||||
type contextHandler struct{ handler slog.Handler }
|
||||
|
||||
func (h *contextHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
return h.handler.Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h *contextHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||
if fields := ContextFieldsFrom(ctx); fields != nil {
|
||||
record.AddAttrs(
|
||||
slog.String("request_id", fields.RequestID),
|
||||
slog.String("trace_id", fields.TraceID),
|
||||
slog.String("device_id", fields.DeviceID),
|
||||
slog.String("client_ip", fields.ClientIP),
|
||||
slog.String("http_method", fields.HTTPMethod),
|
||||
slog.String("http_path", fields.HTTPPath),
|
||||
)
|
||||
if fields.SpanID != "" {
|
||||
record.AddAttrs(slog.String("span_id", fields.SpanID))
|
||||
}
|
||||
if fields.ParentSpanID != "" {
|
||||
record.AddAttrs(slog.String("parent_span_id", fields.ParentSpanID))
|
||||
}
|
||||
}
|
||||
return h.handler.Handle(ctx, record)
|
||||
}
|
||||
|
||||
func (h *contextHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return &contextHandler{handler: h.handler.WithAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h *contextHandler) WithGroup(name string) slog.Handler {
|
||||
return &contextHandler{handler: h.handler.WithGroup(name)}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DailyWriter writes log entries to <root>/<yyyy-mm-dd>/<name>. It keeps the
|
||||
// file handle open for the active day and rotates on the first write after
|
||||
// midnight, matching the directory layout consumed by the log viewer.
|
||||
type DailyWriter struct {
|
||||
mu sync.Mutex
|
||||
root string
|
||||
name string
|
||||
date string
|
||||
file *os.File
|
||||
retentionDay int
|
||||
}
|
||||
|
||||
func NewDailyWriter(root, name string, retentionDay int) *DailyWriter {
|
||||
w := &DailyWriter{root: root, name: name, retentionDay: retentionDay}
|
||||
w.removeExpired()
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *DailyWriter) removeExpired() {
|
||||
if w.retentionDay <= 0 {
|
||||
return
|
||||
}
|
||||
entries, err := os.ReadDir(w.root)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cutoff := time.Now().AddDate(0, 0, -w.retentionDay)
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
date, err := time.Parse("2006-01-02", entry.Name())
|
||||
if err == nil && date.Before(cutoff) {
|
||||
_ = os.RemoveAll(filepath.Join(w.root, entry.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *DailyWriter) Write(value []byte) (int, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
date := time.Now().Format("2006-01-02")
|
||||
if w.file == nil || w.date != date {
|
||||
if w.file != nil {
|
||||
_ = w.file.Close()
|
||||
}
|
||||
filePath := filepath.Join(w.root, date, filepath.Clean(w.name))
|
||||
directory := filepath.Dir(filePath)
|
||||
if err := os.MkdirAll(directory, 0o755); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
w.file, w.date = file, date
|
||||
}
|
||||
return w.file.Write(value)
|
||||
}
|
||||
|
||||
func (w *DailyWriter) Sync() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.file == nil {
|
||||
return nil
|
||||
}
|
||||
return w.file.Sync()
|
||||
}
|
||||
|
||||
func (w *DailyWriter) Close() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.file == nil {
|
||||
return nil
|
||||
}
|
||||
err := w.file.Close()
|
||||
w.file = nil
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type stackFrame struct {
|
||||
File string
|
||||
Line int
|
||||
Func string
|
||||
}
|
||||
|
||||
var stackFileLinePattern = regexp.MustCompile(`\s*(.+\.go):(\d+)\s*$`)
|
||||
|
||||
func finalApplicationCaller(stack string) (stackFrame, bool) {
|
||||
if stack == "" {
|
||||
return stackFrame{}, false
|
||||
}
|
||||
functionName := ""
|
||||
for _, raw := range strings.Split(stack, "\n") {
|
||||
line := strings.TrimSpace(raw)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
matches := stackFileLinePattern.FindStringSubmatch(line)
|
||||
if matches == nil {
|
||||
functionName = line
|
||||
continue
|
||||
}
|
||||
lineNumber, _ := strconv.Atoi(matches[2])
|
||||
if skipStackFile(matches[1]) {
|
||||
functionName = ""
|
||||
continue
|
||||
}
|
||||
return stackFrame{File: matches[1], Line: lineNumber, Func: functionName}, true
|
||||
}
|
||||
return stackFrame{}, false
|
||||
}
|
||||
|
||||
func skipStackFile(filename string) bool {
|
||||
normalized := strings.ReplaceAll(filename, "\\", "/")
|
||||
for _, marker := range []string{
|
||||
"/go/pkg/mod/",
|
||||
"/go.uber.org/",
|
||||
"/gorm.io/",
|
||||
"/internal/logging/",
|
||||
"/internal/server/middleware/",
|
||||
"/internal/server/router/",
|
||||
} {
|
||||
if strings.Contains(normalized, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return strings.Contains(normalized, "/src/") &&
|
||||
(strings.Contains(normalized, "/go/go") || strings.Contains(normalized, "/go/src/"))
|
||||
}
|
||||
|
||||
func functionSourceAt(filename string, line int) (name, source string, startLine, endLine int, err error) {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", "", 0, 0, fmt.Errorf("read file failed: %w", err)
|
||||
}
|
||||
files := token.NewFileSet()
|
||||
parsed, err := parser.ParseFile(files, filename, content, parser.ParseComments)
|
||||
if err != nil {
|
||||
return "", "", 0, 0, fmt.Errorf("parse file failed: %w", err)
|
||||
}
|
||||
var target *ast.FuncDecl
|
||||
ast.Inspect(parsed, func(node ast.Node) bool {
|
||||
declaration, ok := node.(*ast.FuncDecl)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
start := files.Position(declaration.Pos()).Line
|
||||
end := files.Position(declaration.End()).Line
|
||||
if line >= start && line <= end {
|
||||
target, startLine, endLine = declaration, start, end
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if target == nil {
|
||||
return "", "", 0, 0, fmt.Errorf("no function encloses line %d in %s", line, filename)
|
||||
}
|
||||
start := files.Position(target.Pos()).Offset
|
||||
end := files.Position(target.End()).Offset
|
||||
if start < 0 || end > len(content) || start >= end {
|
||||
return "", "", 0, 0, fmt.Errorf("invalid offsets for function: start=%d end=%d len=%d", start, end, len(content))
|
||||
}
|
||||
return target.Name.Name, string(content[start:end]), startLine, endLine, nil
|
||||
}
|
||||
@@ -0,0 +1,469 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/contrib/otel/v3/tracing"
|
||||
kratoslog "github.com/go-kratos/kratos/v3/log"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/exp/zapslog"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Level, Format, EncodeLevel, Prefix, StacktraceKey string
|
||||
LogInConsole, ShowLine bool
|
||||
RetentionDay int
|
||||
FileOnlyModules []string
|
||||
}
|
||||
|
||||
// ErrorEntry is the storage-neutral representation of an Error-level log.
|
||||
// Keeping it in internal/logging lets the log core report failures without taking a
|
||||
// dependency on the application service or persistence layers.
|
||||
type ErrorEntry struct {
|
||||
Form, Info, Level, RequestID, TraceID string
|
||||
}
|
||||
|
||||
// ErrorSink receives Error-level log entries. The sink must not log failures
|
||||
// through the same logger, otherwise a storage failure could recurse forever.
|
||||
type ErrorSink interface {
|
||||
RecordLogError(context.Context, ErrorEntry) error
|
||||
}
|
||||
|
||||
type ErrorSinkFunc func(context.Context, ErrorEntry) error
|
||||
|
||||
func (f ErrorSinkFunc) RecordLogError(ctx context.Context, entry ErrorEntry) error {
|
||||
return f(ctx, entry)
|
||||
}
|
||||
|
||||
type errorSinkState struct {
|
||||
mu sync.RWMutex
|
||||
sink ErrorSink
|
||||
}
|
||||
|
||||
func (s *errorSinkState) record(entry ErrorEntry) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
s.mu.RLock()
|
||||
sink := s.sink
|
||||
s.mu.RUnlock()
|
||||
if sink != nil {
|
||||
_ = sink.RecordLogError(context.Background(), entry)
|
||||
}
|
||||
}
|
||||
|
||||
type handlerOperation struct {
|
||||
attrs []slog.Attr
|
||||
group string
|
||||
}
|
||||
|
||||
type reloadableHandlerState struct {
|
||||
mu sync.RWMutex
|
||||
handler slog.Handler
|
||||
cleanup func()
|
||||
}
|
||||
|
||||
type reloadableHandler struct {
|
||||
state *reloadableHandlerState
|
||||
ops []handlerOperation
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) resolved() slog.Handler {
|
||||
current := h.state.handler
|
||||
for _, operation := range h.ops {
|
||||
if operation.group != "" {
|
||||
current = current.WithGroup(operation.group)
|
||||
} else {
|
||||
current = current.WithAttrs(operation.attrs)
|
||||
}
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
h.state.mu.RLock()
|
||||
defer h.state.mu.RUnlock()
|
||||
return h.resolved().Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||
h.state.mu.RLock()
|
||||
defer h.state.mu.RUnlock()
|
||||
return h.resolved().Handle(ctx, record)
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
next := append([]handlerOperation(nil), h.ops...)
|
||||
next = append(next, handlerOperation{attrs: append([]slog.Attr(nil), attrs...)})
|
||||
return &reloadableHandler{state: h.state, ops: next}
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) WithGroup(name string) slog.Handler {
|
||||
next := append([]handlerOperation(nil), h.ops...)
|
||||
next = append(next, handlerOperation{group: name})
|
||||
return &reloadableHandler{state: h.state, ops: next}
|
||||
}
|
||||
|
||||
type ReloadableLogger struct {
|
||||
state *reloadableHandlerState
|
||||
filename string
|
||||
errorSink *errorSinkState
|
||||
}
|
||||
|
||||
func (l *ReloadableLogger) Reload(root string, options Options) {
|
||||
handler, cleanup := newZapHandler(root, l.filename, options, l.errorSink)
|
||||
l.state.mu.Lock()
|
||||
previous := l.state.cleanup
|
||||
l.state.handler = handler
|
||||
l.state.cleanup = cleanup
|
||||
l.state.mu.Unlock()
|
||||
if previous != nil {
|
||||
previous()
|
||||
}
|
||||
}
|
||||
|
||||
// SetErrorSink changes the database/audit target while keeping the current
|
||||
// logger and its hot-reload state intact.
|
||||
func (l *ReloadableLogger) SetErrorSink(sink ErrorSink) {
|
||||
if l == nil || l.errorSink == nil {
|
||||
return
|
||||
}
|
||||
l.errorSink.mu.Lock()
|
||||
l.errorSink.sink = sink
|
||||
l.errorSink.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *ReloadableLogger) Close() {
|
||||
l.state.mu.Lock()
|
||||
cleanup := l.state.cleanup
|
||||
l.state.cleanup = nil
|
||||
l.state.mu.Unlock()
|
||||
if cleanup != nil {
|
||||
cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
type moduleFilterCore struct {
|
||||
zapcore.Core
|
||||
fileOnly map[string]struct{}
|
||||
mod string
|
||||
}
|
||||
|
||||
func (c *moduleFilterCore) With(fields []zapcore.Field) zapcore.Core {
|
||||
mod := c.mod
|
||||
if value := moduleField(fields); value != "" {
|
||||
mod = value
|
||||
}
|
||||
return &moduleFilterCore{Core: c.Core.With(fields), fileOnly: c.fileOnly, mod: mod}
|
||||
}
|
||||
|
||||
func (c *moduleFilterCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if c.Enabled(entry.Level) {
|
||||
return checked.AddCore(entry, c)
|
||||
}
|
||||
return checked
|
||||
}
|
||||
|
||||
func (c *moduleFilterCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
|
||||
mod := c.mod
|
||||
if value := moduleField(fields); value != "" {
|
||||
mod = value
|
||||
}
|
||||
if _, excluded := c.fileOnly[mod]; excluded {
|
||||
return nil
|
||||
}
|
||||
return c.Core.Write(entry, fields)
|
||||
}
|
||||
|
||||
func moduleField(fields []zapcore.Field) string {
|
||||
for _, field := range fields {
|
||||
if field.Key == "mod" {
|
||||
if field.String != "" {
|
||||
return field.String
|
||||
}
|
||||
if field.Interface != nil {
|
||||
return fmt.Sprint(field.Interface)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type routedFileState struct {
|
||||
mu sync.Mutex
|
||||
writers map[string]*DailyWriter
|
||||
}
|
||||
|
||||
type routedFileCore struct {
|
||||
base zapcore.Core
|
||||
encoder zapcore.Encoder
|
||||
level zapcore.LevelEnabler
|
||||
root string
|
||||
retentionDay int
|
||||
state *routedFileState
|
||||
fields []zapcore.Field
|
||||
errorSink *errorSinkState
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Enabled(level zapcore.Level) bool { return c.level.Enabled(level) }
|
||||
|
||||
func (c *routedFileCore) With(fields []zapcore.Field) zapcore.Core {
|
||||
inherited := append([]zapcore.Field(nil), c.fields...)
|
||||
inherited = append(inherited, fields...)
|
||||
return &routedFileCore{base: c.base.With(fields), encoder: c.encoder, level: c.level, root: c.root, retentionDay: c.retentionDay, state: c.state, fields: inherited, errorSink: c.errorSink}
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if c.Enabled(entry.Level) {
|
||||
return checked.AddCore(entry, c)
|
||||
}
|
||||
return checked
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
|
||||
baseErr := c.base.Write(entry, fields)
|
||||
allFields := append([]zapcore.Field(nil), c.fields...)
|
||||
allFields = append(allFields, fields...)
|
||||
if entry.Level >= zapcore.ErrorLevel && !isGORMLoggerEntry(entry.Caller.File, allFields) {
|
||||
c.errorSink.record(errorEntryFromZap(entry, allFields))
|
||||
}
|
||||
paths := routedLogPaths(moduleField(allFields), entry.Level)
|
||||
if len(paths) == 0 {
|
||||
return baseErr
|
||||
}
|
||||
buffer, err := c.encoder.Clone().EncodeEntry(entry, allFields)
|
||||
if err != nil {
|
||||
if baseErr != nil {
|
||||
return baseErr
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer buffer.Free()
|
||||
for _, name := range paths {
|
||||
writer := c.writer(name)
|
||||
if _, writeErr := writer.Write(buffer.Bytes()); writeErr != nil && baseErr == nil {
|
||||
baseErr = writeErr
|
||||
}
|
||||
}
|
||||
return baseErr
|
||||
}
|
||||
|
||||
func isGORMLoggerEntry(filename string, fields []zapcore.Field) bool {
|
||||
for _, field := range fields {
|
||||
if field.Key == "gorm_logger" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
normalized := strings.ReplaceAll(filename, "\\", "/")
|
||||
return strings.HasSuffix(normalized, "/gorm_logger_writer.go") ||
|
||||
strings.HasSuffix(normalized, "/internal/data/gorm_logger.go")
|
||||
}
|
||||
|
||||
func errorEntryFromZap(entry zapcore.Entry, fields []zapcore.Field) ErrorEntry {
|
||||
requestID, traceID, errorText := "", "", ""
|
||||
for _, field := range fields {
|
||||
switch field.Key {
|
||||
case "request_id":
|
||||
if requestID == "" {
|
||||
requestID = zapFieldString(field)
|
||||
}
|
||||
case "trace_id":
|
||||
if traceID == "" {
|
||||
traceID = zapFieldString(field)
|
||||
}
|
||||
case "error", "err":
|
||||
if errorText == "" {
|
||||
errorText = zapFieldString(field)
|
||||
}
|
||||
}
|
||||
}
|
||||
info := entry.Message
|
||||
if errorText != "" {
|
||||
info += " | 错误: " + errorText
|
||||
}
|
||||
if entry.Caller.File != "" {
|
||||
info += fmt.Sprintf(" \n 源文件:%s:%d", entry.Caller.File, entry.Caller.Line)
|
||||
}
|
||||
if entry.Stack != "" {
|
||||
info += " \n 调用栈:" + entry.Stack
|
||||
if frame, ok := finalApplicationCaller(entry.Stack); ok {
|
||||
functionName, source, startLine, endLine, err := functionSourceAt(frame.File, frame.Line)
|
||||
if err == nil {
|
||||
info += fmt.Sprintf(" \n 最终调用方法:%s:%d (%s lines %d-%d)\n----- 产生日志的方法代码如下 -----\n%s", frame.File, frame.Line, functionName, startLine, endLine, source)
|
||||
} else {
|
||||
info += fmt.Sprintf(" \n 最终调用方法:%s:%d (%s) | extract_err=%v", frame.File, frame.Line, functionName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ErrorEntry{Form: "后端", Info: info, Level: entry.Level.String(), RequestID: requestID, TraceID: traceID}
|
||||
}
|
||||
|
||||
func zapFieldString(field zapcore.Field) string {
|
||||
if field.String != "" {
|
||||
return field.String
|
||||
}
|
||||
if field.Interface != nil {
|
||||
return fmt.Sprint(field.Interface)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Sync() error {
|
||||
result := c.base.Sync()
|
||||
c.state.mu.Lock()
|
||||
defer c.state.mu.Unlock()
|
||||
for _, writer := range c.state.writers {
|
||||
if err := writer.Sync(); err != nil && result == nil {
|
||||
result = err
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *routedFileCore) writer(name string) *DailyWriter {
|
||||
c.state.mu.Lock()
|
||||
defer c.state.mu.Unlock()
|
||||
writer := c.state.writers[name]
|
||||
if writer == nil {
|
||||
writer = NewDailyWriter(c.root, name, c.retentionDay)
|
||||
c.state.writers[name] = writer
|
||||
}
|
||||
return writer
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Close() {
|
||||
c.state.mu.Lock()
|
||||
defer c.state.mu.Unlock()
|
||||
for name, writer := range c.state.writers {
|
||||
_ = writer.Close()
|
||||
delete(c.state.writers, name)
|
||||
}
|
||||
}
|
||||
|
||||
func routedLogPaths(module string, level zapcore.Level) []string {
|
||||
paths := make([]string, 0, 2)
|
||||
if module = safeModuleName(module); module != "" {
|
||||
switch module {
|
||||
case "http":
|
||||
paths = append(paths, filepath.Join("http", "access.log"))
|
||||
case "timedTask":
|
||||
paths = append(paths, filepath.Join("timedTask", "task.log"))
|
||||
case "error":
|
||||
paths = append(paths, filepath.Join("error", "error.log"))
|
||||
default:
|
||||
paths = append(paths, filepath.Join(module, "application.log"))
|
||||
}
|
||||
}
|
||||
if level >= zapcore.ErrorLevel {
|
||||
errorPath := filepath.Join("error", "error.log")
|
||||
if len(paths) == 0 || paths[len(paths)-1] != errorPath {
|
||||
paths = append(paths, errorPath)
|
||||
}
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
func safeModuleName(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Map(func(r rune) rune {
|
||||
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '_' {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}, value)
|
||||
}
|
||||
|
||||
// NewZapLogger adapts a Zap core to the slog logger used by Kratos v3.
|
||||
// The file layout remains compatible with the administration log viewer.
|
||||
func newZapHandler(root, filename string, options Options, errorSink *errorSinkState) (slog.Handler, func()) {
|
||||
file := NewDailyWriter(root, filename, options.RetentionDay)
|
||||
encoder := zap.NewProductionEncoderConfig()
|
||||
encoder.EncodeTime = zapcore.RFC3339NanoTimeEncoder
|
||||
if options.StacktraceKey != "" {
|
||||
encoder.StacktraceKey = options.StacktraceKey
|
||||
}
|
||||
switch options.EncodeLevel {
|
||||
case "CapitalLevelEncoder":
|
||||
encoder.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
case "CapitalColorLevelEncoder":
|
||||
encoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
case "LowercaseColorLevelEncoder":
|
||||
encoder.EncodeLevel = zapcore.LowercaseColorLevelEncoder
|
||||
default:
|
||||
encoder.EncodeLevel = zapcore.LowercaseLevelEncoder
|
||||
}
|
||||
var outputEncoder zapcore.Encoder = zapcore.NewJSONEncoder(encoder)
|
||||
if options.Format != "" && options.Format != "json" {
|
||||
if options.Prefix != "" {
|
||||
encoder.EncodeTime = func(value time.Time, output zapcore.PrimitiveArrayEncoder) {
|
||||
output.AppendString(options.Prefix + value.Format("2006-01-02 15:04:05.000"))
|
||||
}
|
||||
}
|
||||
outputEncoder = zapcore.NewConsoleEncoder(encoder)
|
||||
}
|
||||
level := zap.DebugLevel
|
||||
// zapcore.Level.Set reports an error but does not make the intended
|
||||
// configuration handling obvious here. Parse the configured value directly
|
||||
// so debug/warn/error are actually applied after startup and hot reload.
|
||||
if value := strings.TrimSpace(strings.ToLower(options.Level)); value != "" {
|
||||
if err := level.UnmarshalText([]byte(value)); err != nil {
|
||||
level = zap.DebugLevel
|
||||
}
|
||||
}
|
||||
levelEnabler := zap.NewAtomicLevelAt(level)
|
||||
fileCore := zapcore.NewCore(
|
||||
outputEncoder.Clone(),
|
||||
zapcore.AddSync(file),
|
||||
levelEnabler,
|
||||
)
|
||||
core := zapcore.Core(fileCore)
|
||||
if options.LogInConsole {
|
||||
fileOnly := make(map[string]struct{}, len(options.FileOnlyModules))
|
||||
for _, module := range options.FileOnlyModules {
|
||||
fileOnly[module] = struct{}{}
|
||||
}
|
||||
consoleCore := zapcore.NewCore(outputEncoder.Clone(), zapcore.AddSync(os.Stdout), levelEnabler)
|
||||
core = zapcore.NewTee(fileCore, &moduleFilterCore{Core: consoleCore, fileOnly: fileOnly})
|
||||
}
|
||||
routed := &routedFileCore{base: core, encoder: outputEncoder.Clone(), level: levelEnabler, root: root, retentionDay: options.RetentionDay, state: &routedFileState{writers: map[string]*DailyWriter{}}, errorSink: errorSink}
|
||||
zapLogger := zap.New(routed)
|
||||
handlerOptions := []zapslog.HandlerOption{zapslog.AddStacktraceAt(slog.LevelError)}
|
||||
if options.ShowLine {
|
||||
handlerOptions = append(handlerOptions, zapslog.WithCaller(true))
|
||||
}
|
||||
handler := zapslog.NewHandler(zapLogger.Core(), handlerOptions...)
|
||||
cleanup := func() {
|
||||
_ = zapLogger.Sync()
|
||||
routed.Close()
|
||||
_ = file.Close()
|
||||
}
|
||||
return handler, cleanup
|
||||
}
|
||||
|
||||
// NewReloadableZapLogger keeps the slog/Kratos adapter stable while replacing
|
||||
// the underlying Zap core when the runtime configuration changes.
|
||||
func NewReloadableZapLogger(root, filename string, options Options, attrs ...any) (*slog.Logger, *ReloadableLogger) {
|
||||
errorSink := &errorSinkState{}
|
||||
baseHandler, cleanup := newZapHandler(root, filename, options, errorSink)
|
||||
state := &reloadableHandlerState{handler: baseHandler, cleanup: cleanup}
|
||||
control := &ReloadableLogger{state: state, filename: filename, errorSink: errorSink}
|
||||
handler := &contextHandler{handler: &reloadableHandler{state: state}}
|
||||
logger := kratoslog.NewLogger(handler, kratoslog.WithExtractor(tracing.TraceAttrs)).With(attrs...)
|
||||
return logger, control
|
||||
}
|
||||
|
||||
func NewZapLogger(root, filename string, options Options, attrs ...any) (*slog.Logger, func()) {
|
||||
logger, control := NewReloadableZapLogger(root, filename, options, attrs...)
|
||||
return logger, control.Close
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func TestZapHandlerHonorsConfiguredLevel(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
handler, cleanup := newZapHandler(root, "application.log", Options{Level: "error", Format: "json"}, nil)
|
||||
defer cleanup()
|
||||
|
||||
if handler.Enabled(context.Background(), slog.LevelInfo) {
|
||||
t.Fatal("info should be disabled when level is error")
|
||||
}
|
||||
if !handler.Enabled(context.Background(), slog.LevelError) {
|
||||
t.Fatal("error should be enabled when level is error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestZapHandlerUsesDebugFallbackForInvalidLevel(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
handler, cleanup := newZapHandler(root, "application.log", Options{Level: "not-a-level", Format: "json"}, nil)
|
||||
defer cleanup()
|
||||
|
||||
if !handler.Enabled(context.Background(), slog.LevelDebug) {
|
||||
t.Fatal("invalid levels should use the reference debug fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestZapHandlerRoutesHTTPAndErrorLogs(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
handler, cleanup := newZapHandler(root, "application.log", Options{Level: "info", Format: "json"}, nil)
|
||||
logger := slog.New(handler)
|
||||
logger.Info("request", "mod", "http", "request_id", "req-1")
|
||||
logger.Error("failed", "mod", "users")
|
||||
cleanup()
|
||||
|
||||
dateEntries, err := os.ReadDir(root)
|
||||
if err != nil || len(dateEntries) != 1 {
|
||||
t.Fatalf("expected one daily log directory, entries=%v err=%v", len(dateEntries), err)
|
||||
}
|
||||
date := dateEntries[0].Name()
|
||||
paths := []string{
|
||||
filepath.Join(root, date, "application.log"),
|
||||
filepath.Join(root, date, "http", "access.log"),
|
||||
filepath.Join(root, date, "users", "application.log"),
|
||||
filepath.Join(root, date, "error", "error.log"),
|
||||
}
|
||||
for _, path := range paths {
|
||||
if info, err := os.Stat(path); err != nil || info.Size() == 0 {
|
||||
t.Fatalf("expected non-empty routed log %s: info=%v err=%v", path, info, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestZapHandlerRecordsEveryErrorThroughSink(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
var entries []ErrorEntry
|
||||
logger, control := NewReloadableZapLogger(root, "application.log", Options{Level: "info", Format: "json"})
|
||||
defer control.Close()
|
||||
control.SetErrorSink(ErrorSinkFunc(func(_ context.Context, entry ErrorEntry) error {
|
||||
entries = append(entries, entry)
|
||||
return nil
|
||||
}))
|
||||
logger.Error("task execution failed", "mod", "timedTask", "request_id", "request-1", "trace_id", "trace-1", "error", os.ErrPermission)
|
||||
control.Reload(t.TempDir(), Options{Level: "info", Format: "json"})
|
||||
logger.Error("reloaded logger failure", "mod", "system")
|
||||
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("expected two error entries across reload, got %d", len(entries))
|
||||
}
|
||||
entry := entries[0]
|
||||
if entry.Form != "后端" || entry.Level != "error" || entry.RequestID != "request-1" || entry.TraceID != "trace-1" || !strings.Contains(entry.Info, "task execution failed") || !strings.Contains(entry.Info, os.ErrPermission.Error()) {
|
||||
t.Fatalf("unexpected error entry: %+v", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorEntryIncludesFinalApplicationSource(t *testing.T) {
|
||||
filename := filepath.Join(t.TempDir(), "worker.go")
|
||||
content := "package sample\n\nfunc execute() {\n\tprintln(\"failed\")\n}\n"
|
||||
if err := os.WriteFile(filename, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entry := errorEntryFromZap(zapcore.Entry{Message: "task failed", Level: zapcore.ErrorLevel, Stack: "kra/internal/worker.execute\n" + filename + ":4"}, nil)
|
||||
if !strings.Contains(entry.Info, "最终调用方法:"+filename+":4 (execute lines 3-5)") || !strings.Contains(entry.Info, "func execute()") {
|
||||
t.Fatalf("expected final caller source in error entry: %s", entry.Info)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorSinkSkipsGORMBridge(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
var entries []ErrorEntry
|
||||
state := &errorSinkState{sink: ErrorSinkFunc(func(_ context.Context, entry ErrorEntry) error {
|
||||
entries = append(entries, entry)
|
||||
return nil
|
||||
})}
|
||||
base := zapcore.NewNopCore()
|
||||
core := &routedFileCore{base: base, encoder: zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), level: zapcore.ErrorLevel, root: root, state: &routedFileState{writers: map[string]*DailyWriter{}}, errorSink: state}
|
||||
defer core.Close()
|
||||
for _, filename := range []string{"/tmp/gorm_logger_writer.go", "/workspace/internal/data/gorm_logger.go"} {
|
||||
if err := core.Write(zapcore.Entry{Level: zapcore.ErrorLevel, Message: "database failed", Caller: zapcore.EntryCaller{Defined: true, File: filename, Line: 10}}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := core.Write(zapcore.Entry{Level: zapcore.ErrorLevel, Message: "database failed"}, []zapcore.Field{zap.Bool("gorm_logger", true)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("gorm bridge error must not recurse into sys_error: %+v", entries)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user