2

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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
TeaCup
  • 31
  • 2
  • 2
    `LoggerConfig` has an `Output` field which defaults to `os.Stdout`. You can put any reader there, including the one returned by `os.Openfile`/`os.Create`. You can also modify `DefaultLoggerConfig` to apply the change to all logger instances. – oakad Nov 03 '21 at 01:59

1 Answers1

1

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,
}))
kozmo
  • 4,024
  • 3
  • 30
  • 48