feat(gateway): 添加日志记录功能
- 在项目根目录下创建 log 文件夹 - 实现按天生成日志文件的功能 - 配置日志同时输出到控制台和文件 - 更新 .gitignore 文件,忽略 log 目录
This commit is contained in:
parent
6d4f69eb1f
commit
4446e5aeaa
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
node_modules/
|
node_modules/
|
||||||
doc/src/.vuepress/.cache/
|
doc/src/.vuepress/.cache/
|
||||||
doc/src/.vuepress/.temp/
|
doc/src/.vuepress/.temp/
|
||||||
@ -6,4 +5,4 @@ doc/src/.vuepress/dist/
|
|||||||
gateway/static/
|
gateway/static/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*.db
|
*.db
|
||||||
.idea/
|
log/
|
||||||
@ -7,6 +7,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-contrib/sessions/cookie"
|
"github.com/gin-contrib/sessions/cookie"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -34,9 +38,24 @@ type User struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
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.SetFormatter(&logrus.JSONFormatter{})
|
||||||
logger.SetOutput(os.Stdout)
|
// 同时输出到文件和标准输出
|
||||||
|
logger.SetOutput(io.MultiWriter(os.Stdout, logFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user