前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >go : gin 输出日志文件

go : gin 输出日志文件

原创
作者头像
IT工作者
发布2022-07-28 09:39:20
1.7K0
发布2022-07-28 09:39:20
举报
文章被收录于专栏:程序技术知识

我们运行服务端程序,日志直接会打印,本文介绍如何将服务端日志输出到日志文件

代码:

代码语言:javascript
复制
package main

import (
    "io"
    "os"

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

func main() {

    // 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")
}

运行程序之后服务端没有任何信息输出

再看当前路径下。创建出来一个gin.log的文件

查看日志文件内容

[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.SetMode(gin.ReleaseMode)

[GIN-debug] GET /ping --> main.main.func1 (3 handlers)

[GIN-debug] Listening and serving HTTP on :8080

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档