Files
GoProxy/logger/logger.go
isboyjc f55209d8d3 feat: init
2026-03-29 03:31:59 +08:00

54 lines
901 B
Go

package logger
import (
"fmt"
"log"
"strings"
"sync"
"time"
)
const maxLines = 500
var (
lines []string
mu sync.RWMutex
)
// Init 替换标准 log 输出,同时保留控制台输出
func Init() {
log.SetFlags(0)
log.SetOutput(&writer{})
}
type writer struct{}
func (w *writer) Write(p []byte) (n int, err error) {
line := strings.TrimRight(string(p), "\n")
ts := time.Now().Format("15:04:05")
formatted := fmt.Sprintf("[%s] %s", ts, line)
mu.Lock()
lines = append(lines, formatted)
if len(lines) > maxLines {
lines = lines[len(lines)-maxLines:]
}
mu.Unlock()
// 同时输出到控制台
fmt.Println(formatted)
return len(p), nil
}
// GetLines 返回最近 N 条日志
func GetLines(n int) []string {
mu.RLock()
defer mu.RUnlock()
if n <= 0 || n > len(lines) {
n = len(lines)
}
result := make([]string, n)
copy(result, lines[len(lines)-n:])
return result
}