mixlog

package module
v0.0.0-...-f10d3d4 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MulanPSL-2.0 Imports: 12 Imported by: 2

README

日志记录器

介绍

支持颜色输出。支持各handler分别定义formatter和等级。

使用的第三方包

  • github.com/fatih/color 用于输出颜色log
  • github.com/mattn/go-colorable 用于在Windows中的颜色log
  • github.com/lestrrat-go/file-rotatelogs 用于滚动记录日志文件
安装教程
go get gitee.com/sillyman/mixlog

使用说明

请参考示例。

怎么定义输出格式?

在创建Handler的时候,需要提供一个 *formatter 对象,由 formatter 定义日志样式和日志的时间格式。在模块已经提供了4个常用的日志样式模板。

FormatLongFileName          = "{{.Lvl}} - {{.Time}} - {{.LongFileName}}({{.Line}}) - {{.Message}}"
FormatShortFileName         = "{{.Lvl}} - {{.Time}} - {{.ShortFilename}}({{.Line}}) - {{.Message}}"
FormatFuncNameLongFileName  = "{{.Lvl}} - {{.Time}} - {{.FuncName}} - {{.LongFileName}}({{.Line}}) - {{.Message}}"
FormatFuncNameShortFileName = "{{.Lvl}} - {{.Time}} - {{.FuncName}} - {{.ShortFilename}}({{.Line}}) - {{.Message}}"

日志样式提供了7个模板变量(区分大小写),分别是:

  • {{.Lvl}} 日志等级的字符串形式,如INFO
  • {{.Time}} 时间,时间的格式由 formatter.TimeLayout 定义
  • {{.LongFileName}} 调用位置的完整文件名
  • {{.ShortFilename}} 调用位置的基本文件名
  • {{.Line}} 调用位置所在文件的行号
  • {{.FuncName}} 调用位置所在函数名称
  • {{.Message}} 消息内容
怎么重新定义颜色?

使用 github.com/fatih/color 输出颜色,支持Linux和Windows。模块的一个全局变量 LvlColorMap 定义等级的输出颜色,重新定义它就可以修改颜色了。

var LvlColorMap = map[Lvl]*color.Color{
	LvlDebug:   color.New(color.FgWhite),
	LvlInfo:    color.New(color.FgCyan),
	LvlWarning: color.New(color.FgYellow),
	LvlError:   color.New(color.FgHiRed),
	LvlFatal:   color.New(color.FgMagenta),
}
使用示例
package main

import (
	"os"

	"gitee.com/sillyman/mixlog"
)

func main() {
	// 直接使用全局的日志记录器,默认的等级是 INFO
	mixlog.Debug("我是一条 DEBUG 信息")
	mixlog.Info("我是一条 INFO 信息")
	mixlog.Warning("我是一条 WARNING 信息")
	mixlog.Error("我是一条 ERROR 信息")

	// 重新设置全局日志记录器的 Handler
	mixlog.SetGlobalHandlers(
		mixlog.NewHandlerToWriter(
			mixlog.LvlDebug, mixlog.MustNewFormatter(mixlog.FormatFuncNameLongFileName, ""),
			os.Stdout, false,
		),
	)
	mixlog.Debug("我是一条 DEBUG 信息")
	mixlog.Info("我是一条 INFO 信息")
	mixlog.Warning("我是一条 WARNING 信息")
	mixlog.Error("我是一条 ERROR 信息")

	// 创建一个日志对象,包括了2个handler
	log := mixlog.NewMixLog(
		mixlog.NewHandlerToWriter(
			mixlog.LvlDebug, mixlog.MustNewFormatter(mixlog.FormatFuncNameLongFileName, "20060102 15:04:05-07:00"),
			os.Stdout, false,
		),
		mixlog.MustNewHandlerToFile(
			mixlog.LvlDebug, mixlog.MustNewFormatter(mixlog.FormatFuncNameShortFileName, "2006/01/02T15:04:05.000-07:00"),
			"test.log", true,
		),
	)
	log.Debug("我是一条 DEBUG 信息")
	log.Info("我是一条 INFO 信息")
	log.Warning("我是一条 WARNING 信息")
	log.Error("我是一条 ERROR 信息")
	log.Fatal("我是一条 FATAL 信息,程序会退出 `os.Exit(1)`")
}

以上创建了一个 test.log 文件,并且控制台打印如下:

截图

Documentation

Index

Constants

View Source
const (
	// 日志条目格式化成字符串的常用模板
	FormatLongFileName          = "{{.Lvl}} - {{.Time}} - {{.LongFileName}}({{.Line}}) - {{.Message}}"
	FormatShortFileName         = "{{.Lvl}} - {{.Time}} - {{.ShortFilename}}({{.Line}}) - {{.Message}}"
	FormatFuncNameLongFileName  = "{{.Lvl}} - {{.Time}} - {{.FuncName}} - {{.LongFileName}}({{.Line}}) - {{.Message}}"
	FormatFuncNameShortFileName = "{{.Lvl}} - {{.Time}} - {{.FuncName}} - {{.ShortFilename}}({{.Line}}) - {{.Message}}"
)

Variables

View Source
var LvlColorMap = map[Lvl]*color.Color{
	LvlDebug:   color.New(color.FgWhite),
	LvlInfo:    color.New(color.FgCyan),
	LvlWarning: color.New(color.FgYellow),
	LvlError:   color.New(color.FgHiRed),
	LvlFatal:   color.New(color.FgMagenta),
}

LvlColorMap 等级和颜色的映射

Functions

func Debug

func Debug(a ...interface{})

func Debugf

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

func Error

func Error(a ...interface{})

func Errorf

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

func Fatal

func Fatal(a ...interface{})

func Fatalf

func Fatalf(format string, a ...interface{})

func Info

func Info(a ...interface{})

func Infof

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

func MustNewFormatter

func MustNewFormatter(format, timeLayout string) *formatter

func MustNewHandlerToFile

func MustNewHandlerToFile(lvl Lvl, formatter *formatter, logfile string, doTrunc bool) *handler

MustNewHandlerToFile 创建一个 handler,日志写入到指定的文件

func NewFormatter

func NewFormatter(format, timeLayout string) (*formatter, error)

NewFormatter 创建一个格式化器

func NewHandlerToFile

func NewHandlerToFile(lvl Lvl, formatter *formatter, logfile string, doTrunc bool) (*handler, error)

NewHandlerToFile 创建一个 handler,日志写入到指定的文件

func NewHandlerToRotateLogs

func NewHandlerToRotateLogs(lvl Lvl, formatter *formatter, r *rotatelogs.RotateLogs) *handler

NewHandlerToRotateLogs 创建一个 handler,日志写入到 `*rotatelogs.RotateLogs` rotatelogs 是一个的滚动日志包,地址:https://github.com/lestrrat-go/file-rotatelogs

func NewHandlerToWriter

func NewHandlerToWriter(lvl Lvl, formatter *formatter, w io.Writer, disableColor bool) *handler

NewHandlerToWriter 创建一个 handler,日志写入到 io.Writer

func SetGlobalHandlers

func SetGlobalHandlers(handlers ...*handler)

SetGlobalHandlers 设置全部的处理器

func SetGlobalToGeneric

func SetGlobalToGeneric(stdLvl, fileLvl Lvl)

SetGlobalToGeneric 将全局对象设置为通用模式

func SetGlobalToSimple

func SetGlobalToSimple()

SetGlobalToSimple 将全局对象设置为简单模式

func Warning

func Warning(a ...interface{})

func Warningf

func Warningf(format string, a ...interface{})

Types

type Lvl

type Lvl uint8

Lvl 日志等级

const (
	LvlDebug Lvl = iota
	LvlInfo
	LvlWarning
	LvlError
	LvlFatal
)

func ParseLevel

func ParseLevel(s string) Lvl

ParseLevel 解析字符串为日志等级

func (Lvl) String

func (lvl Lvl) String() string

String 转换成字符串

type MixLog

type MixLog struct {
	// RuntimeCallerSkip 运行 `runtime.Caller` 跳过的堆栈数量,用于获取调用者的函数名和文件名
	RuntimeCallerSkip int
	// contains filtered or unexported fields
}

MixLog 日志记录器

func NewMixLog

func NewMixLog(handlers ...*handler) *MixLog

NewMixLog 创建一个日志记录器实例

func (*MixLog) Debug

func (m *MixLog) Debug(a ...interface{})

func (*MixLog) Debugf

func (m *MixLog) Debugf(format string, a ...interface{})

func (*MixLog) Error

func (m *MixLog) Error(a ...interface{})

func (*MixLog) Errorf

func (m *MixLog) Errorf(format string, a ...interface{})

func (*MixLog) Fatal

func (m *MixLog) Fatal(a ...interface{})

func (*MixLog) Fatalf

func (m *MixLog) Fatalf(format string, a ...interface{})

func (*MixLog) Info

func (m *MixLog) Info(a ...interface{})

func (*MixLog) Infof

func (m *MixLog) Infof(format string, a ...interface{})

func (*MixLog) SetHandler

func (m *MixLog) SetHandler(handlers ...*handler)

AddHandler 添加handler

func (*MixLog) Warning

func (m *MixLog) Warning(a ...interface{})

func (*MixLog) Warningf

func (m *MixLog) Warningf(format string, a ...interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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