优化数据库,日志,命令模块
This commit is contained in:
@@ -67,21 +67,25 @@ var (
|
||||
// DefaultLogger 全局默认日志实例(所有模块可直接用,也可注入自定义实现)
|
||||
DefaultLogger Logger
|
||||
// once 保证日志只初始化一次
|
||||
once sync.Once
|
||||
initOnce sync.Once
|
||||
)
|
||||
|
||||
// LogConfig 日志配置结构体(和项目的config包对齐)
|
||||
type LogConfig struct {
|
||||
Verbose bool // 是否开启详细模式(Debug级别)
|
||||
Level string // 日志级别:debug/info/warn/error
|
||||
ShowColor bool // 是否显示彩色输出
|
||||
LogFile string // 日志文件路径(可选,空则只输出到控制台)
|
||||
Level string `mapstructure:"level" yaml:"level"`
|
||||
Format string `mapstructure:"format" yaml:"format"`
|
||||
Output string `mapstructure:"output" yaml:"output"`
|
||||
Verbose bool `mapstructure:"verbose" yaml:"verbose"`
|
||||
LogFile string `mapstructure:"log_file" yaml:"log_file"`
|
||||
ShowColor bool `mapstructure:"show_color" yaml:"show_color"`
|
||||
}
|
||||
|
||||
// 默认配置
|
||||
var defaultConfig = LogConfig{
|
||||
Verbose: false,
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
Verbose: false,
|
||||
ShowColor: true,
|
||||
LogFile: "/var/log/sunhpc/sunhpc.log",
|
||||
}
|
||||
@@ -113,6 +117,9 @@ func (f *CustomFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
colorReset = ""
|
||||
}
|
||||
|
||||
// Debug,打印原始字节,用于调试
|
||||
// fmt.Printf("%q\n", entry.Message)
|
||||
|
||||
// 拼接格式:
|
||||
// 灰色日期 + 空格 + 带颜色的[级别] + 空格 + 日志内容 + 空格 + 灰色文件行号 + 重置
|
||||
fmt.Fprintf(&buf, "%s%s%s %s[%s]%s %s %s%s:%d%s\n",
|
||||
@@ -150,27 +157,6 @@ func getLevelInfo(level logrus.Level) (string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
// getCallerInfo 获取调用日志的文件和行号(跳过logrus内部调用)
|
||||
func _getCallerInfo() (string, int) {
|
||||
// 跳过的调用栈深度:根据实际情况调整(这里跳过logrus和logger包的调用)
|
||||
skip := 6
|
||||
pc, file, line, ok := runtime.Caller(skip)
|
||||
if !ok {
|
||||
return "unknown.go", 0
|
||||
}
|
||||
|
||||
// 只保留文件名(如 db.go),去掉完整路径
|
||||
fileName := filepath.Base(file)
|
||||
|
||||
// 过滤logrus内部调用(可选)
|
||||
funcName := runtime.FuncForPC(pc).Name()
|
||||
if funcName == "" || filepath.Base(funcName) == "logrus" {
|
||||
return getCallerInfoWithSkip(skip + 1)
|
||||
}
|
||||
|
||||
return fileName, line
|
||||
}
|
||||
|
||||
func getCallerInfo() (string, int) {
|
||||
// 从当前调用开始,逐层向上查找
|
||||
for i := 2; i < 15; i++ { // i从2开始(跳过getCallerInfo自身)
|
||||
@@ -203,8 +189,7 @@ func shouldSkipPackage(funcName, file string) bool {
|
||||
}
|
||||
|
||||
// 跳过logger包(你自己的包装包)
|
||||
if strings.Contains(funcName, "your/package/logger") ||
|
||||
strings.Contains(file, "logger") {
|
||||
if strings.Contains(file, "/logger/") {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -227,21 +212,14 @@ func getCallerInfoWithSkip(skip int) (string, int) {
|
||||
|
||||
// Init 初始化全局默认日志实例(全局只执行一次)
|
||||
func Init(cfg LogConfig) {
|
||||
once.Do(func() {
|
||||
// 合并配置:传入的配置为空则用默认值
|
||||
if cfg.Level == "" {
|
||||
cfg.Level = defaultConfig.Level
|
||||
}
|
||||
if cfg.LogFile == "" {
|
||||
cfg.LogFile = defaultConfig.LogFile
|
||||
}
|
||||
|
||||
initOnce.Do(func() {
|
||||
// 1. 创建logrus实例
|
||||
logrusInst := logrus.New()
|
||||
|
||||
// 2. 配置输出(控制台 + 文件,可选)
|
||||
var outputs []io.Writer
|
||||
outputs = append(outputs, os.Stdout) // 控制台输出
|
||||
|
||||
// 如果配置了日志文件,添加文件输出
|
||||
if cfg.LogFile != "" {
|
||||
// 确保日志目录存在
|
||||
|
||||
Reference in New Issue
Block a user