优化日志
This commit is contained in:
+62
-5
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -27,27 +28,83 @@ func initLogging(store *memoryLogStore, logDir string) func() {
|
||||
logDir = defaultLogDir
|
||||
}
|
||||
if err := os.MkdirAll(logDir, 0755); err != nil {
|
||||
log.SetOutput(io.MultiWriter(os.Stdout, store))
|
||||
async := newAsyncLogWriter(io.MultiWriter(os.Stdout, store), 2048)
|
||||
log.SetOutput(async)
|
||||
log.Printf("日志目录创建失败,仅写入终端和内存: %v", err)
|
||||
return func() {}
|
||||
return func() { _ = async.Close() }
|
||||
}
|
||||
|
||||
filePath := filepath.Join(logDir, "douyu_"+time.Now().In(cst).Format("20060102_150405")+".log")
|
||||
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
log.SetOutput(io.MultiWriter(os.Stdout, store))
|
||||
async := newAsyncLogWriter(io.MultiWriter(os.Stdout, store), 2048)
|
||||
log.SetOutput(async)
|
||||
log.Printf("日志文件创建失败,仅写入终端和内存: %v", err)
|
||||
return func() {}
|
||||
return func() { _ = async.Close() }
|
||||
}
|
||||
|
||||
store.setFilePath(filePath)
|
||||
log.SetOutput(io.MultiWriter(os.Stdout, file, store))
|
||||
async := newAsyncLogWriter(io.MultiWriter(os.Stdout, file, store), 2048)
|
||||
log.SetOutput(async)
|
||||
log.Printf("日志文件已启用: %s", filePath)
|
||||
return func() {
|
||||
_ = async.Close()
|
||||
_ = file.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func newAsyncLogWriter(dst io.Writer, buffer int) *asyncLogWriter {
|
||||
if buffer <= 0 {
|
||||
buffer = 1024
|
||||
}
|
||||
w := &asyncLogWriter{
|
||||
dst: dst,
|
||||
ch: make(chan []byte, buffer),
|
||||
}
|
||||
w.wg.Add(1)
|
||||
go w.run()
|
||||
return w
|
||||
}
|
||||
|
||||
// Write 异步写日志;队列满时丢弃,避免抢购请求被磁盘 I/O 阻塞。
|
||||
func (w *asyncLogWriter) Write(p []byte) (int, error) {
|
||||
if w == nil {
|
||||
return len(p), nil
|
||||
}
|
||||
if atomic.LoadUint32(&w.closed) == 1 {
|
||||
return len(p), nil
|
||||
}
|
||||
buf := append([]byte(nil), p...)
|
||||
select {
|
||||
case w.ch <- buf:
|
||||
default:
|
||||
atomic.AddUint64(&w.dropped, 1)
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (w *asyncLogWriter) run() {
|
||||
defer w.wg.Done()
|
||||
for p := range w.ch {
|
||||
_, _ = w.dst.Write(p)
|
||||
}
|
||||
if dropped := atomic.LoadUint64(&w.dropped); dropped > 0 {
|
||||
_, _ = fmt.Fprintf(w.dst, "%s [日志][warn] 日志队列已丢弃 %d 条,已优先保护抢购链路\n",
|
||||
time.Now().In(cst).Format("2006/01/02 15:04:05.000000"), dropped)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *asyncLogWriter) Close() error {
|
||||
if w == nil {
|
||||
return nil
|
||||
}
|
||||
if atomic.CompareAndSwapUint32(&w.closed, 0, 1) {
|
||||
close(w.ch)
|
||||
w.wg.Wait()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write 实现 io.Writer,把标准 log 输出转存为内存日志。
|
||||
func (s *memoryLogStore) Write(p []byte) (int, error) {
|
||||
text := strings.TrimRight(string(p), "\r\n")
|
||||
|
||||
Reference in New Issue
Block a user