Log Module
Overview
Yokai provides a fxlog module, allowing your application to produce logs.
It wraps the log module, based on Zerolog.
Installation
The fxlog module is automatically loaded by Yokai's core.
When you use a Yokai application template
, you have nothing to install, it's ready to use.
Configuration
This module provides the possibility to configure:
- the
log level
(possible values:trace
,debug
,info
,warning
,error
,fatal
,panic
,no-level
ordisabled
) - the
log output
(possible values:noop
,stdout
ortest
)
Regarding the output:
stdout
: to send the log records toos.Stdout
(default)noop
: to void the log records viaos.Discard
console
: pretty prints logs record toos.Stdout
test
: to send the log records to the TestLogBuffer made available in the Fx container, for further assertions
Usage
This module makes available the Logger in Yokai dependency injection system.
It is built on top of Zerolog
, see its documentation for more details about available methods.
You can inject the logger where needed, but it's recommended to use the one carried by the context.Context
when possible (for automatic logs correlation).
Testing
This module provides the possibility to easily test your application logs, using the TestLogBuffer with modules.log.output=test
.
You can use the provided test assertion helpers in your tests:
AssertHasLogRecord
: to assert on exact attributes matchAssertHasNotLogRecord
: to assert on exact attributes non matchAssertContainLogRecord
: to assert on partial attributes matchAssertContainNotLogRecord
: to assert on partial attributes non match
and use Dump()
to print the current content of the TestLogBuffer.
For example:
package internal_test
import (
"testing"
"github.com/ankorstore/yokai/log"
"github.com/ankorstore/yokai/log/logtest"
"github.com/foo/bar/internal"
"go.uber.org/fx"
)
func TestExample(t *testing.T) {
var logBuffer logtest.TestLogBuffer
internal.RunTest(
t,
fx.Populate(&logBuffer),
fx.Invoke(func(logger *log.Logger) {
logger.Debug().Msg("test message")
}),
)
// print logs records
logBuffer.Dump()
// log assertion example
logtest.AssertHasLogRecord(
t,
logBuffer,
map[string]interface{}{
"level": "debug",
"message": "test message",
},
)
}