英文:
Limitations of GO lang log package
问题
我正在Go语言中实现日志记录器。我正在考虑使用logrus来实现。我想了解内置的log包的限制。
我知道,如果我们想要将日志写入文件或实现滚动文件日志等功能,我们需要手动添加此功能。除此之外,还有其他限制需要选择外部日志包吗?
英文:
I am implementing logger in Go. I am thinking of using logrus for this. I wanted to understand limitations of the built in log package.
I know that if we want to write logs to file or implement rolling file logs etc we would need to add this feature manually. Is there any other limitations for which we have to choose an external log package?
答案1
得分: 2
记录日志到文件可以有多种方式。
按照12因素应用程序的方式(不仅仅是12因素应用程序的方式,但12因素应用程序强调以这种方式进行记录),可以将日志记录到STDOUT和/或STDERR,然后将其导向其他位置进行部署。这也使得开发变得容易。此外,如果您正在使用容器来部署程序,它们会将容器的STDOUT和STDERR日志记录到文件中。
标准库log
包有两种改变输出到文件的方式。
一种方式如下:
log.SetOutput(<实现io.Writer接口的内容,可能是os.File>)
log.Println("一些消息")
另一种方式是:
logger := log.New(<实现io.Writer接口的内容,可能是os.File>, <前缀字符串>, <一些标志,参见https://golang.org/pkg/log/#pkg-constants>)
logger.Println("一些消息")
第二种选项还可以通过每个级别拥有自己的记录器来实现不同的日志级别(例如INFO、DEBUG、WARN、ERROR等)。
然而,logrus已经为您提供了很多这方面的功能,所以如果您只是想快速实现日志记录,那么logrus可能是您最好的选择。
英文:
Logging to file can be done multiple ways.
The 12-factor-app way (not just the 12 factor app way, but 12 factor apps make a point of doing it this way) of doing it would be to log to STDOUT and/or STDERR and then piping it elsewhere for deployment. This also makes development easy. Also if you're using something like containers to deploy your program, they log the container's STDOUT and STDERR to a file to begin with.
The std lib log
package has two ways to change output to a file.
One way is as follows:
log.SetOutput(<something that implements io.Writer, probably os.File>)
log.Println("some message")
The other is:
logger := log.New(<something that implements io.Writer, probably os.File>, <a prefix String>, <some flag, see https://golang.org/pkg/log/#pkg-constants>)
logger.Println("some message")
The second option is also how you can implement different log levels (i.e. INFO, DEBUG, WARN, ERROR, etc) by having each level be its own logger.
However logrus gives you a lot of this already for you, so that is probably your best bet if you just want to have logging implemented quickly.
答案2
得分: 1
使用内置的log
包记录到文件不是问题,你可以使用log.SetOutput()
或Logger.SetOutput()
来设置一个目标io.Writer
,而不是默认的os.Stderr
,例如一个文件*os.File
。
缺少的并且经常被期望的是分级日志记录(例如INFO
、WARN
、DEBUG
、ERROR
等)。关于这个问题,可以阅读博客文章Dave Cheney: Let's talk about logging。
除非这些包“愿意”合作(例如提供SetLogger()
函数),否则你也无法强制使用指定的log.Logger
。
滚动日志文件也是一个缺失的功能。
另一方面,你也可以很容易地“扩展”标准记录器,以便例如记录到MongoDB,详细信息请参见https://stackoverflow.com/questions/40396499/go-create-io-writer-inteface-for-logging-to-mongodb-database。使用MongoDB,你还可以使用Capped collection,它将隐式地为你提供“滚动文件”的功能。
英文:
Logging to file using the builtin log
package is not an issue, you can use log.SetOutput()
or Logger.SetOutput()
to set a destination io.Writer
other than the default os.Stderr
, for example a file *os.File
.
What's missing and often whished for is leveled logging (e.g. INFO
, WARN
, DEBUG
, ERROR
etc.). For a reasoning, read blog post Dave Cheney: Let's talk about logging.
You also can't enforce a specific log.Logger
to be used by designated packages unless those packages "willing" to cooperate (e.g. they provide a SetLogger()
function).
Rolling log files is also a missing feature.
On the other hand, it's also very easy to "extend" the standard logger to log into MongoDB for example, for details see https://stackoverflow.com/questions/40396499/go-create-io-writer-inteface-for-logging-to-mongodb-database. Using MongoDB you can also use a Capped collection which will implicitly give you a "rolling file" functionality.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论