Log4Go 笔记

一、概念

在java、.net平台有非常有名的日志类,golang 也有对应的版本

安装: go get github.com/alecthomas/log4go

二、日志级别

type Level int
const (
    FINEST Level = iota
    FINE
    DEBUG
    TRACE
    INFO
    WARNING
    ERROR
    CRITICAL
)

可以看到作者定义了一个 Level 类型来定义日志级别,使用的时候注意。

三、LogWriter

1. ConsoleLogWriter :控制台输出日志

logger1 := log4go.Logger{}
logger1.AddFilter("logger1", log4go.INFO, log4go.NewConsoleLogWriter())
logger1.Log(log4go.INFO, "logger1:", "hello log4go")

2. FileLogWriter :文件输出日志

logger3 := log4go.Logger{}
logger3.AddFilter("logger3", log4go.ERROR, log4go.NewFileLogWriter("file.log", false))
logger3.Log(log4go.ERROR, "logger3:", "hello log4go")

3. FormatLogWriter : 格式化输出日志

format 解释:

Known format codes: %T - Time (15:04:05 MST) %t - Time (15:04) %D - Date (2006/01/02) %d - Date (01/02/06) %L - Level (FNST, FINE, DEBG, TRAC, WARN, EROR, CRIT) %S - Source %M - Message Ignores unknown formats

Recommended: "[%D %T] [%L] (%S) %M"

logger2 := log4go.Logger{}
std := os.Stdout
logger2.AddFilter("logger2", log4go.DEBUG, log4go.NewFormatLogWriter(std, "[%D %T] [%L] (%S) %M"))
logger2.Log(log4go.DEBUG, "logger2:", "hello log4go")

4. SocketLogWriter

额… 这个等用到再补吧

5. NewXMLLogWriter : xml格式输出日志

logger4 := log4go.Logger{}
logger4.AddFilter("logger4", log4go.ERROR, log4go.NewXMLLogWriter("file.xml", true))
logger4.Log(log4go.ERROR, "logger4:", "hello log4go")
<log created="2016/02/02 14:17:10 CST">
<record level="EROR">
    <timestamp>2016/02/02 14:17:10 CST</timestamp>
    <source>logger4:</source>
    <message>hello log4go</message>
</record>

你可能感兴趣的:(Log4Go 笔记)