Gin: how to forward all log to a file?

Created on 7 Apr 2015  路  5Comments  路  Source: gin-gonic/gin

does gin-gonic has similar API as martini can easily re-direct log to a file
e.g

   m.Map(log.New(f, "[martini]", log.LstdFlags))    
enhancement

Most helpful comment

you could still use MultiWriter()

myfile, _ := os.Create("server.log")
gin.DefaultLogFile = io.MultiWriter(myfile, os.Stdout)

router := gin.Default()
// ...

maybe DefaultLogFile is not the best name. how about DefaultWriter? any idea?

All 5 comments

How about this? https://github.com/gin-gonic/gin/commit/6c788a43004f9763178738a2c2f7a6d276fd60d5

gin.DefaultLogFile = os.Stdout

router := gin.Default()

the idea: the logger middleware takes the gin.DefaultLogFile as default file.

but you can be even more explicit:

yourfile, _ := os.Create("server.log")

router := gin.New()
router.Use(gin.LoggerWithFile(yourfile))

I would have a look at http://golang.org/pkg/io/#MultiWriter to enable both logging to the console and file, works really well.

you could still use MultiWriter()

myfile, _ := os.Create("server.log")
gin.DefaultLogFile = io.MultiWriter(myfile, os.Stdout)

router := gin.Default()
// ...

maybe DefaultLogFile is not the best name. how about DefaultWriter? any idea?

+1 for DefaultWriter

ok, it's done!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kekemuyu picture kekemuyu  路  3Comments

frederikhors picture frederikhors  路  3Comments

Bloomca picture Bloomca  路  3Comments

mastrolinux picture mastrolinux  路  3Comments

mdnight picture mdnight  路  3Comments