码迷,mamicode.com
首页 > 其他好文 > 详细

Log4Go 笔记

时间:2016-02-02 16:16:18      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

一、概念

在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 笔记

标签:

原文地址:http://www.cnblogs.com/loveyx/p/5177531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!