Skip to content

Commit

Permalink
feat: output logs to file and update them periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
mutezebra committed Dec 17, 2024
1 parent b283e90 commit da38444
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 185 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
}
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
52 changes: 30 additions & 22 deletions pkg/base/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +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,
}
6 changes: 3 additions & 3 deletions pkg/errno/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var (
Success = NewErrNo(SuccessCode, "ok")
CustomLaunchScreenSuccess = NewErrNo(consts.StatusOK, "ok") // 兼容处理

AuthError = NewErrNo(AuthErrorCode, "鉴权失败") // 鉴权失败,通常是内部错误,如解析失败
AuthInvalid = NewErrNo(AuthInvalidCode, "鉴权无效") // 鉴权无效,如令牌颁发者不是 west2-online
AuthError = NewErrNo(AuthErrorCode, "鉴权失败") // 鉴权失败,通常是内部错误,如解析失败
AuthInvalid = NewErrNo(AuthInvalidCode, "鉴权无效") // 鉴权无效,如令牌颁发者不是 west2-online
AuthAccessExpired = NewErrNo(AuthAccessExpiredCode, "访问令牌过期") // 访问令牌过期
AuthRefreshExpired = NewErrNo(AuthRefreshExpiredCode, "刷新令牌过期") // 刷新令牌过期
AuthMissing = NewErrNo(AuthMissingCode, "鉴权缺失") // 鉴权缺失,如访问令牌缺失
AuthMissing = NewErrNo(AuthMissingCode, "鉴权缺失") // 鉴权缺失,如访问令牌缺失

ParamError = NewErrNo(ParamErrorCode, "参数错误") // 参数校验失败,可能是参数为空、参数类型错误等
ParamMissingHeader = NewErrNo(ParamMissingHeaderCode, "缺失合法学生请求头数据")
Expand Down
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),
}
}
120 changes: 120 additions & 0 deletions pkg/logger/klog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package logger

import (
"context"
"io"

"github.com/cloudwego/kitex/pkg/klog"
)

type KlogLogger struct{}

func GetKlogLogger() *KlogLogger {
return &KlogLogger{}
}

func (l *KlogLogger) Trace(v ...interface{}) {
Debug(v...)
}

func (l *KlogLogger) Debug(v ...interface{}) {
Debug(v...)
}

func (l *KlogLogger) Info(v ...interface{}) {
Info(v...)
}

func (l *KlogLogger) Notice(v ...interface{}) {
Info(v...)
}

func (l *KlogLogger) Warn(v ...interface{}) {
Warn(v...)
}

func (l *KlogLogger) Error(v ...interface{}) {
Error(v...)
}

func (l *KlogLogger) Fatal(v ...interface{}) {
Fatal(v...)
}

func (l *KlogLogger) Tracef(format string, v ...interface{}) {
Debugf(format, v...)
}

func (l *KlogLogger) Debugf(format string, v ...interface{}) {
Debugf(format, v...)
}

func (l *KlogLogger) Infof(format string, v ...interface{}) {
Infof(format, v...)
}

func (l *KlogLogger) Noticef(format string, v ...interface{}) {
Infof(format, v...)
}

func (l *KlogLogger) Warnf(format string, v ...interface{}) {
Warnf(format, v...)
}

func (l *KlogLogger) Errorf(format string, v ...interface{}) {
Errorf(format, v...)
}

func (l *KlogLogger) Fatalf(format string, v ...interface{}) {
Fatalf(format, v...)
}

func (l *KlogLogger) CtxTracef(ctx context.Context, format string, v ...interface{}) {
Debugf(format, v...)
}

func (l *KlogLogger) CtxDebugf(ctx context.Context, format string, v ...interface{}) {
Debugf(format, v...)
}

func (l *KlogLogger) CtxInfof(ctx context.Context, format string, v ...interface{}) {
Infof(format, v...)
}

func (l *KlogLogger) CtxNoticef(ctx context.Context, format string, v ...interface{}) {
Infof(format, v...)
}

func (l *KlogLogger) CtxWarnf(ctx context.Context, format string, v ...interface{}) {
Warnf(format, v...)
}

func (l *KlogLogger) CtxErrorf(ctx context.Context, format string, v ...interface{}) {
Errorf(format, v...)
}

func (l *KlogLogger) CtxFatalf(ctx context.Context, format string, v ...interface{}) {
Fatalf(format, v...)
}

func (l *KlogLogger) SetLevel(klog.Level) {
}

func (l *KlogLogger) SetOutput(io.Writer) {
}
Loading

0 comments on commit da38444

Please sign in to comment.