如何禁用日志记录器

编程入门 行业动态 更新时间:2024-10-08 13:39:12
本文介绍了如何禁用日志记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一些使用log包的工具化代码.现在该关闭日志记录了,我无法确定如何关闭标准记录器.

I have some heavily instrumented code that makes use of the log package. Now it's come time to turn off the logging, and I can't determine how to turn off the standard logger.

我错过了什么吗?我应该在进行日志调用之前检查标志,还是在生产中将其注释掉?

Have I missed something? Should I be checking a flag before making log calls, or commenting them out in production?

推荐答案

要完全禁用日志,实际上最好调用log.SetFlags(0) Joril 并将输出设置为无操作io.Writer(即log.SetOutput(ioutil.Discard))

For completely disabling logs, it's actually better to call log.SetFlags(0)Joril and set the output to a no-op io.Writer (i.e., log.SetOutput(ioutil.Discard))

但即使在此之后,操作也会在 500-600 ns/op 1

But even after this, the operations will idle around 500-600 ns/op1

通过使用自定义的Logger实现并将所有功能实现为非操作,仍然可以将其缩短为( ns ns/op 约100 ns/op ),如此处(仅覆盖Println疯狂).

This can still be cut short (to around 100 ns/op) by using a custom Logger implementation, and implementing all the functions to be no-op -- as demonstrated here (only overriding Println for bervity).

所有这些的替代方案是使用具有级别的自定义日志记录框架,并将其设置为完全OFF.

The alternative to all these is to use a custom logging framework with levels and set it to complete OFF.

不过请注意,一种常用的日志记录库(日志)具有对性能的影响-在基准与 3K + ns/op 无关,

Note though, one of the commonly used library for logging (logrus) has performance implications -- the same can be found in the benchmarks where it perform with 3K+ ns/op, regardless.

偏见:从基准来看,库 go-logging 的执行水平将Level设置为-1时,使用自定义的Logger实现,而不考虑后端和格式

Biased opinion: from the benchmarks, the library go-logging performs in par with the custom Logger implementation when setting the Level to -1, regardless of the backend and formatting

(可以在此处找到基准源)

基准测试的输出如下:

testing: warning: no tests to run PASS BenchmarkGoLogging-4 1000000 2068 ns/op BenchmarkGoLoggingNullBackend-4 5000000 308 ns/op BenchmarkGoLoggingNullBackendWithFancyFormatter-4 3000000 435 ns/op BenchmarkGoLoggingOffLevel-4 20000000 109 ns/op BenchmarkGoLoggingNullBackendAndOffLevel-4 20000000 108 ns/op BenchmarkGoLoggingNullBackendWithFancyFormatterAndOffLevel-4 20000000 109 ns/op BenchmarkLog15-4 200000 7359 ns/op BenchmarkLog15WithDiscardHandler-4 2000000 922 ns/op BenchmarkLog15WithDiscardHandlerAndOffLevel-4 2000000 926 ns/op BenchmarkLog15WithNopLogger-4 20000000 108 ns/op BenchmarkLog15WithNopLoggerDiscardHandlerA-4 20000000 112 ns/op BenchmarkLog15WithNopLoggerAndDiscardHandlerAndOffLevel-4 20000000 112 ns/op BenchmarkLog-4 1000000 1217 ns/op BenchmarkLogIoDiscardWriter-4 2000000 724 ns/op BenchmarkLogIoDiscardWriterWithoutFlags-4 3000000 543 ns/op BenchmarkLogCustomNullWriter-4 2000000 731 ns/op BenchmarkLogCustomNullWriterWithoutFlags-4 3000000 549 ns/op BenchmarkNopLogger-4 20000000 113 ns/op BenchmarkNopLoggerWithoutFlags-4 20000000 112 ns/op BenchmarkLogrus-4 300000 3832 ns/op BenchmarkLogrusWithDiscardWriter-4 500000 3032 ns/op BenchmarkLogrusWithNullFormatter-4 500000 3814 ns/op BenchmarkLogrusWithPanicLevel-4 500000 3872 ns/op BenchmarkLogrusWithDiscardWriterAndPanicLevel-4 500000 3085 ns/op BenchmarkLogrusWithDiscardWriterAndNullFormatterAndPanicLevel-4 500000 3064 ns/op ok log-benchmarks 51.378s go test -bench . 62.17s user 3.90s system 126% cpu 52.065 total

#1: YMMV ,已在i7-4500U CPU @ 1.80GHz上进行了测试

#1: YMMV, tested on i7-4500U CPU @ 1.80GHz

更多推荐

如何禁用日志记录器

本文发布于:2023-11-25 21:28:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:记录器   日志

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!