golang zap日志模块封装sentry

ops/2024/10/21 20:42:20/

我们自己写个log日志包,把zapsentry封装到一起。
下面直接贴上主要部分代码(两个模块初始化部分的代码请自行查阅官方文档):

  • logger.go
package logimport ("github.com/getsentry/sentry-go""go.uber.org/zap"
)type Logger struct {*zap.Logger
}// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Debug(msg string, fields ...zap.Field) {log.Logger.Debug(msg, fields...)NewMessage(sentry.LevelDebug, msg, fields)
}// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Info(msg string, fields ...zap.Field) {log.Logger.Info(msg, fields...)NewMessage(sentry.LevelInfo, msg, fields)
}// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Warn(msg string, fields ...zap.Field) {log.Logger.Warn(msg, fields...)NewMessage(sentry.LevelWarning, msg, fields)
}// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Error(msg string, fields ...zap.Field) {log.Logger.Error(msg, fields...)NewMessage(sentry.LevelError, msg, fields)
}// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func (log *Logger) Fatal(msg string, fields ...zap.Field) {log.Logger.Fatal(msg, fields...)NewMessage(sentry.LevelFatal, msg, fields)
}
package logimport ("fmt""utils/stime""github.com/getsentry/sentry-go""go.uber.org/zap""go.uber.org/zap/zapcore""math""runtime""time"
)type CustomError struct {msg        stringstackTrace []uintptr
}func (e *CustomError) Error() string {return e.msg
}
func (e *CustomError) StackTrace() []uintptr {return e.stackTrace
}func NewMessage(level sentry.Level, msg string, fields []zap.Field) {if sentryUsable {sentry.WithScope(func(scope *sentry.Scope) {scope.SetLevel(level)scope.SetExtra("msg", msg)if len(fields) > 0 {for _, field := range fields {switch field.Type {case zapcore.StringType:scope.SetExtra(field.Key, field.String)case zapcore.ErrorType:scope.SetExtra(field.Key, field.Interface.(error).Error())case zapcore.StringerType:scope.SetExtra(field.Key, field.Interface.(fmt.Stringer).String())case zapcore.Int8Type, zapcore.Int16Type, zapcore.Int32Type, zapcore.Int64Type, zapcore.Uint8Type, zapcore.Uint16Type, zapcore.Uint32Type, zapcore.Uint64Type:scope.SetExtra(field.Key, field.Integer)case zapcore.Float32Type, zapcore.Float64Type:floatVal := math.Float32frombits(uint32(field.Integer))scope.SetExtra(field.Key, floatVal)case zapcore.BoolType:scope.SetExtra(field.Key, field.Integer == 1)case zapcore.TimeFullType:if ts, ok := field.Interface.(time.Time); ok {scope.SetExtra(field.Key, ts.Format(stime.Format_Normal_YMDhms))}case zapcore.TimeType:scope.SetExtra(field.Key, time.Unix(0, field.Integer).UTC())default:scope.SetExtra(field.Key, fmt.Sprintf("%+v", field.Interface))}}}scope.SetFingerprint([]string{msg})if level == sentry.LevelError {stackTrace := make([]uintptr, 20)runtime.Callers(6, stackTrace)err := &CustomError{msg:        msg,stackTrace: stackTrace,}sentry.CaptureException(err)} else {sentry.CaptureMessage(msg)}})}
}

效果展示:
错误消息会展示错误类型,其它的debug、info等会直接展示消息名称
在这里插入图片描述

错误消息详情:

  • 详情中包含错误位置和调用方法,我就不单独写调用示例了。

在这里插入图片描述

  • 消息详情中可已看到zap.Field中携带的消息内容
    在这里插入图片描述

http://www.ppmy.cn/ops/103562.html

相关文章

python-矩阵交换行

[题目描述] 给定一个 55 的矩阵(数学上,一个 rc 的矩阵是一个由 r 行 c 列元素排列成的矩形阵列),将第 n 行和第 m 行交换,输出交换后的结果。输入: 输入共 6 行,前 5 行为矩阵的每一行元素,元素与元素之间…

使用python导出Excel表格中的lua配置

背景:游戏开发中, 策划使用Excel配置游戏中的参数数据,写一个工具用于导出这些配置 工具选择使用 python来开发,这样Windows、macOS、Linux平台都可以使用,而且有丰富的第三方模块。 本机先安装python,我…

首页性能优化

1、缓存组件 <router-view v-slot"{ Component }"> <keep-alive><component :isComponent /></keep-alive> </router-view>2、图片压缩 npm i image-webpack-loaderconfig.module.rule(img).rule(/\.(png)|(jpg)/).use(image-webpac…

机器人外呼的具体操作步骤是怎样的?

今天小编给大家整理一下几点关于使用AI机器人拨打电销电话的具体步骤可以归纳为以下几个关键环节&#xff1a; ​ ### 一、选择合适的AI机器人服务 1. **平台选择**&#xff1a; - 选择一个提供AI机器人拨打电话服务的平台&#xff0c;这可以是云端的AI电话机器人服务&#…

Java-List结合ComableFuture自定义线程池的工具类

为了结合 CompletableFuture 处理列表中的数据,并利用自定义线程池来并行处理这些元素,我们 可以创建一个工具类 ListCompletableFutureUtil,它包含一个静态方法 processListConcurrently, 该方法接收一个列表、一个处理每个元素的函数以及一个自定义线程池,并利用 Com…

图片去噪及边缘检测

一&#xff1a;在Python中&#xff0c;图片去噪可以通过不同的方法实现&#xff0c;这些方法包括使用简单的滤波技术到更复杂的算法&#xff0c;比如高斯模糊、中值滤波、非局部均值去噪&#xff08;Non-local Means Denoising&#xff09;等。在这里&#xff0c;我将介绍几种常…

报错-_pickle.UnpicklingError: invalid load key, ‘\x9a‘.

1.报错1 File “/usr/local/lib/python3.8/dist-packages/torch/serialization.py”, line 1002, in _legacy_load magic_number pickle_module.load(f, **pickle_load_args) _pickle.UnpicklingError: invalid load key, ‘\x9a’. 报错原因&#xff1a;使用bert的预训练模…

PTH哈希传递攻击

PTH哈希传递攻击&#xff08;Pass The Hash&#xff09; 一、PTH简介 1、攻击原理 在使用 NTLM 身份验证的系统或服务上&#xff0c;用户密码永远不会以明文形式通过网络发送。 Windows 上的应用程序要求用户提供明文密码&#xff0c;然后调用 LsaLogonUser 类的 API&#x…