golang zap日志模块封装sentry

embedded/2024/10/22 10:57:57/

我们自己写个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/embedded/104069.html

相关文章

Magisk/Riru/LSPosed安装

Magisk/Riru/LSPosed安装 1: Magisk安装下载 ​ 首先附上Magisk地址: https://github.com/topjohnwu/Magisk/releases 下载应用apk.安装后,根据应用内提示重新安装,重启手机 2: Riru安装 https://github.com/RikkaApps/Riruhttps://github.com/RikkaApps/Riru/releases/ta…

HTML中自定义属性并通过JS获取属性值

是的&#xff0c;HTML可以自定义属性。自定义属性的语法是使用 data- 前缀&#xff0c;后面跟上自定义的属性名。例如&#xff1a; <div id"example" data-custom"myValue">Hello World</div>在这个例子中&#xff0c;data-custom 就是自定义…

计算机毕业设计选题推荐-救援物资管理系统-Java/Python项目实战

✨作者主页&#xff1a;IT毕设梦工厂✨ 个人简介&#xff1a;曾从事计算机专业培训教学&#xff0c;擅长Java、Python、微信小程序、Golang、安卓Android等项目实战。接项目定制开发、代码讲解、答辩教学、文档编写、降重等。 ☑文末获取源码☑ 精彩专栏推荐⬇⬇⬇ Java项目 Py…

【专题】2024全球电商消费电子市场研究报告合集PDF分享(附原数据表)

原文链接&#xff1a;https://tecdat.cn/?p37552 在如今数字经济蓬勃发展的大环境下&#xff0c;电商行业正以前所未有的迅猛之势&#xff0c;对全球商业版图进行着深刻的重塑。据 Stocklytics 发布的有关全球电商市场价值及未来增长趋势的专项调查报告显示&#xff0c;2024…

【GIT】Idea中的git命令使用-全网最新详细(包括现象含义)

原文网址&#xff1a;【GIT】Idea中的git命令使用-全网最新详细&#xff08;包括现象含义&#xff09; 文章目录 **命令1&#xff1a;查看当前所处分支&#xff1a;****命令2&#xff1a;拉取最新代码&#xff1a;****命令3&#xff1a;切换分支&#xff1a;****命令4&#xff…

数据流的交响乐:SQL在复杂事件处理中的精妙运用

标题&#xff1a;数据流的交响乐&#xff1a;SQL在复杂事件处理中的精妙运用 在当今的数据驱动世界中&#xff0c;复杂事件处理&#xff08;Complex Event Processing&#xff0c;简称CEP&#xff09;已成为实时数据分析的关键技术。SQL&#xff0c;作为一种强大的查询语言&am…

【Python】如何使用pip,安装第三方库和qrcode、xlrd库的使用

第三方库 第三方库就是别人已经实现好了的库&#xff0c;我们可以拿过来直接使用 虽然标准库已经很强大了&#xff0c;但是终究是有限的&#xff0c;而第三方库可以视为是集合了全世界 Python 程序猿的智慧&#xff0c;可以说是几乎无穷无尽 使用 pip pip 是 Python 内置的包…

原生开发柱状图

一、先看效果 原生柱状图 二、实现步骤 1、引入chart.js库&#xff1a; <script src"jscss/chart.js"></script> 如果没有在网上找到chart.js&#xff0c;可以用我文末的。 2、在html页面中加入&#xff1a; <canvas id"myChart">&…