Documentation ¶
Overview ¶
log 实现了一个像 slf4j(Simple Logging Facade for Java) 一样标准的可以自定义级别的 log 库。这个 log 库的需要完成得任务就是提供一个标准统一的接口,同时也提供了一个基本的实现。 使用这个 log 库打印日志,可以随时切换日志级别,可以更换不同的 logger 实现,以打印不同格式的日 志,也可以改变日志输出位置,输出到数据库、消息队列等。
安装:
go get -v -u github.com/omigo/log
使用:
package main import "github.com/omigo/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
- Variables
- func Colorized(c bool)
- func Debug(m ...interface{})
- func Debugf(format string, m ...interface{})
- func Error(m ...interface{})
- func Errorf(format string, m ...interface{})
- func Errorn(m ...interface{})
- func Errornf(format string, m ...interface{})
- func ExtactDateTime(format string) (dateFmt, timeFmt string)
- func Fatal(m ...interface{})
- func Fatalf(format string, m ...interface{})
- func Fataln(m ...interface{})
- func Fatalnf(format string, m ...interface{})
- func Info(m ...interface{})
- func Infof(format string, m ...interface{})
- func IsDebugEnabled() bool
- func IsErrorEnabled() bool
- func IsFatalEnabled() bool
- func IsInfoEnabled() bool
- func IsPanicEnabled() bool
- func IsPrintEnabled() bool
- func IsStackEnabled() bool
- func IsTraceEnabled() bool
- func IsWarnEnabled() bool
- func JSON(m ...interface{})
- func JSONIndent(m ...interface{})
- func Json(m ...interface{})
- func JsonIndent(m ...interface{})
- func Panic(m ...interface{})
- func Panicf(format string, m ...interface{})
- func Print(m ...interface{})
- func Printf(format string, m ...interface{})
- func SetFormat(format string)
- func SetLevel(l Level)
- func SetLevelString(s string)
- func SetPrinter(p Printer)
- func SetWriter(w io.Writer)
- func Stack(m ...interface{})
- func Stackf(format string, m ...interface{})
- func Tdebug(tag string, m ...interface{})
- func Tdebugf(tag string, format string, m ...interface{})
- func Terror(tag string, m ...interface{})
- func Terrorf(tag string, format string, m ...interface{})
- func Terrorn(tag string, m ...interface{})
- func Terrornf(tag string, format string, m ...interface{})
- func Tfatal(tag string, m ...interface{})
- func Tfatalf(tag string, format string, m ...interface{})
- func Tfataln(tag string, m ...interface{})
- func Tfatalnf(tag string, format string, m ...interface{})
- func Tinfo(tag string, m ...interface{})
- func Tinfof(tag string, format string, m ...interface{})
- func Tpanic(tag string, m ...interface{})
- func Tpanicf(tag string, format string, m ...interface{})
- func Tprint(tag string, m ...interface{})
- func Tprintf(tag string, format string, m ...interface{})
- func Trace(m ...interface{})
- func Tracef(format string, m ...interface{})
- func Tstack(tag string, m ...interface{})
- func Tstackf(tag string, format string, m ...interface{})
- func Ttrace(tag string, m ...interface{})
- func Ttracef(tag string, format string, m ...interface{})
- func Twarn(tag string, m ...interface{})
- func Twarnf(tag string, format string, m ...interface{})
- func Warn(m ...interface{})
- func Warnf(format string, m ...interface{})
- type Level
- type Printer
- type Standard
Constants ¶
View Source
const ( LevelToken string = "info" TagToken = "tag" PathToken = "/go/src/github.com/omigo/log/examples/main.go" PackageToken = "github.com/omigo/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 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 ¶
ExtactDateTime 抽取日期和时间格式字符串串
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 IsWarnEnabled ¶
func IsWarnEnabled() bool
func JSONIndent ¶
func JSONIndent(m ...interface{})
func JsonIndent ¶
func JsonIndent(m ...interface{})
func SetLevelString ¶
func SetLevelString(s string)
func Terrorn ¶
func Terrorn(tag string, m ...interface{})
Errorn check last argument, if error but nil, no print log
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 ValueOfLevel ¶
ValueOfLevel 字符串转换成 Level, "debug" => Ldebug
func (*Level) UnmarshalJSON ¶
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 定义了打印接口
Click to show internal directories.
Click to hide internal directories.