完成日志输出到文件、终端多处的处理
This commit is contained in:
+2
-2
@@ -1,10 +1,10 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"game-driver/pkg/logger"
|
||||
"github.com/gopxl/beep/v2"
|
||||
"github.com/gopxl/beep/v2/mp3"
|
||||
"github.com/gopxl/beep/v2/speaker"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
)
|
||||
|
||||
@@ -16,7 +16,7 @@ func PlayBgmMP3(r io.ReadCloser, opts ...beep.LoopOption) (*beep.Ctrl, func() er
|
||||
|
||||
loop2, err := beep.Loop2(streamer, opts...)
|
||||
if err != nil {
|
||||
logger.Infoln("循环播放异常: ", err)
|
||||
zap.S().Infoln("循环播放异常: ", err)
|
||||
return nil, streamer.Close, err
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -2,11 +2,11 @@ package audio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"game-driver/pkg/logger"
|
||||
"github.com/gopxl/beep/v2"
|
||||
"github.com/gopxl/beep/v2/mp3"
|
||||
"github.com/gopxl/beep/v2/speaker"
|
||||
"github.com/gopxl/beep/v2/wav"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
@@ -18,7 +18,7 @@ func init() {
|
||||
if err != nil {
|
||||
panic("扬声器初始化异常: " + err.Error())
|
||||
}
|
||||
logger.Infoln("扬声器初始化完成")
|
||||
zap.S().Infoln("扬声器初始化完成")
|
||||
}
|
||||
|
||||
func PlayWav(c context.Context, r io.Reader) {
|
||||
|
||||
@@ -1,207 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Logger struct {
|
||||
zl *zap.Logger
|
||||
zs zap.SugaredLogger
|
||||
}
|
||||
|
||||
func NewLogger() *Logger {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugar := logger.WithOptions(zap.AddCallerSkip(1)).Sugar()
|
||||
return &Logger{
|
||||
zl: logger,
|
||||
zs: *sugar,
|
||||
}
|
||||
}
|
||||
|
||||
// ZapLogger 返回 zap 原始的 zap.Logger
|
||||
func (l *Logger) ZapLogger() *zap.Logger {
|
||||
return l.zl
|
||||
}
|
||||
|
||||
// Sync 刷新所有缓冲的日志条目
|
||||
func (l *Logger) Sync() error {
|
||||
return l.zs.Sync()
|
||||
}
|
||||
|
||||
var DefaultLogger = NewLogger()
|
||||
|
||||
// Debug logs the provided arguments at [DebugLevel].
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func Debug(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.DebugLevel, args...)
|
||||
}
|
||||
|
||||
// Info logs the provided arguments at [InfoLevel].
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func Info(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.InfoLevel, args...)
|
||||
}
|
||||
|
||||
// Warn logs the provided arguments at [WarnLevel].
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func Warn(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.WarnLevel, args...)
|
||||
}
|
||||
|
||||
// Error logs the provided arguments at [ErrorLevel].
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func Error(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.ErrorLevel, args...)
|
||||
}
|
||||
|
||||
// DPanic logs the provided arguments at [DPanicLevel].
|
||||
// In development, the logger then panics. (See [DPanicLevel] for details.)
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func DPanic(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.DPanicLevel, args...)
|
||||
}
|
||||
|
||||
// Panic constructs a message with the provided arguments and panics.
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func Panic(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.PanicLevel, args...)
|
||||
}
|
||||
|
||||
// Fatal constructs a message with the provided arguments and calls os.Exit.
|
||||
// Spaces are added between arguments when neither is a string.
|
||||
func Fatal(args ...interface{}) {
|
||||
DefaultLogger.zs.Log(zap.FatalLevel, args...)
|
||||
}
|
||||
|
||||
// Debugf formats the message according to the format specifier
|
||||
// and logs it at [DebugLevel].
|
||||
func Debugf(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.DebugLevel, template, args...)
|
||||
}
|
||||
|
||||
// Infof formats the message according to the format specifier
|
||||
// and logs it at [InfoLevel].
|
||||
func Infof(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.InfoLevel, template, args...)
|
||||
}
|
||||
|
||||
// Warnf formats the message according to the format specifier
|
||||
// and logs it at [WarnLevel].
|
||||
func Warnf(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.WarnLevel, template, args...)
|
||||
}
|
||||
|
||||
// Errorf formats the message according to the format specifier
|
||||
// and logs it at [ErrorLevel].
|
||||
func Errorf(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.ErrorLevel, template, args...)
|
||||
}
|
||||
|
||||
// DPanicf formats the message according to the format specifier
|
||||
// and logs it at [DPanicLevel].
|
||||
// In development, the logger then panics. (See [DPanicLevel] for details.)
|
||||
func DPanicf(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.DPanicLevel, template, args...)
|
||||
}
|
||||
|
||||
// Panicf formats the message according to the format specifier
|
||||
// and panics.
|
||||
func Panicf(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.PanicLevel, template, args...)
|
||||
}
|
||||
|
||||
// Fatalf formats the message according to the format specifier
|
||||
// and calls os.Exit.
|
||||
func Fatalf(template string, args ...interface{}) {
|
||||
DefaultLogger.zs.Logf(zap.FatalLevel, template, args...)
|
||||
}
|
||||
|
||||
// Debugw logs a message with some additional context. The variadic key-value
|
||||
// pairs are treated as they are in With.
|
||||
//
|
||||
// When debug-level logging is disabled, this is much faster than
|
||||
//
|
||||
// s.With(keysAndValues).Debug(msg)
|
||||
func Debugw(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.DebugLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Infow logs a message with some additional context. The variadic key-value
|
||||
// pairs are treated as they are in With.
|
||||
func Infow(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.InfoLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Warnw logs a message with some additional context. The variadic key-value
|
||||
// pairs are treated as they are in With.
|
||||
func Warnw(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.WarnLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Errorw logs a message with some additional context. The variadic key-value
|
||||
// pairs are treated as they are in With.
|
||||
func Errorw(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.ErrorLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// DPanicw logs a message with some additional context. In development, the
|
||||
// logger then panics. (See DPanicLevel for details.) The variadic key-value
|
||||
// pairs are treated as they are in With.
|
||||
func DPanicw(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.DPanicLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Panicw logs a message with some additional context, then panics. The
|
||||
// variadic key-value pairs are treated as they are in With.
|
||||
func Panicw(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.PanicLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Fatalw logs a message with some additional context, then calls os.Exit. The
|
||||
// variadic key-value pairs are treated as they are in With.
|
||||
func Fatalw(msg string, keysAndValues ...interface{}) {
|
||||
DefaultLogger.zs.Logw(zap.FatalLevel, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
// Debugln logs a message at [DebugLevel].
|
||||
// Spaces are always added between arguments.
|
||||
func Debugln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.DebugLevel, args...)
|
||||
}
|
||||
|
||||
// Infoln logs a message at [InfoLevel].
|
||||
// Spaces are always added between arguments.
|
||||
func Infoln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.InfoLevel, args...)
|
||||
}
|
||||
|
||||
// Warnln logs a message at [WarnLevel].
|
||||
// Spaces are always added between arguments.
|
||||
func Warnln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.WarnLevel, args...)
|
||||
}
|
||||
|
||||
// Errorln logs a message at [ErrorLevel].
|
||||
// Spaces are always added between arguments.
|
||||
func Errorln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.ErrorLevel, args...)
|
||||
}
|
||||
|
||||
// DPanicln logs a message at [DPanicLevel].
|
||||
// In development, the logger then panics. (See [DPanicLevel] for details.)
|
||||
// Spaces are always added between arguments.
|
||||
func DPanicln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.DPanicLevel, args...)
|
||||
}
|
||||
|
||||
// Panicln logs a message at [PanicLevel] and panics.
|
||||
// Spaces are always added between arguments.
|
||||
func Panicln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.PanicLevel, args...)
|
||||
}
|
||||
|
||||
// Fatalln logs a message at [FatalLevel] and calls os.Exit.
|
||||
// Spaces are always added between arguments.
|
||||
func Fatalln(args ...interface{}) {
|
||||
DefaultLogger.zs.Logln(zap.FatalLevel, args...)
|
||||
}
|
||||
+2
-2
@@ -8,7 +8,7 @@ import (
|
||||
"game-driver/leaf"
|
||||
"game-driver/pkg/audio"
|
||||
"game-driver/pkg/errorsx"
|
||||
"game-driver/pkg/logger"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"log"
|
||||
"time"
|
||||
@@ -49,7 +49,7 @@ func (tts *AliTTS) Sound(text string) {
|
||||
if err == nil && buf != nil {
|
||||
audio.PlayWav(tts.ctx, buf)
|
||||
} else {
|
||||
logger.Errorln("AliTTS 请求异常: ", err)
|
||||
zap.S().Errorln("AliTTS 请求异常: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package utils
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"game-driver/pkg/logger"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -33,7 +33,7 @@ func open(u string) io.ReadCloser {
|
||||
p, _ := strings.CutPrefix(u, "file://")
|
||||
f, e := os.Open(p)
|
||||
if e != nil {
|
||||
logger.Infof("音频文件 [%v] 打开错误: %v\n", u, e)
|
||||
zap.S().Infof("音频文件 [%v] 打开错误: %v\n", u, e)
|
||||
return nil
|
||||
}
|
||||
return f
|
||||
@@ -42,7 +42,7 @@ func open(u string) io.ReadCloser {
|
||||
func get(u string) io.ReadCloser {
|
||||
resp, e := http.Get(u)
|
||||
if e != nil {
|
||||
logger.Infof("音频文件 [%v] 下载失败: %v\n", u, e)
|
||||
zap.S().Infof("音频文件 [%v] 下载失败: %v\n", u, e)
|
||||
return nil
|
||||
}
|
||||
return resp.Body
|
||||
|
||||
+3
-3
@@ -3,7 +3,7 @@ package video
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"game-driver/pkg/logger"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"os/exec"
|
||||
"sync"
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
func Play(ctx context.Context, file string) error {
|
||||
if file == "" {
|
||||
logger.Infoln("video file is empty")
|
||||
zap.S().Infoln("video file is empty")
|
||||
return nil
|
||||
}
|
||||
cmd := exec.CommandContext(ctx, "ffplay", "-autoexit", "-fs", file)
|
||||
@@ -40,7 +40,7 @@ func Play(ctx context.Context, file string) error {
|
||||
}
|
||||
break
|
||||
}
|
||||
logger.Infoln(string(line))
|
||||
zap.S().Infoln(string(line))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user