github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/log/readme.md (about)

     1  #### 介绍
     2  
     3  日志库,依赖三方日志库:  uber [zap](https://github.com/uber-go/zap) , 日志按时间切割[lestrrat-go/file-rotatelogs](github.com/lestrrat-go/file-rotatelogs);不设置日志目录路径,默认打印console日志。
     4  
     5  #### 功能:
     6  
     7  1. 日志分级:基础日志 main(info,debug,warn,err,fatal),panic; 业务日志 biz; 访问请求日志 access,rpc; 启动日志
     8  2. 日志单元unit(k/v)记录,一次log输出
     9  3. 日志切分, 每小时切分一次
    10  4. 自定义编码输出日志格式encoder(zap自带 console ,json 两种日志,其他日志格式需要自定义,参考json日志encoder)
    11  5. gin/grpc 访问日志中间件(middleware)
    12  
    13  #### 接口:
    14  
    15  ```go
    16  // setup log with those params:
    17  // project name for tapper log
    18  // log.json config path,
    19  // default log path for log.json undefined log path
    20  // tapper user defined trace log obj
    21  func Setup(projectName string, confPath string, defaultLogPath string, traceLogger tapper.ITraceLog) error
    22  type ITraceLog interface {
    23  	SetTraceLogFromGinHeader(c *gin.Context) *TraceLog
    24  }
    25  var TraceLogger ITraceLog // eg: BDTraceLog log_bd_http_trace.go
    26  
    27  
    28  func AccessInfo(msg string, fields ...zap.Field)
    29  
    30  func Info(args ...interface{})
    31  func Debug(args ...interface{})
    32  func Warn(args ...interface{})
    33  func Error(args ...interface{})
    34  func Infof(format string, args ...interface{})
    35  func Debugf(format string, args ...interface{})
    36  func Warnf(format string, args ...interface{})
    37  func Errorf(format string, args ...interface{})
    38  func RpcInfo(params ...interface{})
    39  func RpcInfof(format string, params ...interface{})
    40  func Recover(v ...interface{})
    41  func Recoverf(format string, params ...interface{})
    42  
    43  // flush main, biz, access, panic, rpc log
    44  // Sync flushes any buffered log entries.
    45  func FlushLog() 
    46  
    47  // access log middleware
    48  
    49  // add ignore request uri, don't to tapper log
    50  func AddIgnoreReqUri(uri ...string)
    51  // gin access log
    52  func GinLogger() gin.HandlerFunc
    53  // grpc access log
    54  func GrpcLogger() grpc.UnaryServerInterceptor
    55  
    56  // add log unit like bd odp addNotice
    57  func (p *logUnits) AddLogUnit(k string, v string)
    58  
    59  // multi go routine safe add
    60  func (p *LogUnits) AddLogUnitThreadSafe(k string, v string)
    61  
    62  ```
    63  
    64  测试见example_test
    65  
    66  #### 配置 (log.json)
    67  
    68  ```json
    69  {
    70    "logs": [
    71      {
    72        "logger": "main",// log type:main(debug,info,warn)log, err log, access log, biz log, panic log, rpc log 
    73        "min_level": "debug",// log min level 
    74        "add_caller": true,// zapcore addCaller open, skip to show caller line
    75        "policy": "filter",// filter  zapTee 
    76        "filters": [
    77          {
    78            "level": "debug,info,warn",// log level
    79            "path": "./log/zap.log" // log path
    80          },
    81          {
    82            "level": "error",
    83            "path": "./log/zap.err.log"
    84          }
    85        ]
    86      },
    87      {
    88        "logger": "access",
    89        "min_level": "info",
    90        "policy": "file",
    91        "path": "./log/zap-access.log"
    92      },
    93      {
    94        "logger": "biz",
    95        "min_level": "info",
    96        "add_caller": true,
    97        "policy": "file",
    98        "path": "./log/zap-biz.log"
    99      },
   100      {
   101        "logger": "panic",
   102        "min_level": "info",
   103        "add_caller": true,
   104        "policy": "file",
   105        "path": "./log/zap-reco.log"
   106      },
   107      {
   108        "logger": "rpc",
   109        "min_level": "info",
   110        "add_caller": true,
   111        "policy": "file",
   112        "path": "./log/zap-rpc.log"
   113      }
   114    ],
   115    "rotateByHour": true //open rotate log per hour:00, if deploy to docker in k8s, close
   116  }
   117  
   118  ```
   119  
   120  
   121