//  Copyright(c) 2021. Huawei Technologies Co.,Ltd.  All rights reserved.

// Package hwlog provides the capability of processing Huawei log rules.
package hwlog

import (
"context"
"fmt"
"go.uber.org/zap"
)

// Logger logger struct
type Logger struct {
ZapLogger *zap.Logger
}

// NewLogger create Logger
func NewLogger() *Logger {
newLogger := new(Logger)
return newLogger
}

// InitLogger initialize run logger
func (lg *Logger) InitLogger(config *LogConfig, stopCh <-chan struct{}) error {
if lg != nil && lg.ZapLogger != nil {
lg.Warn("logger is been initialized.")
return nil
}
zapLog, err := Init(config, stopCh)
if err != nil {
return err
}
lg.ZapLogger = zapLog
return nil
}

// IsInit check logger initialized
func (lg *Logger) IsInit() bool {
return lg.ZapLogger != nil
}

// Debug record debug not format
func (lg *Logger) Debug(args ...interface{}) {
lg.DebugWithCtx(nil, args...)
}

// Debugf record debug
func (lg *Logger) Debugf(format string, args ...interface{}) {
lg.DebugfWithCtx(nil, format, args...)
}

// DebugWithCtx record Debug not format
func (lg *Logger) DebugWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Debug, fmt.Sprint(args...), ctx)
}
}

// DebugfWithCtx record Debug format
func (lg *Logger) DebugfWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Debug, fmt.Sprintf(format, args...), ctx)
}
}

// Info record info not format
func (lg *Logger) Info(args ...interface{}) {
lg.InfoWithCtx(nil, args...)
}

// Infof record info
func (lg *Logger) Infof(format string, args ...interface{}) {
lg.InfofWithCtx(nil, format, args...)
}

// InfoWithCtx record Info not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) InfoWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Info, fmt.Sprint(args...), ctx)
}
}

// InfofWithCtx record Info format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) InfofWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Info, fmt.Sprintf(format, args...), ctx)
}
}

// Warn record warn not format
func (lg *Logger) Warn(args ...interface{}) {
lg.WarnWithCtx(nil, args...)
}

// Warnf record warn
func (lg *Logger) Warnf(format string, args ...interface{}) {
lg.WarnfWithCtx(nil, format, args...)
}

// WarnWithCtx record Warn not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) WarnWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Warn, fmt.Sprint(args...), ctx)
}
}

// WarnfWithCtx record Warn format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) WarnfWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Warn, fmt.Sprintf(format, args...), ctx)
}
}

// Error record error not format
func (lg *Logger) Error(args ...interface{}) {
lg.ErrorWithCtx(nil, args...)
}

// Errorf record error
func (lg *Logger) Errorf(format string, args ...interface{}) {
lg.ErrorfWithCtx(nil, format, args...)
}

// ErrorWithCtx record Error not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) ErrorWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Error, fmt.Sprint(args...), ctx)
}
}

// ErrorfWithCtx record Error format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) ErrorfWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Error, fmt.Sprintf(format, args...), ctx)
}
}

// Dpanic record DPanic not format
func (lg *Logger) Dpanic(args ...interface{}) {
lg.DPanicWithCtx(nil, args...)
}

// Dpanicf record DPanic
func (lg *Logger) Dpanicf(format string, args ...interface{}) {
lg.DPanicfWithCtx(nil, format, args...)
}

// DPanicWithCtx record DPanic not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) DPanicWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.DPanic, fmt.Sprint(args...), ctx)
}
}

// DPanicfWithCtx record DPanic format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) DPanicfWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.DPanic, fmt.Sprintf(format, args...), ctx)
}
}

// Panic record panic not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) Panic(args ...interface{}) {
lg.PanicWithCtx(nil, args...)
}

// Panicf record panic
func (lg *Logger) Panicf(format string, args ...interface{}) {
lg.PanicfWithCtx(nil, format, args...)
}

// PanicWithCtx record panic not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) PanicWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Panic, fmt.Sprint(args...), ctx)
}
}

// PanicfWithCtx record panic format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) PanicfWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Panic, fmt.Sprintf(format, args...), ctx)
}
}

// Fatal record fatal not format
func (lg *Logger) Fatal(args ...interface{}) {
lg.FatalWithCtx(nil, args...)
}

// Fatalf record fatal
func (lg *Logger) Fatalf(format string, args ...interface{}) {
lg.FatalfWithCtx(nil, format, args...)
}

// FatalWithCtx record fatal not format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) FatalWithCtx(ctx context.Context, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Fatal, fmt.Sprint(args...), ctx)
}
}

// FatalfWithCtx record fatal format with context, if you have no ctx, please use the method with not ctx
func (lg *Logger) FatalfWithCtx(ctx context.Context, format string, args ...interface{}) {
if lg.validate() {
printHelper(lg.ZapLogger.Fatal, fmt.Sprintf(format, args...), ctx)
}
}

func (lg *Logger) validate() bool {
if lg.ZapLogger == nil {
fmt.Println("Fatal function's logger is nil")
return false
}
return true
}

hwlog---api.go的更多相关文章

  1. 干货来袭-整套完整安全的API接口解决方案

    在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API大量存在,安全性令人堪优 在以前WEB API概念没有很普及的时候,都采用自已定义的接口和结构,对 ...

  2. 12306官方火车票Api接口

    2017,现在已进入春运期间,真的是一票难求,深有体会.各种购票抢票软件应运而生,也有购买加速包提高抢票几率,可以理解为变相的黄牛.对于技术人员,虽然写一个抢票软件还是比较难的,但是还是简单看看123 ...

  3. 几个有趣的WEB设备API(二)

    浏览器和设备之间还有很多有趣的接口, 1.屏幕朝向接口 浏览器有两种方法来监听屏幕朝向,看是横屏还是竖屏. (1)使用css媒体查询的方法 /* 竖屏 */ @media screen and (or ...

  4. html5 canvas常用api总结(三)--图像变换API

    canvas的图像变换api,可以帮助我们更加方便的绘画出一些酷炫的效果,也可以用来制作动画.接下来将总结一下canvas的变换方法,文末有一个例子来更加深刻的了解和利用这几个api. 1.画布旋转a ...

  5. JavaScript 对数据处理的5个API

    JavaScript对数据处理包括向上取整.向下取整.四舍五入.固定精度和固定长度5种方式,分别对应ceil,floor,round,toFixed,toPrecision等5个API,本文将对这5个 ...

  6. ES5对Array增强的9个API

    为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...

  7. javascript的api设计原则

    前言 本篇博文来自一次公司内部的前端分享,从多个方面讨论了在设计接口时遵循的原则,总共包含了七个大块.系卤煮自己总结的一些经验和教训.本篇博文同时也参考了其他一些文章,相关地址会在后面贴出来.很难做到 ...

  8. 一百元的智能家居——Asp.Net Mvc Api+讯飞语音+Android+Arduino

    大半夜的,先说些废话提提神 如今智能家居已经不再停留在概念阶段,高大上的科技公司都已经推出了自己的部分或全套的智能家居解决方案,不过就目前的现状而言,大多还停留在展厅阶段,还没有广泛的推广起来,有人说 ...

  9. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

  10. bootstrap + requireJS+ director+ knockout + web API = 一个时髦的单页程序

    也许单页程序(Single Page Application)并不是什么时髦的玩意,像Gmail在很早之前就已经在使用这种模式.通常的说法是它通过避免页面刷新大大提高了网站的响应性,像操作桌面应用程序 ...

随机推荐

  1. Dubbo本地调试

    dubbo 启动标志 Dubbo service server started <dubbo:reference id="transferTimingUploadHisRPCServi ...

  2. android 逆向 smali手写helloworld

    编写Hello.smali文件 .class public LHelloWorld; .super Ljava/lang/Object; .method public static main([Lja ...

  3. 纯CSS实现“流星赶月”,祝大家中秋节快乐

    明天就是中秋节了,就想着用CSS画一个月亮送给园友们吧.但是就画一个月亮也太简单了些,于是便加了一些星星点缀以及流星坠落的效果.这篇文章就用纯CSS为大家实现一个"流星赶月"的效果 ...

  4. 03-MyBatisPlus的CRUD 接口

    一.insert 1.插入操作 @RunWith(SpringRunner.class) @SpringBootTest public class CRUDTests { @Autowired pri ...

  5. k8s使用心得

    查看当前所有namespaces [root@master ~]# kubectl get namespaces -A NAME STATUS AGE default Active 63d hkd A ...

  6. CentOS7内置Realtek网卡驱动r8169降级r8168

    前几天装了几台服务器测试,在使用的过程中发现,每次重启系统,登录界面会弹出网卡提示  "r8169 0000:02:00 eth0 Invalid ocp reg 17758!" ...

  7. 《HelloGitHub》第 78 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  8. Effective java 总结

    用静态工厂方法代替构造器的最主要好处 1.不必每次都创建新的对象 Boolean.valueOf Long.valueOf 2.直接返回接口的子类型,对于外界来说并不需要关心实现细节,主要知道这个接口 ...

  9. 齐博x1页面不直接报错,如何排查

    有的页面是不会直接报错的,比如像下面这个,这个时候需要你用谷歌或火狐浏览器打开,按F12键进入开发者模式,然后选择Network选项,刷新一下当前的网页,就会看到红色的请求.单独打开他.就可以看到错误 ...

  10. .net core-利用PdfSharpCore和SkiaSharp.QrCode 添加PDF二维码页眉

    前序 由于去年的一个项目需要在PDF 添加公司二维码 ,当时在网上找了很多操作PDF方案,第一种Aspose.PDF,很遗憾 Aspose.PDF 有添加版权的背景还是页脚我忘记了,不适合公司项目,最 ...