glog

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 6 Imported by: 0

README

日志打印

基于fmt.Println()上的二次开发,显示内容更丰富。

使用说明

  • 使用 go get -u github.com/trako/gutils 下载或更新

  • 目前只支持控制台模式

  • 使用示例:

glog.Info("hello world!") // 打印INFO级别的日志

glog.FuncDepth(1).Info("hello world!") // 打印INFO级别的日志,并且函数名为上一层

  • 关闭字体颜色示例:

glog.EnableColor(false)

  • 关闭函数名:

glog.EnableFuncName(false)

  • 设置最低日志级别:

glog.SetMinLevel(glog.LevelInfo)

测试

  • 单元测试
go test -count=1 -v .
  • 压力测试
go test -test.bench=".*"

测验结果

2021-01-22 09:09:19.936 [TRAC] [log_test.go:13 BenchmarkTrace] size: 100000
2021-01-22 09:09:19.936 [TRAC] [log_test.go:14 BenchmarkTrace] cost: 1.827090677s
  100000             18271 ns/op

Documentation

Index

Constants

View Source
const (
	LevelTrace = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

Variables

View Source
var LevelMap = map[string]int{
	"FATA": LevelFatal,
	"ERRO": LevelError,
	"WARN": LevelWarn,
	"INFO": LevelInfo,
	"DEBU": LevelDebug,
	"TRAC": LevelTrace,
}

日志等级和描述映射关系

View Source
var StrLevelMap = map[Level]string{
	LevelFatal: "FATA",
	LevelError: "ERRO",
	LevelWarn:  "WARN",
	LevelInfo:  "INFO",
	LevelDebug: "DEBU",
	LevelTrace: "TRAC",
}

Functions

func Debug

func Debug(f interface{}, v ...interface{})

func EnableColor

func EnableColor(enable bool)

EnableColor 是否开启字体颜色

func EnableFuncName

func EnableFuncName(enable bool)

EnableFuncName 是否显示函数名

func EnablePrefix

func EnablePrefix(enable bool)

EnablePrefix 是否开启前缀,比如[INFO]

func Error

func Error(f interface{}, v ...interface{})

func Info

func Info(f interface{}, v ...interface{})

func Panic added in v0.0.5

func Panic(f interface{}, v ...interface{})

func SetFlag

func SetFlag(flag int)

SetFlag 参考 log.LstdFlags

func SetMinLevel

func SetMinLevel(minLevel Level)

SetMinLevel 设置最低日志级别,低于该级别的将不会打印 level值参考 LevelDebug

func Trace added in v0.0.5

func Trace(f interface{}, v ...interface{})

func Warn

func Warn(f interface{}, v ...interface{})

Types

type Level

type Level int

type Logger

type Logger struct {
	Color     bool  `json:"color"`      // 是否开启字体颜色
	Prefix    bool  `json:"prefix"`     // 是否开启前缀,比如[INFO]
	Func      bool  `json:"func"`       // 是否显示函数名
	Flag      int   `json:"flag"`       // 属性,参考log.LstdFlags
	MinLevel  Level `json:"min_level"`  // 最低日志级别,参考Level类型
	CallDepth int   `json:"call_depth"` // 调用函数深度,特意提供可支持外部修改
}

func FuncDepth

func FuncDepth(depth int) *Logger

FuncDepth 设置函数调用层级,从级别0开始

func NewLogger

func NewLogger() *Logger

func (*Logger) Debug

func (p *Logger) Debug(f interface{}, v ...interface{})

func (*Logger) Error

func (p *Logger) Error(f interface{}, v ...interface{})

func (*Logger) Info

func (p *Logger) Info(f interface{}, v ...interface{})

func (*Logger) Panic added in v0.0.5

func (p *Logger) Panic(f interface{}, v ...interface{})

func (*Logger) Trace added in v0.0.5

func (p *Logger) Trace(f interface{}, v ...interface{})

ln - true表示自动换行

func (*Logger) Warn

func (p *Logger) Warn(f interface{}, v ...interface{})

Jump to

Keyboard shortcuts

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