SoFunction
Updated on 2025-03-05

go:gin output log file method

background

We run the server program and the logs will be printed directly. This article introduces how to output the server logs to the log file

Code

package main

import (
	"io"
	"os"

	"/gin-gonic/gin"
)

func main() {

	// Logging to a file.
	f, _ := ("")
	 = (f)

	// Use the following code if you need to write the logs to file and console at the same time.
	//  = (f, )

	router := ()
	("/ping", func(c *) {
		(200, "pong")
	})

	(":8080")
}

After running the program, the server does not have any information output

Look at the current path.

Create a file

View log file content

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:    export GIN_MODE=release
 - using code:    ()

[GIN-debug] GET    /ping                     --> .func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2019/12/06 - 09:49:58 |[97;42m 200 [0m|      47.188µs |             ::1 |[97;44m GET     [0m /ping

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.