diff --git a/.gitignore b/.gitignore index 0805785..4e4a168 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ - node_modules/ doc/src/.vuepress/.cache/ doc/src/.vuepress/.temp/ @@ -6,4 +5,4 @@ doc/src/.vuepress/dist/ gateway/static/ .DS_Store *.db -.idea/ +log/ \ No newline at end of file diff --git a/gateway/main.go b/gateway/main.go index 1031352..87a0330 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -7,6 +7,10 @@ import ( "strings" "time" + "fmt" + "io" + "path/filepath" + "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" @@ -34,9 +38,24 @@ type User struct { } func init() { + // 确保log目录存在 + if err := os.MkdirAll("log", 0755); err != nil { + panic(fmt.Sprintf("创建日志目录失败: %v", err)) + } + + // 生成日志文件名 (格式: log/2024-03-21.log) + logFileName := filepath.Join("log", time.Now().Format("2006-01-02")+".log") + + // 打开日志文件 + logFile, err := os.OpenFile(logFileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) + if err != nil { + panic(fmt.Sprintf("打开日志文件失败: %v", err)) + } + // 配置日志格式 logger.SetFormatter(&logrus.JSONFormatter{}) - logger.SetOutput(os.Stdout) + // 同时输出到文件和标准输出 + logger.SetOutput(io.MultiWriter(os.Stdout, logFile)) } func main() {