go : gin + lumberjack 输出日志文件

func init() {
//标准日志库
	logFile, err := os.OpenFile("logs/goserver.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log file failed,err:",err)
		return
	}

	//lumberjack日志滚动记录器
	//lumberjack是一个日志滚动记录器。可以把日志文件根据大小、日期等分割。一般情况下,
	//lumberjack配合其他日志库,实现日志的滚动(rolling)记录。

	log.SetOutput(
		&lumberjack.Logger{
		Filename: "logs/goserver.log",		//日志文件得位置
		MaxSize: 1,						//切割之前日志文件得大小(单位:MB)
		MaxBackups: 3,						//保留旧文件得最大个数
		MaxAge: 5,							//保留旧文件得最大天数
		Compress: true,						//是否压缩旧文件
	})
	//设置标准记录器的输出标志 :完整文件名和行号   微秒分辨率    本地时区中的日期
	log.SetFlags(log.Llongfile | log.Ltime |log.Ldate)
	//同时写文件和屏幕
	multiWriter := io.MultiWriter(os.Stdout, logFile)
	log.SetOutput(multiWriter)

	gin.DefaultWriter = multiWriter
	}
//简单实现
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")
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
goroutine 342 [running]: sync.fatal({0xbdff5b, 0x20}) D:/Program Files (x86)/Go/src/runtime/panic.go:1031 +0x29 sync.(*RWMutex).Unlock(0x19f96d0) D:/Program Files (x86)/Go/src/sync/rwmutex.go:209 +0x57 go-study/models.sendMsg(0x0, {0x135fe0f0, 0x15, 0x15}) D:/go/go-study/models/Message.go:193 +0x70 go-study/models.Chat({0x12fe4500, 0x13bec360}, 0x130d0380) D:/go/go-study/models/Message.go:82 +0x3a0 go-study/service.SendUserMsg(0x13bec360) D:/go/go-study/service/userBasicService.go:237 +0x54 github.com/gin-gonic/gin.(*Context).Next(...) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1(0x13bec360) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/recovery.go:102 +0x89 github.com/gin-gonic/gin.(*Context).Next(...) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 github.com/gin-gonic/gin.LoggerWithConfig.func1(0x13bec360) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/logger.go:240 +0xa7 github.com/gin-gonic/gin.(*Context).Next(...) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174 github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0x13be80e0, 0x13bec360) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:620 +0x51b github.com/gin-gonic/gin.(*Engine).ServeHTTP(0x13be80e0, {0xd04140, 0x13c060a0}, 0x130d0380) D:/Program Files (x86)/Go/bin/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:576 +0x1c9 net/http.serverHandler.ServeHTTP({0x13d46000}, {0xd04140, 0x13c060a0}, 0x130d0380) D:/Program Files (x86)/Go/src/net/http/server.go:2947 +0x285 net/http.(*conn).serve(0x132d8360, {0xd048a0, 0x132f2738}) D:/Program Files (x86)/Go/src/net/http/server.go:1991 +0x67d created by net/http.(*Server).Serve D:/Program Files (x86)/Go/src/net/http/server.go:3102 +0x498
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值