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. Can someone help me with it.
Asked
Active
Viewed 117 times
1 Answers
0
You could take a look at the zaptest package:
Package zaptest provides a variety of helpers for testing log 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

Matteo
- 37,680
- 11
- 100
- 115
-
Yes I checked that too. But in my case the function which prints logs doesn't accept any arguments of type *zap.Logger. The function resides in external library. – saisree Jan 12 '23 at 13:30