Răsfoiți Sursa

fix:调整相关日志代码,便于后期项目部署

Roc 3 ani în urmă
părinte
comite
2656a2d1b3
3 a modificat fișierele cu 27 adăugiri și 17 ștergeri
  1. 10 6
      config/config.go
  2. 13 2
      core/config.go
  3. 4 9
      core/log.go

+ 10 - 6
config/config.go

@@ -19,12 +19,16 @@ type Serve struct {
 
 // Log 日志配置
 type Log struct {
-	Prefix     string `mapstructure:"prefix" json:"prefix" yaml:"prefix" description:"日志输出前缀"`
-	LogFile    bool   `mapstructure:"log-file" json:"logFile" yaml:"log-file" description:""`
-	Stdout     string `mapstructure:"stdout" json:"stdout" yaml:"stdout" description:""`
-	FileStdout string `mapstructure:"file-stdout" json:"file-stdout" yaml:"file-stdout" description:""`
-	SaveMaxDay int    `mapstructure:"save-max-day" json:"save-max-day" yaml:"save-max-day" description:"最多保留多少天的日志"`
-	CuttingDay int    `mapstructure:"cutting-day" json:"cutting-day" yaml:"cutting-day" description:"相隔几天切割文件"`
+	Prefix         string `mapstructure:"prefix" json:"prefix" yaml:"prefix" description:"日志输出前缀"`
+	LogFile        bool   `mapstructure:"log-file" json:"logFile" yaml:"log-file" description:""`
+	Stdout         string `mapstructure:"stdout" json:"stdout" yaml:"stdout" description:""`
+	FileStdout     string `mapstructure:"file-stdout" json:"file-stdout" yaml:"file-stdout" description:""`
+	SaveMaxDay     int    `mapstructure:"save-max-day" json:"save-max-day" yaml:"save-max-day" description:"最多保留多少天的日志"`
+	CuttingDay     int    `mapstructure:"cutting-day" json:"cutting-day" yaml:"cutting-day" description:"相隔几天切割文件"`
+	LogDirPath     string `mapstructure:"log-dir-path" json:"log-dir-path" yaml:"log-dir-path" description:"日志目录"`
+	LogSoftLink    string `mapstructure:"log-soft-link" json:"log-soft-link" yaml:"log-soft-link" description:"日志软链接"`
+	BinlogDirPath  string `mapstructure:"binlog-dir-path" json:"binlog-dir-path" yaml:"binlog-dir-path" description:"binlog日志目录"`
+	BinlogSoftLink string `mapstructure:"binlog-soft-link" json:"binlog-soft-link" yaml:"binlog-soft-link" description:"binlog日志软链接"`
 }
 
 // Mysql 数据库配置

+ 13 - 2
core/config.go

@@ -5,13 +5,24 @@ import (
 	"github.com/fsnotify/fsnotify"
 	"github.com/spf13/viper"
 	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/utils"
 )
 
-const ConfigFile = "config/config.yaml"
+const ConfigFile = "config/config.yaml"                                //本地(测试)环境下的配置文件地址
+const ProConfigFile = "/home/code/config/hongze_yb/config/config.yaml" //生产环境下的配置文件地址
 
 func init() {
 	v := viper.New()
-	v.SetConfigFile(ConfigFile)
+
+	configFilePath := ConfigFile
+
+	//如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
+	if !utils.FileIsExist(configFilePath) {
+		configFilePath = ProConfigFile
+	}
+
+	//设置配置文件
+	v.SetConfigFile(configFilePath)
 
 	err := v.ReadInConfig()
 	if err != nil {

+ 4 - 9
core/log.go

@@ -15,19 +15,14 @@ import (
 )
 
 const (
-	LogDir      = "log"
-	LogSoftLink = "latest_log"
-	Module      = "hongze_yb"
-
-	//mysql数据库日志
-	BinlogDir      = "binlog"
-	BinlogSoftLink = "latest_binlog"
+	Module = "hongze_yb"
 )
 
 var (
 	defaultFormatter = ` %{time:2006/01/02 - 15:04:05.000} %{longfile} %{color:bold}▶ [%{level:.6s}] %{message}%{color:reset}`
 )
 
+// 初始化日志
 func init() {
 	logConfig := global.CONFIG.Log
 	logger := oplogging.MustGetLogger(Module)
@@ -36,7 +31,7 @@ func init() {
 	registerStdout(logConfig, &backends)
 
 	//注册框架输出(日志文件)
-	fileWriter := registerFile(logConfig, &backends, LogDir, LogSoftLink)
+	fileWriter := registerFile(logConfig, &backends, logConfig.LogDirPath, logConfig.LogSoftLink)
 	if fileWriter != nil {
 		if global.CONFIG.Serve.RunMode == "debug" {
 			gin.DefaultWriter = io.MultiWriter(fileWriter, os.Stdout)
@@ -59,7 +54,7 @@ func initMysqlLog() {
 	registerStdout(logConfig, &backends)
 
 	//注册框架输出(日志文件)
-	fileWriter := registerFile(logConfig, &backends, BinlogDir, BinlogSoftLink)
+	fileWriter := registerFile(logConfig, &backends, logConfig.BinlogDirPath, logConfig.BinlogSoftLink)
 	global.MYSQL_LOG = fileWriter
 }