logging - 如何禁用 log.Logger

logging - 如何禁用 log.Logger

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

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

这仍然可以通过使用自定义实现来缩短(到100 ns/opLogger左右) ,并将所有功能实现为无操作 - 如此处所示(仅为Printlnbervity 覆盖)。

所有这些的替代方法是使用具有级别的自定义日志框架并将其设置为完全关闭。

但请注意,常用的日志记录库之一 ( logrus ) 具有性能影响——无论如何,在它使用3K+ ns/op执行的基准测试中都可以找到相同的内容。

有偏见的意见:从基准测试来看,无论后端和格式如何,库go-logging在设置toLogger时的性能与自定义实现相当Level-1

(基准源可以在这里找到)

基准的输出如下:

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 上测试

相关推荐