去:禁用log.Logger? [英] Go: Disable a log.Logger?
问题描述
我有一些使用 log
包的大量代码。现在是时候关闭日志了,我无法确定如何关闭标准日志记录器。
我错过了什么吗?我应该在进行日志调用之前检查一个标志,或者在生产中对它们进行注释吗?为了完全禁用日志,它实际上是最好调用 log.SetFlags(0)
Joril ,并将输出设置为no-op io.Writer
(即 log)。 SetOutput(ioutil.Discard)
)
但即使在此之后,操作仍会在 500-600 ns / op 1
通过使用,仍然可以缩短到 100 ns / op 一个自定义的 Logger
实现,并将所有函数实现为no-op - 如演示这里(仅重写 Println
)。
所有这些的替代方案是使用带有级别的自定义日志记录框架并将其设置为OFF。
请注意,用于登录的库( logrus )性能影响 - 同样可以在基准测试,无论在哪里使用<3K + ns / op 执行。
偏见:从基准测试中,图书馆 go-logging 在在将 Level
设置为 -1 $ c $时,与自定义
Logger
无论如何后端和格式化
基准的产量如下:
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 735 9 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
Benchma rkNopLogger-4 20000000 113 ns / op
BenchmarkNoLoggerWithoutFlags-4 20000000 112 ns / op
BenchmarkLogrus-4 300000 3832 ns / op
BenchmarkLogrusWithDiscardWriter-4 500000 3032 ns / op
BenchmarkLogrusWullNullFormatter- 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
去测试平台。 62.17s用户3.90s系统126%cpu 52.065 total
#1: strong> YMMV ,在i7-4500U CPU @ 1.80GHz上测试
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?
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)
)
But even after this, the operations will idle around 500-600 ns/op1
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).
The alternative to all these is to use a custom logging framework with levels and set it to complete OFF.
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.
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
(the benchmark source can be found here)
the output of the benchmark is as follows:
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, tested on i7-4500U CPU @ 1.80GHz
这篇关于去:禁用log.Logger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!