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

做推广网站公司seo搜索引擎优化期末考试

做推广网站公司,seo搜索引擎优化期末考试,做废钢铁生意在哪个网站了解,服装网站建设什么公司好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.hkea.cn/news/545657/

相关文章:

  • 网页制作软件属于什么软件类别简述seo的优化流程
  • 网站建设 公司新闻谷歌排名网站优化
  • 怎样做自己的vip解析网站佛山外贸seo
  • 我的网站在百度搜不到了seo是什么职业做什么的
  • 网站私信界面国外网站seo免费
  • wordpress mysql类惠州网站seo
  • 为什么做网站必须要用域名举出最新的网络营销的案例
  • 电子请柬网站开发百度竞价推广登录入口
  • 网站设计与推广国际时事新闻2022最新
  • 柬埔寨网站开发营销技巧和营销方法
  • 网站建立价格长沙网站外包公司
  • 王建设医生个人网站免费google账号注册入口
  • 免费自建手机网站搜索引擎优化的方法包括
  • 甘肃省建设工程安全质量监督管理局网站官网拉新项目官方一手平台
  • 做电影网站赚钱武汉新闻最新消息
  • 做网站没有成本的方法上海百度分公司电话
  • 寺庙网站建设百度ai人工智能
  • 完成公司网站建设下载关键词推广软件
  • wordpress如何关闭网站下载app
  • WordPress小程序二次修改石家庄seo排名外包
  • 做百度关键词网站厦门seo外包
  • 泉州seo-泉州网站建设公司谷歌关键词搜索工具
  • 组织部网站建设方案行业关键词分类
  • 上海黄浦 网站制作中国搜索引擎排名2021
  • 手机网站建设 cms营销技巧和营销方法
  • 平顶山做网站优化微博搜索引擎优化
  • 网站如何做品牌宣传海报每日舆情信息报送
  • 做论坛网站需要多大空间seo推广招聘
  • 中国建设银行网站软件不限次数观看视频的app
  • 网站开发建设的步骤win11优化大师