Compare commits
No commits in common. "main" and "v0.0.89" have entirely different histories.
16
conf/new.go
16
conf/new.go
|
@ -104,19 +104,3 @@ 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
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package conf
|
||||
|
||||
import "git.apinb.com/bsm-sdk/core/vars"
|
||||
|
||||
type Base struct {
|
||||
Service string `yaml:"Service"` // 服务名称
|
||||
Port string `yaml:"Port"` // 服务监听端口,0为自动随机端口
|
||||
|
@ -84,12 +82,23 @@ 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 vars.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 LogLevel `yaml:"Level"`
|
||||
Dir string `yaml:"Dir"`
|
||||
Endpoint string `yaml:"Endpoint"`
|
||||
Console bool `yaml:"Console"`
|
||||
File bool `yaml:"File"`
|
||||
Remote bool `yaml:"Remote"`
|
||||
}
|
||||
|
|
|
@ -133,13 +133,3 @@ 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)
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ 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") // 密钥头缺失
|
||||
|
@ -82,7 +81,6 @@ var (
|
|||
// code: 错误码
|
||||
// msg: 错误消息
|
||||
func NewError(code int, msg string) error {
|
||||
AllErrors[code] = msg
|
||||
return status.New(codes.Code(code), msg).Err()
|
||||
}
|
||||
|
||||
|
@ -90,7 +88,6 @@ 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()
|
||||
}
|
||||
|
||||
|
@ -98,6 +95,5 @@ 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()
|
||||
}
|
||||
|
|
|
@ -13,12 +13,11 @@ 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 vars.LogLevel
|
||||
level conf.LogLevel
|
||||
infoLogger *log.Logger
|
||||
warnLogger *log.Logger
|
||||
errorLogger *log.Logger
|
||||
|
@ -70,7 +69,7 @@ func NewLogger(cfg *conf.LogConf) (*Logger, error) {
|
|||
multiWriter := io.MultiWriter(consoleWriter, fileWriter)
|
||||
|
||||
logger := &Logger{
|
||||
level: cfg.Level,
|
||||
level: conf.LogLevel(cfg.Level),
|
||||
fileWriter: fileWriter,
|
||||
consoleWriter: consoleWriter,
|
||||
logDir: cfg.Dir,
|
||||
|
@ -153,7 +152,7 @@ func (l *Logger) sendToRemote(level, name, out string) {
|
|||
|
||||
// Debug 输出调试信息
|
||||
func (l *Logger) Debug(v ...interface{}) {
|
||||
if l.level <= vars.DEBUG {
|
||||
if l.level <= conf.DEBUG {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
|
@ -165,7 +164,7 @@ func (l *Logger) Debug(v ...interface{}) {
|
|||
|
||||
// Debugf 格式化输出调试信息
|
||||
func (l *Logger) Debugf(format string, v ...interface{}) {
|
||||
if l.level <= vars.DEBUG {
|
||||
if l.level <= conf.DEBUG {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
|
@ -177,7 +176,7 @@ func (l *Logger) Debugf(format string, v ...interface{}) {
|
|||
|
||||
// Info 输出信息
|
||||
func (l *Logger) Info(v ...interface{}) {
|
||||
if l.level <= vars.INFO {
|
||||
if l.level <= conf.INFO {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
|
@ -189,7 +188,7 @@ func (l *Logger) Info(v ...interface{}) {
|
|||
|
||||
// Infof 格式化输出信息
|
||||
func (l *Logger) Infof(format string, v ...interface{}) {
|
||||
if l.level <= vars.INFO {
|
||||
if l.level <= conf.INFO {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
|
@ -201,7 +200,7 @@ func (l *Logger) Infof(format string, v ...interface{}) {
|
|||
|
||||
// Warn 输出警告
|
||||
func (l *Logger) Warn(v ...interface{}) {
|
||||
if l.level <= vars.WARN {
|
||||
if l.level <= conf.WARN {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
|
@ -213,7 +212,7 @@ func (l *Logger) Warn(v ...interface{}) {
|
|||
|
||||
// Warnf 格式化输出警告
|
||||
func (l *Logger) Warnf(format string, v ...interface{}) {
|
||||
if l.level <= vars.WARN {
|
||||
if l.level <= conf.WARN {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
|
@ -225,7 +224,7 @@ func (l *Logger) Warnf(format string, v ...interface{}) {
|
|||
|
||||
// Error 输出错误
|
||||
func (l *Logger) Error(v ...interface{}) {
|
||||
if l.level <= vars.ERROR {
|
||||
if l.level <= conf.ERROR {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprint(v...)
|
||||
if l.onRemote {
|
||||
|
@ -237,7 +236,7 @@ func (l *Logger) Error(v ...interface{}) {
|
|||
|
||||
// Errorf 格式化输出错误
|
||||
func (l *Logger) Errorf(format string, v ...interface{}) {
|
||||
if l.level <= vars.ERROR {
|
||||
if l.level <= conf.ERROR {
|
||||
l.checkAndRotateLog()
|
||||
out := fmt.Sprintf(format, v...)
|
||||
if l.onRemote {
|
||||
|
@ -285,14 +284,14 @@ func (l *Logger) Println(v ...interface{}) {
|
|||
}
|
||||
|
||||
// SetLevel 设置日志级别
|
||||
func (l *Logger) SetLevel(level vars.LogLevel) {
|
||||
func (l *Logger) SetLevel(level conf.LogLevel) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
l.level = level
|
||||
}
|
||||
|
||||
// GetLevel 获取日志级别
|
||||
func (l *Logger) GetLevel() vars.LogLevel {
|
||||
func (l *Logger) GetLevel() conf.LogLevel {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
return l.level
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
package vars
|
||||
|
||||
// LogLevel 日志级别
|
||||
type LogLevel int
|
||||
|
||||
const (
|
||||
DEBUG LogLevel = iota
|
||||
INFO
|
||||
WARN
|
||||
ERROR
|
||||
FATAL
|
||||
)
|
|
@ -1,26 +0,0 @@
|
|||
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)
|
||||
}
|
Loading…
Reference in New Issue