Compare commits

..

8 Commits

Author SHA1 Message Date
yanweidong 524e310dfe fix logger 2025-10-07 17:31:14 +08:00
yanweidong 409cb53e8c fix bug 2025-10-05 15:05:50 +08:00
yanweidong 404957f16a fix bug 2025-10-05 15:00:07 +08:00
yanweidong 3d6871138a fix bug 2025-10-05 14:59:01 +08:00
yanweidong f7d8988415 add AppendMigrate 2025-10-05 14:57:17 +08:00
yanweidong 820d7a5c63 fix errcode 2025-10-04 20:21:45 +08:00
yanweidong 3038c6c22c fix logger bug 2025-10-04 18:22:19 +08:00
yanweidong 5584757ff4 fix logger bug 2025-10-04 17:42:26 +08:00
7 changed files with 90 additions and 30 deletions

View File

@ -104,3 +104,19 @@ func CheckIP(ip string) string {
}
return ip
}
// 初始化Logger配置
func InitLoggerConf(cfg *LogConf) *LogConf {
if cfg == nil {
return &LogConf{
Name: strings.ToLower(vars.ServiceKey),
Level: vars.LogLevel(vars.DEBUG),
Dir: "./logs/",
Endpoint: "",
Console: true,
File: true,
Remote: false,
}
}
return cfg
}

View File

@ -1,5 +1,7 @@
package conf
import "git.apinb.com/bsm-sdk/core/vars"
type Base struct {
Service string `yaml:"Service"` // 服务名称
Port string `yaml:"Port"` // 服务监听端口,0为自动随机端口
@ -82,23 +84,12 @@ type WebSocketConf struct {
HandshakeTimeout string `yaml:"HandshakeTimeout"`
}
// LogLevel 日志级别
type LogLevel int
const (
DEBUG LogLevel = iota
INFO
WARN
ERROR
FATAL
)
type LogConf struct {
Name string `yaml:"Name"`
Level LogLevel `yaml:"Level"`
Dir string `yaml:"Dir"`
Endpoint string `yaml:"Endpoint"`
Console bool `yaml:"Console"`
File bool `yaml:"File"`
Remote bool `yaml:"Remote"`
Name string `yaml:"Name"`
Level vars.LogLevel `yaml:"Level"`
Dir string `yaml:"Dir"`
Endpoint string `yaml:"Endpoint"`
Console bool `yaml:"Console"`
File bool `yaml:"File"`
Remote bool `yaml:"Remote"`
}

View File

@ -133,3 +133,13 @@ func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err
return gormDb, nil
}
// AppendMigrate 调用此函数后,会在数据库初始化时自动迁移表结构
//
// - table: 需要自动迁移的表
func AppendMigrate(table any) {
if MigrateTables == nil {
MigrateTables = make([]any, 0)
}
MigrateTables = append(MigrateTables, table)
}

View File

@ -11,6 +11,7 @@ import (
// HTTP请求头相关错误码起始码:1000
var (
AllErrors = make(map[int]string)
ErrHeaderRequestId = NewError(1001, "Header Request-Id Not Found") // 请求ID头缺失
ErrHeaderAuthorization = NewError(1002, "Header Authorization Not Found") // 授权头缺失
ErrHeaderSecretKey = NewError(1003, "Header Secret-Key Not Found") // 密钥头缺失
@ -81,6 +82,7 @@ var (
// code: 错误码
// msg: 错误消息
func NewError(code int, msg string) error {
AllErrors[code] = msg
return status.New(codes.Code(code), msg).Err()
}
@ -88,6 +90,7 @@ func NewError(code int, msg string) error {
// code: 错误码
// msg: 错误消息
func ErrFatal(code int, msg string) error {
AllErrors[code] = msg
return status.New(codes.Code(code), msg).Err()
}
@ -95,5 +98,6 @@ func ErrFatal(code int, msg string) error {
// code: 错误码
// msg: 错误消息,会自动转换为大写
func ErrNotFound(code int, msg string) error {
AllErrors[code] = strings.ToUpper(msg)
return status.New(codes.Code(code), strings.ToUpper(msg)).Err()
}

View File

@ -13,11 +13,12 @@ import (
"git.apinb.com/bsm-sdk/core/conf"
"git.apinb.com/bsm-sdk/core/utils"
"git.apinb.com/bsm-sdk/core/vars"
)
// Logger 日志器结构
type Logger struct {
level conf.LogLevel
level vars.LogLevel
infoLogger *log.Logger
warnLogger *log.Logger
errorLogger *log.Logger
@ -69,7 +70,7 @@ func NewLogger(cfg *conf.LogConf) (*Logger, error) {
multiWriter := io.MultiWriter(consoleWriter, fileWriter)
logger := &Logger{
level: conf.LogLevel(cfg.Level),
level: cfg.Level,
fileWriter: fileWriter,
consoleWriter: consoleWriter,
logDir: cfg.Dir,
@ -152,7 +153,7 @@ func (l *Logger) sendToRemote(level, name, out string) {
// Debug 输出调试信息
func (l *Logger) Debug(v ...interface{}) {
if l.level <= conf.DEBUG {
if l.level <= vars.DEBUG {
l.checkAndRotateLog()
out := fmt.Sprint(v...)
if l.onRemote {
@ -164,7 +165,7 @@ func (l *Logger) Debug(v ...interface{}) {
// Debugf 格式化输出调试信息
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.level <= conf.DEBUG {
if l.level <= vars.DEBUG {
l.checkAndRotateLog()
out := fmt.Sprintf(format, v...)
if l.onRemote {
@ -176,7 +177,7 @@ func (l *Logger) Debugf(format string, v ...interface{}) {
// Info 输出信息
func (l *Logger) Info(v ...interface{}) {
if l.level <= conf.INFO {
if l.level <= vars.INFO {
l.checkAndRotateLog()
out := fmt.Sprint(v...)
if l.onRemote {
@ -188,7 +189,7 @@ func (l *Logger) Info(v ...interface{}) {
// Infof 格式化输出信息
func (l *Logger) Infof(format string, v ...interface{}) {
if l.level <= conf.INFO {
if l.level <= vars.INFO {
l.checkAndRotateLog()
out := fmt.Sprintf(format, v...)
if l.onRemote {
@ -200,7 +201,7 @@ func (l *Logger) Infof(format string, v ...interface{}) {
// Warn 输出警告
func (l *Logger) Warn(v ...interface{}) {
if l.level <= conf.WARN {
if l.level <= vars.WARN {
l.checkAndRotateLog()
out := fmt.Sprint(v...)
if l.onRemote {
@ -212,7 +213,7 @@ func (l *Logger) Warn(v ...interface{}) {
// Warnf 格式化输出警告
func (l *Logger) Warnf(format string, v ...interface{}) {
if l.level <= conf.WARN {
if l.level <= vars.WARN {
l.checkAndRotateLog()
out := fmt.Sprintf(format, v...)
if l.onRemote {
@ -224,7 +225,7 @@ func (l *Logger) Warnf(format string, v ...interface{}) {
// Error 输出错误
func (l *Logger) Error(v ...interface{}) {
if l.level <= conf.ERROR {
if l.level <= vars.ERROR {
l.checkAndRotateLog()
out := fmt.Sprint(v...)
if l.onRemote {
@ -236,7 +237,7 @@ func (l *Logger) Error(v ...interface{}) {
// Errorf 格式化输出错误
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.level <= conf.ERROR {
if l.level <= vars.ERROR {
l.checkAndRotateLog()
out := fmt.Sprintf(format, v...)
if l.onRemote {
@ -284,14 +285,14 @@ func (l *Logger) Println(v ...interface{}) {
}
// SetLevel 设置日志级别
func (l *Logger) SetLevel(level conf.LogLevel) {
func (l *Logger) SetLevel(level vars.LogLevel) {
l.mu.Lock()
defer l.mu.Unlock()
l.level = level
}
// GetLevel 获取日志级别
func (l *Logger) GetLevel() conf.LogLevel {
func (l *Logger) GetLevel() vars.LogLevel {
l.mu.RLock()
defer l.mu.RUnlock()
return l.level

12
vars/logger.go Normal file
View File

@ -0,0 +1,12 @@
package vars
// LogLevel 日志级别
type LogLevel int
const (
DEBUG LogLevel = iota
INFO
WARN
ERROR
FATAL
)

26
with/logger.go Normal file
View File

@ -0,0 +1,26 @@
package with
import (
"strings"
"git.apinb.com/bsm-sdk/core/conf"
"git.apinb.com/bsm-sdk/core/logger"
"git.apinb.com/bsm-sdk/core/vars"
)
// 初始化Logger配置
func Logger(cfg *conf.LogConf) {
if cfg == nil {
cfg = &conf.LogConf{
Name: strings.ToLower(vars.ServiceKey),
Level: vars.LogLevel(vars.DEBUG),
Dir: "./logs/",
Endpoint: "",
Console: true,
File: true,
Remote: false,
}
}
logger.InitLogger(cfg)
}