diff --git a/configs.yaml b/configs.yaml index 12c6564..b679878 100644 --- a/configs.yaml +++ b/configs.yaml @@ -1,3 +1,5 @@ app: mode: "release" # release, debug, test addr: ":8084" + log: + path: "./logs" diff --git a/logs/app-2024-07.log b/logs/app-2024-07.log new file mode 100644 index 0000000..521a968 --- /dev/null +++ b/logs/app-2024-07.log @@ -0,0 +1,2 @@ +{"time":"2024-07-27T11:53:52.6893356+08:00","level":"INFO","msg":"server is listening on ","addr":":8084"} +{"time":"2024-07-27T11:58:33.341606+08:00","level":"INFO","msg":"server is listening on ","addr":":8084"} diff --git a/utils/config.go b/utils/config.go index c925dda..dac3f34 100644 --- a/utils/config.go +++ b/utils/config.go @@ -9,6 +9,8 @@ import ( func defaultConfig() { viper.SetDefault("app.mode", "debug") viper.SetDefault("app.addr", ":8080") + viper.SetDefault("app.log.path", "./logs") + } // ParseConfig 解析配置 diff --git a/utils/log.go b/utils/log.go index ea5287b..daedf2f 100644 --- a/utils/log.go +++ b/utils/log.go @@ -1,11 +1,14 @@ package utils import ( + "fmt" "github.com/gin-gonic/gin" + "github.com/spf13/viper" "io" "log" "log/slog" "os" + "time" ) // logger @@ -28,7 +31,9 @@ func setLoggerWriter() { switch gin.Mode() { case gin.ReleaseMode: // 打开文件 - logfile := "./logs/app.log" + month := time.Now().Format("2006-01") + logfile := viper.GetString("app.log.path") + logfile += fmt.Sprintf("/app-%s.log", month) if file, err := os.OpenFile(logfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666); err != nil { log.Println(err) return