douyanjing0822 2015-12-09 13:31
浏览 12

具有文件记录功能的可配置记录器

I'm looking for logging library in Go, something similar like log4j in java.

  1. As I understood build-in logger from 'log' package have restricted possibility in configurations and log levels.

  2. Maybe someone used one of https://godoc.org/github.com/op/go-logging, https://github.com/cihub/seelog?

  3. What about concurrency issues? What is the best way to store logger in system? One per program or create loggers every time?

Thanks!

  • 写回答

1条回答 默认 最新

  • douzhang6496 2016-11-08 15:57
    关注

    The std lib log package handles a lot of this for you. You can either log to STDOUT/STDERR or set the output of the log package itself by doing as follows:

    log.SetOutput(<something that implements io.Writer>)
    log.Println("some message")
    

    And depending on what that io.Writer is (i.e. an os.File, net.TCPConn, net.UDPConn, etc) it will log "some message" to it.

    This can also be done on a per logger basis like so

    logger := log.New(<something that implements io.Writer>, <log prefix string>, <log flags https://golang.org/pkg/log/#pkg-constants>)
    logger.Println("Some message")
    

    This example will accomplish the same thing and add a prefix to each log. The second example is a struct instead of functions from the package, so that will have to be shared across Goroutines correrctly. This example can implement log levels by having each level be its own logger instance.

    It's also worth taking a look at logrus https://github.com/Sirupsen/logrus it handles a lot of the boilerplate for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题