gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Home Page:https://gin-gonic.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does the gin close the log file?

TimurIskandarov opened this issue · comments

How to write log file

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
    // Disable Console Color, you don't need console color when writing the logs to file.
    gin.DisableConsoleColor()

    // Logging to a file.
    f, _ := os.Create("gin.log")
    gin.DefaultWriter = io.MultiWriter(f)

    // Use the following code if you need to write the logs to file and console at the same time.
    // gin.DefaultWriter = io.MultiWriter(f, os.Stdout)

    router := gin.Default()
    router.GET("/ping", func(c *gin.Context) {
        c.String(200, "pong")
    })

    router.Run(":8080")
}

https://gin-gonic.com/docs/examples/write-log/

How does the gin close the log file?

Do you want to turn off the logging stream at runtime?

Your requirement is strange, please describe it in detail.

@RedCrazyGhost when working with files, the construction is usually used

    // Logging to a file.
    f, _ := os.Create("gin.log")
    defer f.Close()
    gin.DefaultWriter = io.MultiWriter(f)

I don't want to disable the logging stream at runtime, I'm just not sure if the file is actually closed when the application exits

I see what you mean, you want to say whether the log stream of gin will be automatically closed when the program is closed.

When the application is closed, memory and handles are freed up, and there is no problem with file hogging.

If you are concerned, check out the official example on how to stop the application properly.

Graceful restart or stop