Skip to content

Commit

Permalink
feat: output log to file and update them periodically (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
mutezebra authored Dec 20, 2024
1 parent 67125a8 commit f4c4864
Show file tree
Hide file tree
Showing 15 changed files with 602 additions and 193 deletions.
13 changes: 4 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,10 @@ func getService(name string) *service {
}
}

// GetLoggerLevel 会尝试对文本日志等级做转换,如果失败则返回默认 INFO
func GetLoggerLevel() int64 {
// GetLoggerLevel 会返回服务的日志等级
func GetLoggerLevel() string {
if Server == nil {
return constants.LevelInfo // 默认 INFO
return constants.DefaultLogLevel
}

v, ok := constants.LevelMap[Server.LogLevel]
if !ok {
return constants.LevelInfo // 默认 INFO
}
return v
return Server.LogLevel
}
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/hertz-contrib/cors v0.1.0
github.com/hertz-contrib/gzip v0.0.3
github.com/hertz-contrib/opensergo v0.0.1
github.com/kitex-contrib/obs-opentelemetry/logging/zap v0.0.0-20241120035129-55da83caab1b
github.com/kitex-contrib/registry-etcd v0.2.5
github.com/redis/go-redis/v9 v9.7.0
github.com/segmentio/kafka-go v0.4.47
Expand All @@ -42,7 +41,7 @@ require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/bytedance/gopkg v0.1.1
github.com/bytedance/gopkg v0.1.1 // indirect
github.com/bytedance/sonic v1.12.6
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down Expand Up @@ -123,7 +122,7 @@ require (
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
github.com/smartystreets/goconvey v1.7.2
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
Expand All @@ -150,7 +149,7 @@ require (
golang.org/x/arch v0.12.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20241215155358-4a5509556b9e // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.10.0
golang.org/x/sys v0.28.0 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,6 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kitex-contrib/obs-opentelemetry/logging/zap v0.0.0-20241120035129-55da83caab1b h1:td+0YgPBJkuA8srZwO7SjqYqNSW0fEdGDaCEWAmzQGA=
github.com/kitex-contrib/obs-opentelemetry/logging/zap v0.0.0-20241120035129-55da83caab1b/go.mod h1:NUuhhOLwpinYkidyb6X+e8LS+ccZO4NL95QfTjg0//E=
github.com/kitex-contrib/registry-etcd v0.2.5 h1:16On5GQAmN9tdbFy95QtwGH3aTWnW/BfdvdDmzLIytE=
github.com/kitex-contrib/registry-etcd v0.2.5/go.mod h1:Imgvy+EkqExHuJs4+VFK0g1bFWHaCJ0KuK1sofWYZf4=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
Expand Down Expand Up @@ -706,8 +704,6 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0 h1:DeFD0VgTZ+Cj6hxravY
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0/go.mod h1:GijYcYmNpX1KazD5JmWGsi4P7dDTTTnfv1UbGn84MnU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0 h1:gvmNvqrPYovvyRmCSygkUDyL8lC5Tl845MLEwqpxhEU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0/go.mod h1:vNUq47TGFioo+ffTSnKNdob241vePmtNZnAODKapKd0=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 h1:+XWJd3jf75RXJq29mxbuXhCXFDG3S3R4vBUeSI2P7tE=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0/go.mod h1:hqgzBPTf4yONMFgdZvL/bK42R/iinTyVQtiWihs3SZc=
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
Expand Down Expand Up @@ -793,8 +789,8 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
Expand Down
2 changes: 1 addition & 1 deletion pkg/base/client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func InitMySQL(tableName string) (db *gorm.DB, err error) {
SingularTable: true, // 使用单数表名
},
Logger: glogger.New(
logger.GetLogger(),
logger.GetMysqlLogger(),
glogger.Config{
SlowThreshold: time.Second, // 超过一秒的查询被认为是慢查询
LogLevel: glogger.Warn, // 日志等级
Expand Down
58 changes: 36 additions & 22 deletions pkg/base/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,55 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
. "github.com/smartystreets/goconvey/convey"

"github.com/west2-online/fzuhelper-server/pkg/errno"
)

func TestBuildBaseResp(t *testing.T) {
nilError := BuildBaseResp(nil)
assert.Equal(t, int64(errno.SuccessCode), nilError.Code)
assert.Equal(t, errno.Success.ErrorMsg, nilError.Msg)
Convey("TestBuildBaseResp", t, func() {
nilError := BuildBaseResp(nil)
So(nilError.Code, ShouldEqual, int64(errno.SuccessCode))
So(nilError.Code, ShouldEqual, int64(errno.SuccessCode))
So(nilError.Msg, ShouldEqual, errno.Success.ErrorMsg)

normalError := BuildBaseResp(fmt.Errorf("ok"))
assert.Equal(t, int64(errno.InternalServiceErrorCode), normalError.Code)
assert.Equal(t, "ok", normalError.Msg)
normalError := BuildBaseResp(fmt.Errorf("ok"))
So(normalError.Code, ShouldEqual, int64(errno.InternalServiceErrorCode))
So(normalError.Msg, ShouldEqual, "ok")

errnoError := BuildBaseResp(errno.NewErrNo(200, "ok"))
assert.Equal(t, int64(200), errnoError.Code)
assert.Equal(t, "ok", errnoError.Msg)
errnoError := BuildBaseResp(errno.NewErrNo(200, "ok"))
So(errnoError.Code, ShouldEqual, int64(200))
So(errnoError.Msg, ShouldEqual, "ok")
})
}

func TestBuildSuccessResp(t *testing.T) {
r := BuildSuccessResp()
assert.Equal(t, int64(errno.SuccessCode), r.Code)
assert.Equal(t, errno.Success.ErrorMsg, r.Msg)
Convey("TestBuildSuccessResp", t, func() {
r := BuildSuccessResp()
So(r.Code, ShouldEqual, int64(errno.SuccessCode))
So(r.Msg, ShouldEqual, errno.Success.ErrorMsg)
})
}

func TestLogError(t *testing.T) {
LogError(nil)
LogError(fmt.Errorf("ok"))
LogError(errno.Success)
// LogError(errno.NewErrNoWithStack(200, "ok")) // have tested
}

func TestBuildRespAndLog(t *testing.T) {
nilError := BuildBaseResp(nil)
assert.Equal(t, int64(errno.SuccessCode), nilError.Code)
assert.Equal(t, errno.Success.ErrorMsg, nilError.Msg)
Convey("Test BuildRespAndLog", t, func() {
nilError := BuildBaseResp(nil)
So(nilError.Code, ShouldEqual, int64(errno.SuccessCode))
So(nilError.Msg, ShouldEqual, errno.Success.ErrorMsg)

normalError := BuildBaseResp(fmt.Errorf("ok"))
assert.Equal(t, int64(errno.InternalServiceErrorCode), normalError.Code)
assert.Equal(t, "ok", normalError.Msg)
normalError := BuildBaseResp(fmt.Errorf("ok"))
So(normalError.Code, ShouldEqual, int64(errno.InternalServiceErrorCode))
So("ok", ShouldEqual, normalError.Msg)

errnoError := BuildBaseResp(errno.NewErrNo(200, "ok"))
assert.Equal(t, int64(200), errnoError.Code)
assert.Equal(t, "ok", errnoError.Msg)
errnoError := BuildBaseResp(errno.NewErrNo(200, "ok"))
So(errnoError.Code, ShouldEqual, int64(200))
So(errnoError.Msg, ShouldEqual, "ok")
})
}
20 changes: 2 additions & 18 deletions pkg/constants/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ const (
// LogFilePath 对应 ${pwd}/{LogFilePath}/log.log 相对于当前运行路径而言
LogFilePath = "log"

LevelTrace int64 = iota
LevelDebug
LevelInfo
LevelNotice
LevelWarn
LevelError
LevelFatal
// DefaultLogLevel 是默认的日志等级. Supported Level: debug info warn error fatal
DefaultLogLevel = "INFO"
)

// LevelMap 适用于读取配置后进行日志转换
var LevelMap = map[string]int64{
"TRACE": LevelTrace,
"DEBUG": LevelDebug,
"INFO": LevelInfo,
"NOTICE": LevelNotice,
"WARN": LevelWarn,
"ERROR": LevelError,
"FATAL": LevelFatal,
}
5 changes: 1 addition & 4 deletions pkg/eshook/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package eshook

import (
"github.com/cloudwego/kitex/pkg/klog"
elastic "github.com/elastic/go-elasticsearch"
"go.uber.org/zap"

"github.com/west2-online/fzuhelper-server/config"
"github.com/west2-online/fzuhelper-server/pkg/base/client"
Expand All @@ -39,6 +37,5 @@ func InitLoggerWithHook(index string, esclient *elastic.Client) {
}

hook := NewElasticHook(esclient, config.Elasticsearch.Host, index)
v := logger.DefaultLogger(zap.Hooks(hook.Fire))
klog.SetLogger(v)
logger.AddLoggerHook(hook.Fire)
}
79 changes: 26 additions & 53 deletions pkg/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,47 @@ package logger
import (
"os"

"github.com/cloudwego/kitex/pkg/klog"
kitexzap "github.com/kitex-contrib/obs-opentelemetry/logging/zap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

const (
DefaultSkip = 4 // 默认跳过的栈帧数
)

type Config struct {
Enc zapcore.Encoder
Ws zapcore.WriteSyncer
lvl zapcore.Level
type config struct {
core zapcore.Core
enc zapcore.Encoder
ws zapcore.WriteSyncer
lvl zapcore.Level
}

func InitLoggerWithLevel(lvl zapcore.Level) {
klog.SetLogger(NewLogger(lvl, Config{}))
}
func buildConfig(core zapcore.Core) *config {
cfg := defaultConfig()
cfg.core = core
if cfg.core == nil {
cfg.core = zapcore.NewCore(cfg.enc, cfg.ws, cfg.lvl)
}

func InitLoggerWithConfig(lvl zapcore.Level, cfg Config, options ...zap.Option) {
klog.SetLogger(NewLogger(lvl, cfg, options...))
return cfg
}

func NewLogger(lvl zapcore.Level, cfg Config, options ...zap.Option) *kitexzap.Logger {
if cfg.Enc == nil {
cfg.Enc = defaultEnc()
}
if cfg.Ws == nil {
cfg.Ws = defaultWs()
}
cfg.lvl = lvl

var ops []kitexzap.Option
ops = append(ops, kitexzap.WithZapOptions(defaultOptions()...))
ops = append(ops, kitexzap.WithCoreEnc(cfg.Enc))
ops = append(ops, kitexzap.WithCoreWs(cfg.Ws))
ops = append(ops, kitexzap.WithCoreLevel(zap.NewAtomicLevelAt(cfg.lvl)))
ops = append(ops, kitexzap.WithZapOptions(options...))
return kitexzap.NewLogger(ops...)
func BuildLogger(cfg *config, opts ...zap.Option) *zap.Logger {
return zap.New(cfg.core, opts...)
}

func DefaultLogger(options ...zap.Option) *kitexzap.Logger {
var ops []kitexzap.Option
ops = append(ops, kitexzap.WithZapOptions(defaultOptions()...))
ops = append(ops, kitexzap.WithCoreEnc(defaultEnc()))
ops = append(ops, kitexzap.WithCoreWs(defaultWs()))
ops = append(ops, kitexzap.WithCoreLevel(zap.NewAtomicLevelAt(defaultLvl())))
ops = append(ops, kitexzap.WithZapOptions(options...))
return kitexzap.NewLogger(ops...)
func defaultConfig() *config {
return &config{
enc: defaultEnc(),
ws: defaultWs(),
lvl: defaultLvl(),
}
}

func defaultEnc() zapcore.Encoder {
cfg := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
StacktraceKey: "stacktrace",
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "msg",
// StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder, // 日志等级大写
EncodeTime: zapcore.ISO8601TimeEncoder, // 时间格式
Expand All @@ -96,11 +77,3 @@ func defaultWs() zapcore.WriteSyncer {
func defaultLvl() zapcore.Level {
return zapcore.DebugLevel
}

func defaultOptions() []zap.Option {
return []zap.Option{
zap.AddStacktrace(zap.ErrorLevel),
zap.AddCaller(),
zap.AddCallerSkip(DefaultSkip),
}
}
Loading

0 comments on commit f4c4864

Please sign in to comment.