首页 > 其他分享 >go fiber: 把异常信息写到错误日志中

go fiber: 把异常信息写到错误日志中

时间:2024-11-16 12:40:59浏览次数:1  
标签:fiber err github error go 日志 com

一,代码:

1,userBusiness.go

package business

import ("fmt")

//得到多个用户,按分页返回
func GetUserList(page int ,pageSize int) (string,error) {

    b := 0
    a:=100/b
    fmt.Println(a)

    return "1,2,3",nil
}

代码中包含有除0错,会引发panic

2,userController.go

它用调用包含除0错的userBusiness

package controller

import (
    "github.com/gofiber/fiber/v2"
    //"time"
    "industry/business"

    "fmt"
)

type UserController struct{}

func NewUserController() *UserController {
	return &UserController{}
}


func (dc *UserController) ListUser(c *fiber.Ctx) error {
	// 处理列出用户的逻辑
    list,_:=business.GetUserList(1,30)
    fmt.Println(list)


	return c.SendString("列出用户")
}

3,recoverMiddleware.go

参考了fiber官方的recover类,添加了写日志功能

package middleware

import (
	"fmt"
	//"os"
	"runtime/debug"
    "time"
	"github.com/gofiber/fiber/v2"
    "industry/config"
)

//在中间件中得到panic,获取堆栈后写入到日志中
func NewRecover(c *fiber.Ctx) (err error) { //nolint:nonamedreturns // Uses recover() to overwrite the error

		// Catch panics
		defer func() {

			if r := recover(); r != nil {
               // fmt.Printf("panic: %v\n%s\n", r.(error), debug.Stack())

                timeStr:=time.Now().Format("2006-01-02 15:04:05")
                //得到时间,ip地址,请求的地址,参数,user-agent,
                fullURL := c.OriginalURL()   //url
                clientIP := c.IP()    //ip地址
                method := c.Method() // 获取请求方法
                userAgent := c.Get("User-Agent")   //user agent
                // 获取所有查询参数
                queryParams := fmt.Sprintf("%v",c.Queries())
                fmt.Println("Query Params:", queryParams)

    filename:="/data/logs/gologs/exception-" + time.Now().Format("2006-01-02") + ".log"
    content:="["+timeStr+"] "+r.(error).Error()+"\n"+clientIP+" "+method+" "+fullURL+" "+userAgent+"\n"+queryParams+"\n"+string(debug.Stack())+"\n"
    config.GlobalWriteFile(filename,content)

				var ok bool
				if err, ok = r.(error); !ok {
					// Set error that will call the global error handler
					err = fmt.Errorf("%v", r)
				}

			}
		}()

		// Return err if exist, else move to next handler
		return c.Next()
}

 

4,routes.go

package routes

import (
	"github.com/gofiber/fiber/v2"
	"industry/controller"
    "runtime/debug"
    "industry/middleware"
    "fmt"
)



func SetupRoutes() *fiber.App {

    app := fiber.New(fiber.Config{
		ErrorHandler: func(c *fiber.Ctx, err error) error {

			// 发送自定义错误页面
			//return c.Status(code).JSON(config.Error("内部错误:"+err.Error()))
            return c.SendString("内部错误:"+err.Error())
		},
	})



    app.Use(middleware.NewRecover)

	//用户模块
	userController := controller.NewUserController()
	user := app.Group("/user")
	user.Get("/info", userController.GetUser)
	user.Post("/", userController.CreateUser)
    user.Get("/list", userController.ListUser)

    return app
}

5,写日志的类:

package config

import (
    "os"
    "fmt"
)

// filename:文件路径,content:要写的内容
func GlobalWriteFile(filename,content string) error {
        // 输出到文件
		file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			fmt.Println("日志文件的打开错误 : " + err.Error())
			return err
		}
		defer file.Close()
		if _, err := file.WriteString(content); err != nil {
			fmt.Println("写入日志文件错误 : " + err.Error())
            return err
		}

		return nil
}

 

二,测试效果:

[2024-11-14 17:56:51] runtime error: integer divide by zero
192.168.219.1 POST /user/list?token=111&a=22&c=33 PostmanRuntime/7.42.0
map[a:22 c:33 token:111]
goroutine 21 [running]:
runtime/debug.Stack()
        /usr/local/soft/go/src/runtime/debug/stack.go:26 +0x5e
industry/middleware.NewRecover.func1()
        /data/goapp/industry/middleware/recoverMiddleware.go:37 +0x29a
panic({0x6f9360?, 0xa08960?})
        /usr/local/soft/go/src/runtime/panic.go:785 +0x132
industry/business.GetUserList(0x7f008002000000?, 0xc00005ba80?)
        /data/goapp/industry/business/userBusiness.go:9 +0x9
industry/controller.(*UserController).ListUser(0x467edd?, 0xc00014e008)
        /data/goapp/industry/controller/userController.go:29 +0x26
github.com/gofiber/fiber/v2.(*App).next(0xc0000e4a08, 0xc00014e008)
        /data/gopath/pkg/mod/github.com/gofiber/fiber/[email protected]/router.go:145 +0x1be
github.com/gofiber/fiber/v2.(*Ctx).Next(0x10?)
        /data/gopath/pkg/mod/github.com/gofiber/fiber/[email protected]/ctx.go:1034 +0x4d
industry/middleware.NewRecover(0x6f65e0?)
        /data/goapp/industry/middleware/recoverMiddleware.go:50 +0x53
github.com/gofiber/fiber/v2.(*App).next(0xc0000e4a08, 0xc00014e008)
        /data/gopath/pkg/mod/github.com/gofiber/fiber/[email protected]/router.go:145 +0x1be
github.com/gofiber/fiber/v2.(*App).handler(0xc0000e4a08, 0x4adc0f?)
        /data/gopath/pkg/mod/github.com/gofiber/fiber/[email protected]/router.go:172 +0x69
github.com/valyala/fasthttp.(*Server).serveConn(0xc0000ec248, {0x7d3198, 0xc0000ac1e0})
        /data/gopath/pkg/mod/github.com/valyala/[email protected]/server.go:2385 +0xed1
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc00011a090, 0xc0000e63a0)
        /data/gopath/pkg/mod/github.com/valyala/[email protected]/workerpool.go:225 +0x92
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
        /data/gopath/pkg/mod/github.com/valyala/[email protected]/workerpool.go:197 +0x32
created by github.com/valyala/fasthttp.(*workerPool).getCh in goroutine 1
        /data/gopath/pkg/mod/github.com/valyala/[email protected]/workerpool.go:196 +0x194

 

标签:fiber,err,github,error,go,日志,com
From: https://www.cnblogs.com/architectforest/p/18546745

相关文章

  • golang: 在线上用nginx部署应用
    一,启动应用:1,编译程序$gobuild2,用nohup启动应用的二进制程序$nohup/data/goapp/industry/industry>>/data/logs/gologs/back.log2>&1&[1]48963,检查应用是否启动:$ss-lntp|grep3000LISTEN040960.0.0.0:30000.0.0.0:*users:(("......
  • go fiber: 增加访问日志accesslog
    一,代码这里我们使用官方提供的github.com/gofiber/fiber/v2/middleware/logger这个现成的中间件官方文档地址:https://docs.gofiber.io/api/middleware/logger/routes.gopackageroutesimport( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/......
  • go fiber: 多个全局中间件的执行顺序
    一,代码:middleware.gopackagemiddlewareimport( "fmt" "github.com/gofiber/fiber/v2")funcMiddle1(c*fiber.Ctx)error{ fmt.Println("middle1before") err:=c.Next() fmt.Println("middle1after") returnerr}......
  • go fiber:路由中间件
    一,目录结构:二,代码1,中间件代码packagemiddlewareimport( "fmt" "github.com/gofiber/fiber/v2" "industry/config")//token校验funcCheckUser(c*fiber.Ctx)error{ token:=c.Query("token") fmt.Println("token:"......
  • django naive datetime问题
    naivedatetime问题,其实就是datetime数据没有携带时间就传递给了模型类现象:D:\anaconda3\envs\schedule_devops\lib\site-packages\django\db\models\fields_init_.py:1367:RuntimeWarning:DateTimeFieldNodeStopRecord.stop_timereceivedanaivedatetime(2024-11-1602:......
  • Let'sGoFurther - Chapter 6: SQL Migrations
      InstallingthemigratetoolTomanageSQLmigrationsinthisprojectwe’regoingtousethemigratecommand-line tool(whichitselfiswritteninGo).OnLinuxandWindows,theeasiestmethodistodownloadapre-builtbinaryandmove ittoalocat......
  • 通过MongoDB Atlas 实现语义搜索与 RAG——迈向AI的搜索机制
    目录通过MongoDBAtlas实现语义搜索与RAG——迈向AI的搜索机制一、引言二、语义搜索与MongoDBAtlas的背景三、MongoDBAtlas的向量搜索功能1.向量搜索的实现方式2.典型操作示例四、RAG在MongoDBAtlas的应用1、RAG是什么2、RAG的实现过程3、RAG的实际应......
  • Hyperf 微服务与 MongoDB 日志记录
    以下是关于在Hyperf微服务中使用MongoDB记录用户操作日志的相关技术详解: 一、Hyperf框架简介Hyperf是基于Swoole协程的高性能PHP微服务框架,它提供了诸多便捷的功能和组件,方便开发者快速构建高效、稳定的微服务应用。在微服务架构中,日志记录是非常重要的一环,有助于排查问题......
  • django 数据库ORM通用的公共函数
    通用查询1、公共函数:defgeneric_query(model,filter_kwargs=None,order_by=None,limit=None,aggregate=None,annotate=None):"""通用的DjangoORM查询函数。:parammodel:Django模型类:paramfilter_kwargs:过滤条件字典:paramorder_by:......
  • (mongodb副本集) PSA模式添加、修改节点
    (mongodb副本集)PSA模式添加、修改节点PSA模式介绍PSA模式(Primary-Secondary-Arbiter)是MongoDB复制集中的一种架构配置。在这种模式下,复制集由一个主节点(PRIMARY)、一个从节点(SECONDARY)和一个仲裁者节点(ARBITER)组成。在MongoDB的复制集中,仲裁者节点(Arbiter)是一种特殊类型的节点,......