当前位置: 首页 > news >正文

建设集团招工信息网站seo入门视频

建设集团招工信息网站,seo入门视频,怎样制作h5页面,重庆建网站计划zap日志封装 Zap是一个高性能、结构化日志库,专为Go语言设计。它由Uber开源,并且在Go社区中非常受欢迎。它的设计目标是提供一个简单易用、高效稳定、灵活可扩展的日志系统。 以下是Zap的一些主要特点: 1.高性能:Zap的性能非常出…

zap日志封装

Zap是一个高性能、结构化日志库,专为Go语言设计。它由Uber开源,并且在Go社区中非常受欢迎。它的设计目标是提供一个简单易用、高效稳定、灵活可扩展的日志系统。

在这里插入图片描述

以下是Zap的一些主要特点:

1.高性能:Zap的性能非常出色,可以在不影响应用程序性能的情况下记录大量的日志。它的性能比其他Go语言的日志库高出数倍,这使得它成为高负载生产环境中的不错选择。
2.结构化日志:Zap支持结构化日志,这意味着你可以在日志中包含结构化数据,而不是只是简单的文本。这个功能非常有用,因为它可以让你更容易地对日志进行分析和搜索。
3.可扩展:Zap提供了一个灵活的接口,可以让你轻松地添加自定义的日志输出器和格式化器。这使得它非常适合在大型项目中使用。
4.模块化:Zap提供了一个模块化的设计,可以让你选择仅使用你需要的功能。这使得它非常适合在不同的项目中使用,因为你可以只使用你需要的功能,而不必使用整个库。
5.安全:Zap使用了一个严格的日志记录器接口,这可以确保你的应用程序的日志记录不会被恶意软件篡改或删除。

实现方式

yaml配置文件

在根目录下创建一个configs文件夹,然后再创建zap.debug.yaml

# zap logger configuration
Zap:Level: 'info'Prefix: 'gin-vue-admin'Format: 'console'Director: 'logs'EncodeLevel: 'LowercaseColorLevelEncoder'StacktraceKey: 'stacktrace'MaxAge: 30 # 默认日志留存默认以天为单位ShowLine: trueLogInConsole: true

放置在全局Config中 config.go

我的创建是使用protobuf快速创建出来的,如果你不是使用protobuf,你可以忽略这个tag。在根目录下创建一个config文件,然后创建一个config.go 文件用来存放全局的config。

config.go 文件

type Config struct {state         protoimpl.MessageStatesizeCache     protoimpl.SizeCacheunknownFields protoimpl.UnknownFieldsZap     *Zap     `protobuf:"bytes,2,opt,name=Zap,proto3" json:"Zap,omitempty"`}// Zap zap logger config
type Zap struct {state         protoimpl.MessageStatesizeCache     protoimpl.SizeCacheunknownFields protoimpl.UnknownFields// Level 级别Level string `protobuf:"bytes,1,opt,name=Level,proto3" json:"Level,omitempty"`// Prefix 日志前缀Prefix string `protobuf:"bytes,2,opt,name=Prefix,proto3" json:"Prefix,omitempty"`// Format 输出Format string `protobuf:"bytes,3,opt,name=Format,proto3" json:"Format,omitempty"`// Director 日志文件夹Director string `protobuf:"bytes,4,opt,name=Director,proto3" json:"Director,omitempty"`// EncodeLevel 编码级EncodeLevel string `protobuf:"bytes,5,opt,name=EncodeLevel,proto3" json:"EncodeLevel,omitempty"`// StacktraceKey 栈名StacktraceKey string `protobuf:"bytes,6,opt,name=StacktraceKey,proto3" json:"StacktraceKey,omitempty"`// MaxAge 日志留存时间MaxAge int64 `protobuf:"varint,7,opt,name=MaxAge,proto3" json:"MaxAge,omitempty"`// ShowLine 显示行ShowLine bool `protobuf:"varint,8,opt,name=ShowLine,proto3" json:"ShowLine,omitempty"`// LogInConsole 输出控制台LogInConsole bool `protobuf:"varint,9,opt,name=LogInConsole,proto3" json:"LogInConsole,omitempty"`
}

创建zap.go

然后再config文件夹再创建zap.go文件,该文件主要是用来将我们配置文件的内容转换成为zap所认识的内容。

type LevelEncoder int// ZapEncodeLevel 根据 EncodeLevel 返回 zapcore.LevelEncoder
func (x *Zap) ZapEncodeLevel() zapcore.LevelEncoder {switch {case x.EncodeLevel == "LowercaseLevelEncoder": // 小写编码器(默认)return zapcore.LowercaseLevelEncodercase x.EncodeLevel == "LowercaseColorLevelEncoder": // 小写编码器带颜色return zapcore.LowercaseColorLevelEncodercase x.EncodeLevel == "CapitalLevelEncoder": // 大写编码器return zapcore.CapitalLevelEncodercase x.EncodeLevel == "CapitalColorLevelEncoder": // 大写编码器带颜色return zapcore.CapitalColorLevelEncoderdefault:return zapcore.LowercaseLevelEncoder}
}// TransportLevel 根据字符串转化为 zapcore.Level
func (x *Zap) TransportLevel() zapcore.Level {x.Level = strings.ToLower(x.Level)switch x.Level {case "debug":return zapcore.DebugLevelcase "info":return zapcore.InfoLevelcase "warn":return zapcore.WarnLevelcase "error":return zapcore.WarnLevelcase "dpanic":return zapcore.DPanicLevelcase "panic":return zapcore.PanicLevelcase "fatal":return zapcore.FatalLeveldefault:return zapcore.DebugLevel}
}

创建核心文件core

这里我主要放置一些Initialization 初始化的方法,比如gorm、viper、zap等一些核心的内容。

创建zap.go

在core文件中创建zap.go 文件,该文件主要是初始化自己配置的zap日志,一般会把日志分割、日志存放地、注册到全局等放置在这里,当然为了让代码更加整洁和可阅读性下,我们会对这里封装成为方法。注: _zap 命名方式是因为和zap包重名了,可以根据自己喜好命名,但是这样的命明也就是仅在该文件下生效,你可以认为这样变成了所谓的私有性

core/zap.go

var Zap = new(_zap)type _zap struct{}// Initialization 初始化
func (c *_zap) Initialization() {ok, _ := utils.Directory.PathExists(global.Config.Zap.Director)if !ok { // 判断是否有 global.Config.Zap.Director 文件夹fmt.Printf("create %v directory\n", global.Config.Zap.Director)_ = os.Mkdir(global.Config.Zap.Director, os.ModePerm)}cores := internal.Zap.GetZapCores()         // 获取 zap 核心切片logger := zap.New(zapcore.NewTee(cores...)) // 初始化 zap.Loggerif global.Config.Zap.ShowLine {             // 判断是否显示行logger = logger.WithOptions(zap.AddCaller())}zap.ReplaceGlobals(logger) // logger 注册到全局, 通过 zap.L() 调用日志组件
}

创建私有访问方法

在core文件夹下创建interal文件中,这个internal的方法仅能在这个core下的文件才可以进行访问,其他文件夹比如service、api、util等文件夹无法访问,这样使得这些方法不会泄漏导致程序结构的污染性,我个人也比较喜欢这样去命名以及去写代码。注:下列写法:core/interal/zap.go ,但是我们调取interal文件夹的方法不需要通过core去调取,直接使用interal进行访问。

core/interal/zap.go


var Zap = new(_zap)type _zap struct{}// GetEncoder 获取 zapcore.Encoder
func (z *_zap) GetEncoder() zapcore.Encoder {// 日志的内容格式有 控制台 和 jsonif global.Config.Zap.Format == "json" {return zapcore.NewJSONEncoder(z.GetEncoderConfig())}return zapcore.NewConsoleEncoder(z.GetEncoderConfig())
}// GetEncoderConfig 获取zapcore.EncoderConfig
func (z *_zap) GetEncoderConfig() zapcore.EncoderConfig {return zapcore.EncoderConfig{MessageKey:     "message",LevelKey:       "level",TimeKey:        "time",NameKey:        "logger",CallerKey:      "caller",StacktraceKey:  global.Config.Zap.StacktraceKey,LineEnding:     zapcore.DefaultLineEnding,EncodeLevel:    global.Config.Zap.ZapEncodeLevel(),EncodeTime:     z.CustomTimeEncoder,EncodeDuration: zapcore.SecondsDurationEncoder,EncodeCaller:   zapcore.FullCallerEncoder,}
}// GetEncoderCore 获取Encoder的 zapcore.Core
func (z *_zap) GetEncoderCore(l zapcore.Level, level zap.LevelEnablerFunc) zapcore.Core {syncer, err := FileRotatelogs.GetWriteSyncer(l.String()) // 使用file-rotatelogs进行日志分割if err != nil {fmt.Printf("Get Write Syncer Failed err:%v", err.Error())return nil}return zapcore.NewCore(z.GetEncoder(), syncer, level)
}// CustomTimeEncoder 自定义日志输出时间格式
func (z *_zap) CustomTimeEncoder(t time.Time, encoder zapcore.PrimitiveArrayEncoder) {encoder.AppendString(global.Config.Zap.Prefix + " " + t.Format("2006-01-02 15:04:05.000"))
}// GetZapCores 根据配置文件的Level获取 []zapcore.Corefunc (z *_zap) GetZapCores() []zapcore.Core {cores := make([]zapcore.Core, 0, 7)for level := global.Config.Zap.TransportLevel(); level <= zapcore.FatalLevel; level++ {cores = append(cores, z.GetEncoderCore(level, z.GetLevelPriority(level)))}return cores
}// GetLevelPriority 根据 zapcore.Level 获取 zap.LevelEnablerFunc
func (z *_zap) GetLevelPriority(level zapcore.Level) zap.LevelEnablerFunc {switch level {case zapcore.DebugLevel:return func(level zapcore.Level) bool { // 调试级别return level == zap.DebugLevel}case zapcore.InfoLevel:return func(level zapcore.Level) bool { // 日志级别return level == zap.InfoLevel}case zapcore.WarnLevel:return func(level zapcore.Level) bool { // 警告级别return level == zap.WarnLevel}case zapcore.ErrorLevel:return func(level zapcore.Level) bool { // 错误级别return level == zap.ErrorLevel}case zapcore.DPanicLevel:return func(level zapcore.Level) bool { // dpanic级别return level == zap.DPanicLevel}case zapcore.PanicLevel:return func(level zapcore.Level) bool { // panic级别return level == zap.PanicLevel}case zapcore.FatalLevel:return func(level zapcore.Level) bool { // 终止级别return level == zap.FatalLevel}default:return func(level zapcore.Level) bool { // 调试级别return level == zap.DebugLevel}}
}

在main中注册

在根目录下创建一个main.go文件(这个就不多啰嗦了…)

main.go

func main() {core.Zap.Initialization()
}
http://www.dt0577.cn/news/10004.html

相关文章:

  • 营销型集团网站建设友情链接交换
  • 做购物网站怎么赚钱扬州网络推广公司
  • 青岛市城乡建设委员会网站电话百度400电话
  • 营销网站的方法百度营销是什么
  • 网站备案必须要幕布吗seo关键词优化工具
  • asp网站建设今日热搜榜
  • 尼尔的h版是那个网站做的免费软文发布平台
  • 长春建站合肥seo服务商
  • 哪个网站可以发宝贝链接做宣传百度搜索结果
  • amaze ui做网站好吗西安seo顾问公司
  • 常德政府网站站长seo的内容主要有哪些方面
  • 网站开发视频教程泉州百度seo
  • 提高网站排名品牌营销咨询公司
  • dw网站的滑屏怎么做活动营销的方式有哪些
  • 做洗衣液的企业网站今天国际新闻最新消息10条
  • 公司的网站推广怎么做优化设计单元测试卷答案
  • 网站建设做网站seo权重是什么意思
  • 做三折页的网站互动营销公司
  • 巿住房城乡建设委官方网站东莞搜索优化
  • 做网站前提需要什么免费视频外链生成推荐
  • 网站建设如何测试重庆快速排名优化
  • 临沂网站制作公司6李飞seo
  • 做暖暖免费视频网站站长基地
  • 在哪可以找到做网站的社交媒体营销
  • 有什么可以做cad赚钱的网站武汉seo公司排名
  • 有哪些做的好的小众网站深圳seo网站推广方案
  • 安全的企业网站开发如何开网店
  • 女性手表网站国家免费培训网站
  • 网站制作最新技术百度搜索引擎入口登录
  • 河池城乡住房和建设局网站微信朋友圈推广软文