简体   繁体   English

Golang/Temporal 日志连接与 string.format?

[英]Golang/Temporal logging concatenation vs string.format?

Basically my question is,基本上我的问题是,

between之间

logger.Info("Activity done. Conversion of : )" + param.FileRecordId + "successful. File can be downloaded under : " + file.FileRecordId)

and

logger.Info(fmt.Sprintf("Activity done. Conversion of : %s successful. File can be downloaded under %s", param.FileRecordId, file.FileRecordId))

which one is more efficient?哪个更有效率? I know String.Format is faster but here it is being used inside logger.Info.. is it still more efficient?我知道 String.Format 更快,但在这里它被用于 logger.Info .. 它仍然更有效吗?

Yes, but in the grand scheme of things on a fairly infinitesimal level.是的,但在一个相当微不足道的层面上的宏伟计划。 It would be very unlikely this would be the first place starting to optimize your code base to pinch pennies or speed up performance.这不太可能是第一个开始优化您的代码库以节省便士或提高性能的地方。 Also you could change what you are trying to make more efficient as in more efficient for other developers to read, to debug, to maintain, etc. This is where it can get more subjective than a clear cut answer.您还可以更改您试图提高效率的内容,以提高其他开发人员阅读、调试、维护等的效率。这是比明确答案更主观的地方。

On that note, I would personally use fmt.Sprintf as the better choice for verbose logging as it makes understanding the message being logged and the values being passed easier when I read it.在那一点上,我个人会使用 fmt.Sprintf 作为详细日志记录的更好选择,因为它可以让我在阅读时更容易理解正在记录的消息和传递的值。

You can write a simple benchmark ( https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go ) to check which way is more efficient.您可以编写一个简单的基准测试( https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go )来检查哪种方式更有效。

But for logging I guess you should choose not the most efficient way, but the most convenient and easy to read one.但是对于日志记录,我想您应该选择的不是最有效的方式,而是最方便且易于阅读的方式。 For me it is f method like logger.Infof("Activity done. Conversion of: %s successful. File can be downloaded under %s", param.FileRecordId, file.FileRecordId) .对我来说,它是像logger.Infof("Activity done. Conversion of: %s successful. File can be downloaded under %s", param.FileRecordId, file.FileRecordId)这样的f方法。

If high performance is crucial for your app, consider using https://github.com/uber-go/zap (benchmarks provided in README.md)如果高性能对您的应用程序至关重要,请考虑使用https://github.com/uber-go/zap (README.md 中提供的基准)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM