yanglog

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

README

yanglog日志包,适合于golang语言

本模块,是在高性能日志包zap和日志切割lumberjack的基础上封装了一层。只需要传一个参数,即可完成配置。使用更加傻瓜,更加方便。

功能说明:

1.可自定义指定info(包含error)和error的日志文件

2.默认配置了:日志轮转,压缩,清理旧日志。可查看代码看默认参数值

3.用了zap(高性能日志库)的sugar功能

4.输出日志为json格式

使用代码举例:

备注:需要引入zap

package main
import (
 "context"
 // 引入该包
 // 或github.com/xiaoshanyangcode/yanglog
 logger  "gitee.com/xiaoshanyangcode/yanglog"
 "time"
 // 需要引入zap
 "go.uber.org/zap" 
)
func main() {
 // 全局退出函数
 ctx, cancel := context.WithCancel(context.Background())
 // 配置,只需要传LogConf这一个参数(子参数全部是可选的,可一个都不填)
 logconf := logger.LogConf{InfoFile: "looog/info_utc.log"}
 //  生成日志对象
 log := logger.NewLogger(ctx, logconf)
 defer log.Sync()
  
 // 可以正常使用了
 // 用法同zap的sugar,本身就是
 log.Error("This is an error message")
 log.Debugw("debug", "asdf", "asdfsdfaf")
 // 循环创建日志举例
 go func(log *zap.SugaredLogger) {
   for i := 0; i < 1000; i++ {
     log.Error("This is an error message")
     log.Debugw("debug", "asdf", "asdfsdfaf")
     time.Sleep(time.Millisecond * 1000)
   }
 }(log)
 time.Sleep(time.Minute)
 cancel()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(ctx context.Context, config LogConf) *zap.SugaredLogger

终端输出debug级别的日志,文件输出info和error日志。如果不满足要求,请自行修改

Types

type LogConf

type LogConf struct {
	// 注意:轮转后旧的日志文件名自动带日期(UTC时间),建议在名字上带上utc。
	InfoFile   string // 可选。请写绝对路径。默认值:可执行程序目录/log/info_utc.log
	ErrorFile  string // 可选。请写绝对路径。默认值:可执行程序目录/log/error_utc.log
	MaxSize    int    // 可选。MB。默认值:1000
	MaxBackups int    // 可选。保留旧日志文件的最大数量,默认值:1000
	MaxAge     int    // 可选。保留旧日志的最大天数,默认值:90
	OutFormat  bool   // 可选。默认值为False,输出格式为json,设置为True时输出格式为str。
}

注意:轮转后旧的日志文件名自动带日期(UTC时间),建议在名字上带上utc。

Jump to

Keyboard shortcuts

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