log

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

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

Go to latest
Published: May 13, 2018 License: MIT Imports: 14 Imported by: 18

README

log Build Status Godoc license

log 提供一 个类型 slf4j 的标准接口,同时也提供了一个基本的实现,可以自定义日志格式,输出各种类型的日志,如 csv/json/xml,同时支持 Tag(TraceId/RequestId)。

Usage

安装:go get -v -u github.com/arstd/log

使用:

package main

import "github.com/arstd/log"

func main() {
    log.Debugf("this is a test message, %d", 1111)

	format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02 15:04:05.000000", log.TagToken,
		log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)

	format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
		"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Infof("this is a test message, %d", 1111)

	format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
		"2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
	log.SetFormat(format)
	log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)
}

日志输出:

2016-01-16 20:28:34 debug examples/main.go:10 this is a test message, 1111
2016-01-16 20:28:34.280601 6ba7b814-9dad-11d1-80b4-00c04fd430c8 info examples/main.go:15 this is a test message, 1111
{"date": "2016-01-16", "time": "20:28:34.28", "level": "info", "file": "examples/main.go", "line": 20, "log": "this is a test message, 1111"}
<log><date>2016-01-16</date><time>20:28:34.280</time><level>info</level><file>examples/main.go</file><line>25</line><msg>this is a test message, 1111</msg><log>

更多用法 examples 着色示例 color.png

Go Doc and API

所有可调用的接口 API 和 文档都在 log.go

log/Printer/Standard

Golang 不同于 Java,非面向对象语言(没有继承,只有组合,不能把组合实例赋给被组合的实例,即 Java 说的 子对象 赋给 父对象),为了方便使用,很多函数都是包封装的,无需创建 struct ,就可以直接调用。 (一般把裸露的方法称为函数,结构体和其他类型的方法才称为某某的方法)

log 包也一样,使用时,无需 new ,直接用。log 包有所有级别的函数可以调用,所有函数最终都调用了 print 函数。print 函数又调用了包内部变量的 std 的 Print 方法。这个 std 是一个 Printer 接 口类型,定义了打印接口。用不同的实现改变 std 就可以打印出不同格式的日志,也可以输出到不同位置。 (这个接口貌似还没有抽象好,再想想)

Printer 有个基本的实现 Standard,如果不改变,默认使用这个实现打印日志。

Standard 实现了的 Printer 接口,把日志打印到 Stdout。

性能测试

环境:MacBookPro 15,4 核 8 线程 16G 内存

实际测试结果(把日志重定向到文件): 该库平均每秒可输出 16w 行日志; Go 语言标准库平均每秒输出 36.5w 行日志。

模板方式输出日志对性能有一定影响,其他 New Record 等也可能造成性能下降。但是实现上比标准库略微 复杂,输出格式可以灵活配置,所以整体上可以接受,后期随着对 Go 语言的学习更深入再不断优化。

TODO

  • 测试是否支持各种格式的日期
  • 处理秒和毫秒,如1:1:02.9
  • 实现日志文件按一定规则自动滚动

Documentation

Overview

log 实现了一个像 slf4j(Simple Logging Facade for Java) 一样标准的可以自定义级别的 log 库。这个 log 库的需要完成得任务就是提供一个标准统一的接口,同时也提供了一个基本的实现。 使用这个 log 库打印日志,可以随时切换日志级别,可以更换不同的 logger 实现,以打印不同格式的日 志,也可以改变日志输出位置,输出到数据库、消息队列等。

安装:

go get -v -u github.com/arstd/log

使用:

package main

import "github.com/arstd/log"

func main() {
    log.Debugf("this is a test message, %d", 1111)

    format := fmt.Sprintf("%s %s %s %s:%d %s", "2006-01-02 15:04:05.000000", log.TagToken,
    	log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
    log.SetFormat(format)
    log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)

    format = fmt.Sprintf(`{"date": "%s", "time": "%s", "level": "%s", "file": "%s", "line": %d, "log": "%s"}`,
    	"2006-01-02", "15:04:05.999", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
    log.SetFormat(format)
    log.Infof("this is a test message, %d", 1111)

    format = fmt.Sprintf(`<log><date>%s</date><time>%s</time><level>%s</level><file>%s</file><line>%d</line><msg>%s</msg><log>`,
    	"2006-01-02", "15:04:05.000", log.LevelToken, log.ProjectToken, log.LineToken, log.MessageToken)
    log.SetFormat(format)
    log.Tinfof("6ba7b814-9dad-11d1-80b4-00c04fd430c8", "this is a test message, %d", 1111)
}

日志输出:

2016-01-16 20:28:34 debug examples/main.go:10 this is a test message, 1111
2016-01-16 20:28:34.280601 6ba7b814-9dad-11d1-80b4-00c04fd430c8 info examples/main.go:15 this is a test message, 1111
{"date": "2016-01-16", "time": "20:28:34.28", "level": "info", "file": "examples/main.go", "line": 20, "log": "this is a test message, 1111"}
<log><date>2016-01-16</date><time>20:28:34.280</time><level>info</level><file>examples/main.go</file><line>25</line><msg>this is a test message, 1111</msg><log>

Index

Constants

View Source
const (
	LevelToken   string = "info"
	TagToken            = "tag"
	PathToken           = "/go/src/github.com/arstd/log/examples/main.go"
	PackageToken        = "github.com/arstd/log/examples/main.go"
	ProjectToken        = "examples/main.go"
	FileToken           = "main.go"
	LineToken    int    = 88
	MessageToken string = "message"
)

可以用这些串和日期、时间(包含毫秒数)任意组合,拼成各种格式的日志,如 csv/json/xml

View Source
const DefaultFormat = "2006-01-02 15:04:05.000 info examples/main.go:88 message"

DefaultFormat 默认日志格式

View Source
const DefaultFormatTag = "2006-01-02 15:04:05.000 tag info examples/main.go:88 message"

DefaultFormatTag 默认日志格式带标签

Variables

View Source
var Labels = [...]string{"all", "trace", "debug", "info", "warn", "error", "panic", "fatal", "print", "stack"}

Labels 每个级别对应的标签

Functions

func Colorized

func Colorized(c bool)

Colorized 输出日志是否着色,默认着色,如果设置的级别高于 debug,不着色

func CtxDebug

func CtxDebug(ctx context.Context, m ...interface{})

func CtxDebugf

func CtxDebugf(ctx context.Context, format string, m ...interface{})

func CtxError

func CtxError(ctx context.Context, m ...interface{})

func CtxErrorf

func CtxErrorf(ctx context.Context, format string, m ...interface{})

func CtxFatal

func CtxFatal(ctx context.Context, m ...interface{})

func CtxFatalf

func CtxFatalf(ctx context.Context, format string, m ...interface{})

func CtxFatalln

func CtxFatalln(ctx context.Context, m ...interface{})

func CtxInfo

func CtxInfo(ctx context.Context, m ...interface{})

func CtxInfof

func CtxInfof(ctx context.Context, format string, m ...interface{})

func CtxPanic

func CtxPanic(ctx context.Context, m ...interface{})

func CtxPanicf

func CtxPanicf(ctx context.Context, format string, m ...interface{})

func CtxPanicln

func CtxPanicln(ctx context.Context, m ...interface{})

func CtxPrint

func CtxPrint(ctx context.Context, m ...interface{})

func CtxPrintf

func CtxPrintf(ctx context.Context, format string, m ...interface{})

func CtxPrintln

func CtxPrintln(ctx context.Context, m ...interface{})

func CtxStack

func CtxStack(ctx context.Context, m ...interface{})

func CtxWarn

func CtxWarn(ctx context.Context, m ...interface{})

func CtxWarnf

func CtxWarnf(ctx context.Context, format string, m ...interface{})

func Debug

func Debug(m ...interface{})

func Debugf

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

func Error

func Error(m ...interface{})

func Errorf

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

func Errorn

func Errorn(m ...interface{})

Errorn check last argument, if error but nil, no print log

func Errornf

func Errornf(format string, m ...interface{})

Errorn check last argument, if error but nil, no print log

func ExtactDateTime

func ExtactDateTime(format string) (dateFmt, timeFmt string)

ExtactDateTime 抽取日期和时间格式字符串串

func Fatal

func Fatal(m ...interface{})

func Fatalf

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

func Fataln

func Fataln(m ...interface{})

func Fatalnf

func Fatalnf(format string, m ...interface{})

func Info

func Info(m ...interface{})

func Infof

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

func IsDebugEnabled

func IsDebugEnabled() bool

func IsErrorEnabled

func IsErrorEnabled() bool

func IsFatalEnabled

func IsFatalEnabled() bool

func IsInfoEnabled

func IsInfoEnabled() bool

func IsPanicEnabled

func IsPanicEnabled() bool

func IsPrintEnabled

func IsPrintEnabled() bool

func IsStackEnabled

func IsStackEnabled() bool

func IsTraceEnabled

func IsTraceEnabled() bool

判断各种级别的日志是否会被输出

func IsWarnEnabled

func IsWarnEnabled() bool

func JSON

func JSON(m ...interface{})

先转换成 JSON 格式,然后打印

func JSONIndent

func JSONIndent(m ...interface{})

func Json

func Json(m ...interface{})

func JsonIndent

func JsonIndent(m ...interface{})

func Panic

func Panic(m ...interface{})

func Panicf

func Panicf(format string, m ...interface{})

func Print

func Print(m ...interface{})

func Printf

func Printf(format string, m ...interface{})

func SetFormat

func SetFormat(format string)

SetFormat 改变日志格式

func SetLevel

func SetLevel(l Level)

SetLevel 设置日志级别

func SetLevelString

func SetLevelString(s string)

func SetOutputLevel

func SetOutputLevel(l interface{})

func SetPrinter

func SetPrinter(p Printer)

SetPrinter 切换 Printer 实现

func SetWriter

func SetWriter(w io.Writer)

SetWriter 改变输出位置,通过这个接口,可以实现日志文件按时间或按大小滚动

func Stack

func Stack(m ...interface{})

func Stackf

func Stackf(format string, m ...interface{})

func Tdebug

func Tdebug(tag string, m ...interface{})

func Tdebugf

func Tdebugf(tag string, format string, m ...interface{})

func Terror

func Terror(tag string, m ...interface{})

func Terrorf

func Terrorf(tag string, format string, m ...interface{})

func Terrorn

func Terrorn(tag string, m ...interface{})

Errorn check last argument, if error but nil, no print log

func Terrornf

func Terrornf(tag string, format string, m ...interface{})

Errorn check last argument, if error but nil, no print log

func Tfatal

func Tfatal(tag string, m ...interface{})

func Tfatalf

func Tfatalf(tag string, format string, m ...interface{})

func Tfataln

func Tfataln(tag string, m ...interface{})

func Tfatalnf

func Tfatalnf(tag string, format string, m ...interface{})

func Tinfo

func Tinfo(tag string, m ...interface{})

func Tinfof

func Tinfof(tag string, format string, m ...interface{})

func Tpanic

func Tpanic(tag string, m ...interface{})

func Tpanicf

func Tpanicf(tag string, format string, m ...interface{})

func Tprint

func Tprint(tag string, m ...interface{})

func Tprintf

func Tprintf(tag string, format string, m ...interface{})

func Trace

func Trace(m ...interface{})

打印日志

func TraceCtx

func TraceCtx(ctx context.Context, method string, format string, m ...interface{}) (string, string, time.Time)

TraceCtx 方法入口打印日志

func TraceIn

func TraceIn(tag string, method string, format string, m ...interface{}) (string, string, time.Time)

TraceIn 方法入口打印日志

func TraceOut

func TraceOut(tag string, method string, startTime time.Time)

TraceOut 方法退出记录下消耗时间

func Tracef

func Tracef(format string, m ...interface{})

按一定格式打印日志

func Tstack

func Tstack(tag string, m ...interface{})

func Tstackf

func Tstackf(tag string, format string, m ...interface{})

func Ttrace

func Ttrace(tag string, m ...interface{})

打印日志时带上 tag

func Ttracef

func Ttracef(tag string, format string, m ...interface{})

按一定格式打印日志,并在打印日志时带上 tag

func Twarn

func Twarn(tag string, m ...interface{})

func Twarnf

func Twarnf(tag string, format string, m ...interface{})

func Warn

func Warn(m ...interface{})

func Warnf

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

Types

type Level

type Level uint8

Level 日志级别

const (
	Lall Level = iota // 等同于 Larace

	Ltrace
	Ldebug // 默认日志级别,方便开发
	Linfo
	Lwarn
	Lerror
	Lpanic // panic 打印错误栈,但是可以 recover
	Lfatal // fatal 表明严重错误,程序直接退出,慎用

	// 提供这个级别日志,方便在无论何种情况下,都打印必要信息,比如服务启动信息
	Lprint
	Lstack // 打印堆栈信息
)

所有日志级别常量,级别越高,日志越重要,级别越低,日志越详细

func GetLevel

func GetLevel() (l Level)

GetLevel 返回设置的日志级别

func ValueOfLevel

func ValueOfLevel(vstr string) (v Level, err error)

ValueOfLevel 字符串转换成 Level, "debug" => Ldebug

func (*Level) MarshalJSON

func (v *Level) MarshalJSON() ([]byte, error)

MarshalJSON 便于 JSON 解析

func (Level) String

func (v Level) String() string

String 返回日志标签

func (*Level) UnmarshalJSON

func (v *Level) UnmarshalJSON(b []byte) error

UnmarshalJSON 便于 JSON 解析

type Printer

type Printer interface {

	// 所有方法最终归为这个方法,真正打印日志
	Tprintf(l Level, tag string, format string, m ...interface{})

	// SetFormat 改变日志格式
	SetFormat(format string)

	// 输出日志是否着色,默认着色
	Colorized(c bool)

	// SetWriter 改变输出流
	SetWriter(w io.Writer)
}

Printer 定义了打印接口

type Standard

type Standard struct {
	// contains filtered or unexported fields
}

Standard 日志输出基本实现

func NewStandard

func NewStandard(w io.Writer, format string) *Standard

NewStandard 返回标准实现

func (*Standard) Colorized

func (s *Standard) Colorized(c bool)

Colorized 输出日志是否着色,默认着色

func (*Standard) SetFormat

func (s *Standard) SetFormat(format string)

SetFormat 改变日志格式

func (*Standard) SetWriter

func (s *Standard) SetWriter(w io.Writer)

SetWriter 改变输出流

func (*Standard) Tprintf

func (s *Standard) Tprintf(l Level, tag string, format string, m ...interface{})

Tprintf 打印日志

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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