[英]Testing logs printed using other external library in golang
My application uses other external library (which uses zap library) to print audit-logs and now I want to test the printed audit-logs in my golang testing file.我的应用程序使用其他外部库(使用 zap 库)来打印审计日志,现在我想在我的 golang 测试文件中测试打印的审计日志。 Can someone help me with it.有人可以帮我吗?
You could take a look at the zaptest package:你可以看看zaptest package:
Package zaptest provides a variety of helpers for testing log output. Package zaptest提供了多种测试日志的helpers output。
As example, in your test:例如,在您的测试中:
func Test_zap(t *testing.T) {
t.Run("Handle log message", func(t *testing.T) {
// Given
observedZapCore, observedLogs := observer.New(zap.InfoLevel)
observedLogger := zap.New(observedZapCore)
// When
myFunction(observedLogger)
// Then
require.Equal(t, 1, observedLogs.Len())
firstLog := observedLogs.All()[0]
assert.Equal(t, "log myFunction", firstLog.Message)
})
}
where myfunction is我的功能在哪里
func myFunction(logger *zap.Logger) {
logger.Info("log myFunction")
}
Check also this interesting article about that另请查看这篇有趣的文章
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.