master
lr 4 months ago
parent 3aa223a279
commit 550dcb5306

@ -1,3 +1,5 @@
app:
mode: "release" # release, debug, test
addr: ":8084"
log:
path: "./logs"

@ -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"}

@ -9,6 +9,8 @@ import (
func defaultConfig() {
viper.SetDefault("app.mode", "debug")
viper.SetDefault("app.addr", ":8080")
viper.SetDefault("app.log.path", "./logs")
}
// ParseConfig 解析配置

@ -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

Loading…
Cancel
Save