Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does the gin close the log file? #3849

Open
TimurIskandarov opened this issue Feb 19, 2024 · 3 comments
Open

How does the gin close the log file? #3849

TimurIskandarov opened this issue Feb 19, 2024 · 3 comments

Comments

@TimurIskandarov
Copy link

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?

@RedCrazyGhost
Copy link
Contributor

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

Your requirement is strange, please describe it in detail.

@TimurIskandarov
Copy link
Author

TimurIskandarov commented Mar 1, 2024

@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

@RedCrazyGhost
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants