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?
CodePudding user response:
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.
CodePudding user response:
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.
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)
.
If high performance is crucial for your app, consider using https://github.com/uber-go/zap (benchmarks provided in README.md)