log

package
v0.0.0-...-a36dcc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2023 License: MIT Imports: 22 Imported by: 0

README

介绍

日志库,依赖三方日志库: uber zap , 日志按时间切割lestrrat-go/file-rotatelogs;不设置日志目录路径,默认打印console日志。

功能:

  1. 日志分级:基础日志 main(info,debug,warn,err,fatal),panic; 业务日志 biz; 访问请求日志 access,rpc; 启动日志
  2. 日志单元unit(k/v)记录,一次log输出
  3. 日志切分, 每小时切分一次
  4. 自定义编码输出日志格式encoder(zap自带 console ,json 两种日志,其他日志格式需要自定义,参考json日志encoder)
  5. gin/grpc 访问日志中间件(middleware)

接口:

// setup log with those params:
// project name for tapper log
// log.json config path,
// default log path for log.json undefined log path
// tapper user defined trace log obj
func Setup(projectName string, confPath string, defaultLogPath string, traceLogger tapper.ITraceLog) error
type ITraceLog interface {
	SetTraceLogFromGinHeader(c *gin.Context) *TraceLog
}
var TraceLogger ITraceLog // eg: BDTraceLog log_bd_http_trace.go


func AccessInfo(msg string, fields ...zap.Field)

func Info(args ...interface{})
func Debug(args ...interface{})
func Warn(args ...interface{})
func Error(args ...interface{})
func Infof(format string, args ...interface{})
func Debugf(format string, args ...interface{})
func Warnf(format string, args ...interface{})
func Errorf(format string, args ...interface{})
func RpcInfo(params ...interface{})
func RpcInfof(format string, params ...interface{})
func Recover(v ...interface{})
func Recoverf(format string, params ...interface{})

// flush main, biz, access, panic, rpc log
// Sync flushes any buffered log entries.
func FlushLog() 

// access log middleware

// add ignore request uri, don't to tapper log
func AddIgnoreReqUri(uri ...string)
// gin access log
func GinLogger() gin.HandlerFunc
// grpc access log
func GrpcLogger() grpc.UnaryServerInterceptor

// add log unit like bd odp addNotice
func (p *logUnits) AddLogUnit(k string, v string)

// multi go routine safe add
func (p *LogUnits) AddLogUnitThreadSafe(k string, v string)

测试见example_test

配置 (log.json)

{
  "logs": [
    {
      "logger": "main",// log type:main(debug,info,warn)log, err log, access log, biz log, panic log, rpc log 
      "min_level": "debug",// log min level 
      "add_caller": true,// zapcore addCaller open, skip to show caller line
      "policy": "filter",// filter  zapTee 
      "filters": [
        {
          "level": "debug,info,warn",// log level
          "path": "./log/zap.log" // log path
        },
        {
          "level": "error",
          "path": "./log/zap.err.log"
        }
      ]
    },
    {
      "logger": "access",
      "min_level": "info",
      "policy": "file",
      "path": "./log/zap-access.log"
    },
    {
      "logger": "biz",
      "min_level": "info",
      "add_caller": true,
      "policy": "file",
      "path": "./log/zap-biz.log"
    },
    {
      "logger": "panic",
      "min_level": "info",
      "add_caller": true,
      "policy": "file",
      "path": "./log/zap-reco.log"
    },
    {
      "logger": "rpc",
      "min_level": "info",
      "add_caller": true,
      "policy": "file",
      "path": "./log/zap-rpc.log"
    }
  ],
  "rotateByHour": true //open rotate log per hour:00, if deploy to docker in k8s, close
}

Documentation

Overview

use function

Index

Examples

Constants

View Source
const (
	LOGID     = "logId"
	REFERER   = "referer"
	COOKIE    = "cookie"
	CLIENT_IP = "client_ip"
	LOCAL_IP  = "local_ip"
	MODULE    = "module"
	UA        = "ua"
	HOST      = "host"
	URI       = "uri"
	TRACE     = "trace"
	NOTICES   = "notice"
	MONITOR   = "monitor"
	RESPONSE  = "response"
	REQUEST   = "request"
	CODE      = "code"
	COST      = "cost"
	METHOD    = "method"
	ERR       = "err"
)
View Source
const (
	REQUEST_PARAM_CTX = "REQUEST_PARAM_CTX"
)

Variables

View Source
var (
	LOCALIP        = net.GetLocalIPv4()
	MaxRespLen     = 0
	MaxReqParamLen = 4096
	IgnoreReqUris  = make([]string, 0)
)

Functions

func AccessInfo

func AccessInfo(msg string, fields ...zap.Field)
Example
fields := []zap.Field{
	zap.String(LOGID, "123123"),
	zap.String(URI, "/source/get"),
	zap.String(REFERER, "www.google.com"),
	zap.Any(COOKIE, "cookie:1231"),
	zap.String(CLIENT_IP, "192.168.7.3"),
	zap.String(LOCAL_IP, LOCALIP),
	zap.String(UA, "web-chrome"),
	zap.String(HOST, "www.baidu.com"),
	zap.String(MODULE, "live"),
	zap.String("request_param", "a=1&b=2"),
	zap.String(NOTICES, "notices"),
	zap.String(MONITOR, "{'monitor':true}"),
	zap.Int(CODE, 200),
	zap.String(RESPONSE, "response"),
	zap.Int64(COST, 100),
}
AccessInfo("", fields...)
Output:

func AddIgnoreReqUri

func AddIgnoreReqUri(uri ...string)

add ignore request uri

func BizArchive

func BizArchive(format string, params ...interface{})

func Debug

func Debug(params ...interface{})

func Debugf

func Debugf(format string, params ...interface{})

func Error

func Error(params ...interface{})

func Errorf

func Errorf(format string, params ...interface{})

func FlushLog

func FlushLog()

flush main, biz, access, panic, rpc log Sync flushes any buffered log entries.

func GinLogger

func GinLogger() gin.HandlerFunc

gin access log

func GrpcLogger

func GrpcLogger() grpc.UnaryServerInterceptor

grpc access log

func Info

func Info(params ...interface{})

func Infof

func Infof(format string, params ...interface{})

func IsExist

func IsExist(path string) bool

func Recover

func Recover(v ...interface{})

recover panic log

func Recoverf

func Recoverf(format string, params ...interface{})

recover panic format log

func RpcInfo

func RpcInfo(params ...interface{})

func RpcInfof

func RpcInfof(format string, params ...interface{})

func SetLogPrintMaxReqParamLen

func SetLogPrintMaxReqParamLen(maxReqParamLen int)

func SetLogPrintMaxRespLen

func SetLogPrintMaxRespLen(maxRespLen int)

func SetLogRequestParam

func SetLogRequestParam(ctx context.Context, body interface{})

func Setup

func Setup(projectName string, confPath string, defaultLogPath string, traceLogger tapper.ITraceLog) error

setup log with those params: project name for tapper log log.json config path, default log path for log.json undefined log path tapper user defined trace log obj

Example
err := Setup("testProject", "./", "./log", nil)
if err != nil {
	println(err)
}
Example_Info()
Example_Infof()
ExampleAccessInfo()
Output:

func TimeEncoder

func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

func Warn

func Warn(params ...interface{})

func Warnf

func Warnf(format string, params ...interface{})

Types

type Filter

type Filter struct {
	Level string `json:"level"`
	Path  string `json:"path"`
}

type LogConfig

type LogConfig struct {
	Logs         []LoggerConfig `json:"logs"`
	RotateByHour bool           `json:"rotateByHour"`
}

type Logger

type Logger struct {
	Filename string
	// contains filtered or unexported fields
}

func (*Logger) Close

func (l *Logger) Close() error

Close implements io.Closer, and closes the current logfile.

func (*Logger) Write

func (l *Logger) Write(p []byte) (n int, err error)

type LoggerConfig

type LoggerConfig struct {
	Logger    string   `json:"logger"`
	MinLevel  string   `json:"min_level"`
	AddCaller bool     `json:"add_caller"`
	Policy    string   `json:"policy"`
	Filters   []Filter `json:"filters"`
	Path      string   `json:"path"`
}

Directories

Path Synopsis
from zap json_encoder.go change to encoder
from zap json_encoder.go change to encoder
internal
bufferpool
Package bufferpool houses zap's shared internal buffer pool.
Package bufferpool houses zap's shared internal buffer pool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL