Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 日志模块大一统,提供全局日志 #1062

Merged
merged 17 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dice/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2613,11 +2613,11 @@ func (d *Dice) Save(isAuto bool) {
}
d.LastUpdatedTime = 0
} else if err1 != nil && err2 != nil {
d.Logger.Errorln("保存 serve.yaml 和 advanced.yaml 出错", err2)
d.Logger.Error("保存 serve.yaml 和 advanced.yaml 出错", err2)
} else if err1 != nil {
d.Logger.Errorln("保存 serve.yaml 出错", err1)
d.Logger.Error("保存 serve.yaml 出错", err1)
} else {
d.Logger.Errorln("保存 advanced.yaml 出错", err2)
d.Logger.Error("保存 advanced.yaml 出错", err2)
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions dice/dice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import (
"github.com/robfig/cron/v3"
ds "github.com/sealdice/dicescript"
"github.com/tidwall/buntdb"
"go.uber.org/zap"

rand2 "golang.org/x/exp/rand"
"golang.org/x/exp/slices"
"golang.org/x/time/rate"

"sealdice-core/dice/censor"
"sealdice-core/dice/logger"
"sealdice-core/dice/model"
log "sealdice-core/utils/kratos"
)

type CmdExecuteResult struct {
Expand Down Expand Up @@ -104,10 +103,10 @@ type ExtInfo struct {
OnLoad func() `yaml:"-" json:"-" jsbind:"onLoad"`
}

// DiceConfig TODO:历史遗留问题,由于不输出DICE日志效果过差,已经抹除日志输出选项,剩余两个选项,私以为可以想办法也抹除掉。
type DiceConfig struct { //nolint:revive
Name string `yaml:"name"` // 名称,默认为default
DataDir string `yaml:"dataDir"` // 数据路径,为./data/{name},例如data/default
IsLogPrint bool `yaml:"isLogPrint"` // 是否在控制台打印log
Name string `yaml:"name"` // 名称,默认为default
DataDir string `yaml:"dataDir"` // 数据路径,为./data/{name},例如data/default
}

type ExtDefaultSettingItem struct {
Expand Down Expand Up @@ -138,8 +137,8 @@ type Dice struct {
BaseConfig DiceConfig `yaml:"-"`
DBData *sqlx.DB `yaml:"-"` // 数据库对象
DBLogs *sqlx.DB `yaml:"-"` // 数据库对象
Logger *zap.SugaredLogger `yaml:"-"` // 日志
LogWriter *logger.WriterX `yaml:"-"` // 用于api的log对象
Logger *log.Helper `yaml:"-"` // 日志
LogWriter *log.WriterX `yaml:"-"` // 用于api的log对象
IsDeckLoading bool `yaml:"-"` // 正在加载中
DeckList []*DeckInfo `yaml:"deckList" jsbind:"deckList"` // 牌堆信息
CommandPrefix []string `yaml:"commandPrefix" jsbind:"commandPrefix"` // 指令前导
Expand Down Expand Up @@ -314,7 +313,7 @@ func (d *Dice) Init() {
_ = os.MkdirAll(filepath.Join(d.BaseConfig.DataDir, "extra"), 0o755)
_ = os.MkdirAll(filepath.Join(d.BaseConfig.DataDir, "scripts"), 0o755)

log := logger.Init(filepath.Join(d.BaseConfig.DataDir, "record.log"), d.BaseConfig.Name, d.BaseConfig.IsLogPrint)
log := logger.Init()
d.Logger = log.Logger
d.LogWriter = log.WX

Expand Down
4 changes: 2 additions & 2 deletions dice/dice_attrs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

"github.com/jmoiron/sqlx"
ds "github.com/sealdice/dicescript"
"go.uber.org/zap"

"sealdice-core/dice/model"
log "sealdice-core/utils/kratos"
)

type AttrsManager struct {
db *sqlx.DB
logger *zap.SugaredLogger
logger *log.Helper
m SyncMap[string, *AttributesItem]
}

Expand Down
6 changes: 3 additions & 3 deletions dice/dice_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,20 @@ func (dm *DiceManager) Backup(sel BackupSelection, fromAuto bool) (string, error

err := model.FlushWAL(d.DBData)
if err != nil {
d.Logger.Warnln("备份时data数据库flush出错", err.Error())
d.Logger.Errorf("备份时data数据库flush出错 错误为:%v", err.Error())
} else {
backup(d, filepath.Join(dataDir, "data.db"))
}
err = model.FlushWAL(d.DBLogs)
if err != nil {
d.Logger.Warnln("备份时logs数据库flush出错", err.Error())
d.Logger.Errorf("备份时logs数据库flush出错 错误为:%v", err.Error())
} else {
backup(d, filepath.Join(dataDir, "data-logs.db"))
}
if d.CensorManager != nil && d.CensorManager.DB != nil {
err = model.FlushWAL(d.CensorManager.DB)
if err != nil {
d.Logger.Warnln("备份时censor数据库flush出错", err.Error())
d.Logger.Errorf("备份时censor数据库flush出错 %v", err.Error())
} else {
backup(d, filepath.Join(dataDir, "data-censor.db"))
}
Expand Down
4 changes: 2 additions & 2 deletions dice/dice_jsvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
"github.com/samber/lo"
"go.uber.org/zap"
"gopkg.in/elazarl/goproxy.v1"
"gopkg.in/yaml.v3"

"sealdice-core/static"
"sealdice-core/utils/crypto"
log "sealdice-core/utils/kratos"
)

var (
Expand Down Expand Up @@ -1335,7 +1335,7 @@ type JsScriptTask struct {
entryID *cron.EntryID
lock *sync.Mutex

logger *zap.SugaredLogger
logger *log.Helper
}

type JsScriptTaskCtx struct {
Expand Down
6 changes: 3 additions & 3 deletions dice/dice_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

"github.com/dop251/goja_nodejs/require"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
"gopkg.in/yaml.v3"

log "sealdice-core/utils/kratos"
)

type VersionInfo struct {
Expand Down Expand Up @@ -75,7 +76,7 @@ type DiceManager struct { //nolint:revive
ServiceName string
JustForTest bool
JsRegistry *require.Registry
UpdateSealdiceByFile func(packName string, log *zap.SugaredLogger) bool // 使用指定压缩包升级海豹,如果出错返回false,如果成功进程会自动结束
UpdateSealdiceByFile func(packName string, log *log.Helper) bool // 使用指定压缩包升级海豹,如果出错返回false,如果成功进程会自动结束

ContainerMode bool // 容器模式:禁用内置适配器,不允许使用内置Lagrange和旧的内置Gocq
CleanupFlag atomic.Uint32 // 1 为正在清理,0为普通状态
Expand Down Expand Up @@ -325,7 +326,6 @@ func (dm *DiceManager) TryCreateDefault() {
if len(dm.Dice) == 0 {
defaultDice := new(Dice)
defaultDice.BaseConfig.Name = "default"
defaultDice.BaseConfig.IsLogPrint = true
defaultDice.MessageDelayRangeStart = 0.4
defaultDice.MessageDelayRangeEnd = 0.9
defaultDice.MarkModified()
Expand Down
4 changes: 3 additions & 1 deletion dice/dice_name_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

wr "github.com/mroth/weightedrand"
"github.com/xuri/excelize/v2"

log "sealdice-core/utils/kratos"
)

type NamesGenerator struct {
Expand All @@ -24,7 +26,7 @@ func (ng *NamesGenerator) Load() {
for _, fn := range []string{"./data/names/names.xlsx", "./data/names/names-dnd.xlsx"} {
f, err := excelize.OpenFile(fn)
if err != nil {
fmt.Println("加载names信息出错", fn, err)
log.Warn("加载names信息出错", fn, err)
continue
}

Expand Down
4 changes: 2 additions & 2 deletions dice/js_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"os"
"path/filepath"

"go.uber.org/zap"
log "sealdice-core/utils/kratos"
)

func Base64ToImageFunc(logger *zap.SugaredLogger) func(string) (string, error) {
func Base64ToImageFunc(logger *log.Helper) func(string) (string, error) {
return func(b64 string) (string, error) {
// 解码 Base64 值
data, e := base64.StdEncoding.DecodeString(b64)
Expand Down
111 changes: 9 additions & 102 deletions dice/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,113 +1,20 @@
package logger

import (
"encoding/json"
"os"

"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
log "sealdice-core/utils/kratos"
)

var enabledLevel = zap.InfoLevel

func SetEnableLevel(level zapcore.Level) {
switch level {
case zapcore.DebugLevel, zapcore.InfoLevel, zapcore.WarnLevel, zapcore.ErrorLevel,
zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel:
{
enabledLevel = level
}
default: // no-op
}
}

type LogItem struct {
Level string `json:"level"`
TS float64 `json:"ts"`
Caller string `json:"caller"`
Msg string `json:"msg"`
}

type WriterX struct {
LogLimit int64
Items []*LogItem
}

type LogInfo struct {
LoggerRaw *zap.Logger
Logger *zap.SugaredLogger
WX *WriterX
Logger *log.Helper
WX *log.WriterX
}

var logLimitDefault int64 = 100

func (w *WriterX) Write(p []byte) (n int, err error) {
var a LogItem
err2 := json.Unmarshal(p, &a)
if err2 == nil {
w.Items = append(w.Items, &a)
limit := w.LogLimit
if limit == 0 {
w.LogLimit = logLimitDefault
}
if len(w.Items) > int(limit) {
w.Items = w.Items[1:]
}
}
return len(p), nil
}

func Init(path string, name string, enableConsoleLog bool) *LogInfo {
lumlog := &lumberjack.Logger{
Filename: path,
MaxSize: 10, // megabytes
MaxBackups: 3, // number of log files
MaxAge: 7, // days
}

encoder := getEncoder()

pe := zap.NewProductionEncoderConfig()
wx := &WriterX{}

cores := []zapcore.Core{
zapcore.NewCore(encoder, zapcore.AddSync(lumlog), enabledLevel),

// This outputs to WebUI, DO NOT apply enabledLevel
zapcore.NewCore(zapcore.NewJSONEncoder(pe), zapcore.AddSync(wx), zapcore.InfoLevel),
}

if enableConsoleLog {
pe2 := zap.NewProductionEncoderConfig()
pe2.EncodeTime = zapcore.ISO8601TimeEncoder

consoleEncoder := zapcore.NewConsoleEncoder(pe2)
consoleEncoder.AddString("dice", name)
cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), enabledLevel))
}

core := zapcore.NewTee(cores...)

loggerRaw := zap.New(core, zap.AddCaller())
defer func(loggerRaw *zap.Logger) {
_ = loggerRaw.Sync()
}(loggerRaw) // flushes buffer, if any

logger := loggerRaw.Sugar()
logger.Infow("Dice日志开始记录")

func Init() *LogInfo {
// KV输出
loghelper := log.NewCustomHelper(log.LOG_DICE, false, nil)
loghelper.Info("Dice日志开始记录")
return &LogInfo{
LoggerRaw: loggerRaw,
Logger: logger,
WX: wx,
Logger: loghelper,
WX: log.GetWriterX(),
}
}

func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder

return zapcore.NewConsoleEncoder(encoderConfig)
}
1 change: 0 additions & 1 deletion dice/model/group_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func GroupInfoListGet(db *sqlx.DB, callback func(id string, updatedAt int64, dat

err = rows.Scan(&id, &pUpdatedAt, &data)
if err != nil {
fmt.Println("!!!", err.Error())
return err
}

Expand Down
4 changes: 3 additions & 1 deletion dice/model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"

"github.com/jmoiron/sqlx"

log "sealdice-core/utils/kratos"
)

type LogOne struct {
Expand Down Expand Up @@ -480,7 +482,7 @@ func LogMarkDeleteByMsgID(db *sqlx.DB, groupID string, logName string, rawID int
// fmt.Printf("log delete %v %d\n", rawId, logId)
_, err = db.Exec("DELETE FROM log_items WHERE log_id=? AND raw_msg_id=?", logID, rid)
if err != nil {
fmt.Println("log delete error", err.Error())
log.Error("log delete error", err.Error())
return err
}

Expand Down
10 changes: 5 additions & 5 deletions dice/platform_adapter_gocq.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"github.com/labstack/echo/v4"
"github.com/sacOO7/gowebsocket"
"github.com/samber/lo"
"go.uber.org/zap"
"gopkg.in/yaml.v3"

"sealdice-core/message"
log "sealdice-core/utils/kratos"
"sealdice-core/utils/procs"
)

Expand Down Expand Up @@ -273,7 +273,7 @@ func FormatDiceIDQQChGroup(guildID, channelID string) string {
return fmt.Sprintf("QQ-CH-Group:%s-%s", guildID, channelID)
}

func tryParseOneBot11ArrayMessage(log *zap.SugaredLogger, message string, writeTo *MessageQQ) error {
func tryParseOneBot11ArrayMessage(log *log.Helper, message string, writeTo *MessageQQ) error {
msgQQType2 := new(MessageQQArray)
err := json.Unmarshal([]byte(message), msgQQType2)

Expand Down Expand Up @@ -560,7 +560,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
} else {
// TODO: 这玩意的创建是个专业活,等下来弄
// session.ServiceAtNew[groupId] = GroupInfo{}
fmt.Println("TODO create group")
log.Debug("TODO create group")
}
// 这句话太吵了
// log.Debug("群信息刷新: ", msgQQ.Data.GroupName)
Expand Down Expand Up @@ -992,7 +992,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
if pa.riskAlertShieldCount > 0 {
pa.riskAlertShieldCount--
} else {
fmt.Println("群消息发送失败: 账号可能被风控")
log.Warn("群消息发送失败: 账号可能被风控")
_ = ctx.Dice.SendMail("群消息发送失败: 账号可能被风控", MailTypeCIAMLock)
}
}
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func (pa *PlatformAdapterGocq) Serve() int {
}
session.Execute(ep, msg, false)
} else {
fmt.Println("Received message " + message)
log.Debug("Received message " + message)
}
}

Expand Down
Loading
Loading