英文:
How to save log message to file (labstack/echo)
问题
我需要帮助..
我正在使用golang,目前我想将我的日志保存到日志文件中。我使用了echo框架进行日志记录,像这样:
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
有人知道如何将其保存到文件中吗?
英文:
I need help..
I am using golang, and currently I want to save my log into logger file. I used echo framework for logging, like this:
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))
Does anyone knows How to save it into file?
答案1
得分: 1
LoggerConfig
的Output
字段是一个io.Writer
类型。可以将任何实现了io.Writer
接口的对象赋值给Output
字段。
f, err := os.OpenFile("logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("error opening file: %v", err))
}
defer f.Close()
// 中间件
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
Output: f,
}))
英文:
Output
field of LoggerConfig
is io.Writer. Set to Output
field any io.Writer implementation
f, err := os.OpenFile("logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(fmt.Sprintf("error opening file: %v", err))
}
defer f.Close()
// Middleware
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
Output: f,
}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论