From 4446e5aeaaabe5888265b821785fef68111525df Mon Sep 17 00:00:00 2001 From: jdysya <1912377458@qq.com> Date: Sat, 15 Feb 2025 19:35:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(gateway):=20=E6=B7=BB=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在项目根目录下创建 log 文件夹 - 实现按天生成日志文件的功能 - 配置日志同时输出到控制台和文件 - 更新 .gitignore 文件,忽略 log 目录 --- .gitignore | 3 +-- gateway/main.go | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) 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() {