栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何禁用日志记录器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何禁用日志记录器

要完全禁用日志,最好调用

log.SetFlags(0)
Joril并将输出设置为无操作
io.Writer
(例如
log.SetOutput(ioutil.Discard)

但即使在此之后,操作仍将闲置约500-600 ns / op 1

这仍然可切断短路(左右 为100 ns / OP )通过使用自定义

Logger
的实现,并实现所有功能进行任何操作-
这表现在这里(只覆盖
Println
了bervity)。

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

但是请注意,常用的日志记录库之一(logrus)会对性能产生影响 -在基准测试中,它的性能均达到 3K + ns / op ,无论如何。

偏见的意见:从基准,图书馆 去,记录在参数与自定义执行

Logger
执行设定时,
Level
-1
,无论后端和格式

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

基准的输出如下:

testing: warning: no tests to runPASSBenchmarkGoLogging-4 1000000          2068 ns/opBenchmarkGoLoggingNullBackend-4 5000000308 ns/opBenchmarkGoLoggingNullBackendWithFancyFormatter-4     3000000435 ns/opBenchmarkGoLoggingOffLevel-4   20000000109 ns/opBenchmarkGoLoggingNullBackendAndOffLevel-420000000108 ns/opBenchmarkGoLoggingNullBackendWithFancyFormatterAndOffLevel-4    20000000109 ns/opBenchmarkLog15-4      200000          7359 ns/opBenchmarkLog15WithDiscardHandler-4         2000000922 ns/opBenchmarkLog15WithDiscardHandlerAndOffLevel-4         2000000926 ns/opBenchmarkLog15WithNopLogger-4  20000000108 ns/opBenchmarkLog15WithNopLoggerDiscardHandlerA-4         20000000112 ns/opBenchmarkLog15WithNopLoggerAndDiscardHandlerAndOffLevel-4       20000000112 ns/opBenchmarkLog-4       1000000          1217 ns/opBenchmarkLogIoDiscardWriter-4   2000000724 ns/opBenchmarkLogIoDiscardWriterWithoutFlags-4  3000000543 ns/opBenchmarkLogCustomNullWriter-4  2000000731 ns/opBenchmarkLogCustomNullWriterWithoutFlags-4 3000000549 ns/opBenchmarkNopLogger-420000000113 ns/opBenchmarkNopLoggerWithoutFlags-4          20000000112 ns/opBenchmarkLogrus-4     300000          3832 ns/opBenchmarkLogrusWithDiscardWriter-4          500000          3032 ns/opBenchmarkLogrusWithNullFormatter-4          500000          3814 ns/opBenchmarkLogrusWithPanicLevel-4  500000          3872 ns/opBenchmarkLogrusWithDiscardWriterAndPanicLevel-4        500000          3085 ns/opBenchmarkLogrusWithDiscardWriterAndNullFormatterAndPanicLevel-4   500000          3064 ns/opok      log-benchmarks  51.378sgo test -bench .  62.17s user 3.90s system 126% cpu 52.065 total

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/471064.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号