Seelog CustomReceiver
目录
Seelog is a powerful and easy-to-learn logging framework that provides functionality for flexible dispatching, filtering, and formatting log messages. It is natively written in the Go programming language.
安装:
go get -u github.com/cihub/seelog
例子:
package main</pre> <pre>import log "github.com/cihub/seelog"</pre> <pre>func main() { defer log.Flush() log.Info("Hello from Seelog!") }
配置文件:
logger.xml
<seelog> <outputs formatid="main"> <console /> <filter levels="error" formatid="err"> <file path="/path/to/log.log" /> </filter> </outputs> <formats> <format id="main" format="%Date(2006 Jan 02/3:04:05 MST) [%Level] %Msg%n"/> <format id="format2" format="%File %FullPath %RelFile %Msg%n"/> </formats> </seelog>
Custome receivers
参考:https://github.com/cihub/seelog/wiki/Custom-receivers
采用seelog.LoggerFromParamConfigAs…,载入xml配置文件以及CfgParseParams.CustomReceiverProducers.
首先定义基本的CustomReceiver类型:
type CustomReceiver struct { context SomeContext } <pre>func (cr *CustomReceiver) ReceiveMessage(message string, level log.LogLevel, context log.LogContextInterface) error { ctxLogger.Log(message, cr.context) return nil } func (cr *CustomReceiver) AfterParse(initArgs log.CustomReceiverInitArgs) error { return nil } func (cr *CustomReceiver) Flush() { } func (cr *CustomReceiver) Close() error { return nil }
func testLogWithContext(c SomeContext) { parserParams := &log.CfgParseParams{ CustomReceiverProducers: map[string]log.CustomReceiverProducer{ "myreceiver": func(log.CustomReceiverInitArgs) (log.CustomReceiver, error) { return &CustomReceiver{c}, nil }, }, } logger, err := log.LoggerFromParamConfigAsfile(cfgfile, parserParams) if err != nil { panic(err) } defer logger.Flush() err = log.ReplaceLogger(logger) if err != nil { panic(err) } log.Debug("Test1") log.Info("Test2") }
码字很辛苦,转载请注明来自ChenJiehua的《Seelog CustomReceiver》
评论