Golang的一个很小结构日志库

网友投稿 686 2022-11-05

Golang的一个很小结构日志库

Golang的一个很小结构日志库

log

A small structured logging library for Golang. Documentation is available at GoDoc.

Releases

Master branch is the stable production-ready branch.

Features

Only standard library dependenciesOutput configurations can be modified at runtimeDefault formatter formats log messages as JSON encoded string. Custom formatters can be used.

Example

package mainimport ( "fmt" "github.com/szxp/log" "os")func main() { // register an io.Writer in the DefaultRouter // everything that is not a debug message will be written to stdout log.Output{ Id: "stdout1", Writer: os.Stdout, Formatter: nil, Filter: log.Not(log.Eq("level", "debug")), }.Register() // optional error callback in the DefaultRouter for debugging purposes log.OnError(func(err error, fields log.Fields, o log.Output) { fmt.Printf("%v: %+v: %+v", err, fields, o) }) // create a logger logger := log.LoggerConfig{ // TimeFormat: time.RFC3339, // optional, see standard time package for custom formats Name: "loggername", // optional, name of the logger UTC: true, // optional, use UTC rather than local time zone FileLine: log.ShortFileLine, // optional, include file and line number SortFields: true, // optional, sort field keys in increasing order Router: nil, // optional, defaults to log.DefaultRouter }.NewLogger() // produce some log messages logger.Log(log.Fields{ "level": "info", "user": log.Fields{ "id": 1, "username": "admin", }, "activated": true, "projects": []string{"p1", "p2", "p3"}, }) logger.Log(log.Fields{ "level": "debug", "details": "...", }) // output reconfiguration in the DefaultRouter // for example disable filtering on Stdout log.Output{ Id: "stdout1", Writer: os.Stdout, Formatter: nil, Filter: nil, }.Register() // Output: // {"activated":true,"file":"example_test.go:44","level":"info","logger":"loggername","projects":["p1","p2","p3"],"user":{"id":1,"username":"admin"}}}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:gevent:轻量级进程内的并发编程
下一篇:【多线程】 死锁是什么? 如何检测代码中的死锁?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~