# README
Fx Log Module
Installation
go get github.com/ankorstore/yokai/fxlog
Documentation
Dependencies
This module is intended to be used alongside the fxconfig module.
Loading
To load the module in your Fx application:
package main
import (
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxlog"
"github.com/ankorstore/yokai/log"
"go.uber.org/fx"
)
func main() {
fx.New(
fxconfig.FxConfigModule, // load the module dependency
fxlog.FxLogModule, // load the module
fx.Invoke(func(logger *log.Logger) { // invoke the logger
logger.Info().Msg("message")
}),
).Run()
}
If needed, you can also configure Fx to use this logger for its own event logs:
package main
import (
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxlog"
"go.uber.org/fx"
)
func main() {
fx.New(
fxconfig.FxConfigModule, // load the module dependency
fxlog.FxLogModule, // load the module
fx.WithLogger(fxlog.NewFxEventLogger), // configure Fx event logging
).Run()
}
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
# ./configs/config.yaml
app:
name: app
env: dev
version: 0.1.0
debug: false
modules:
log:
level: info # by default
output: stdout # by default
Notes:
- the config
app.name
(or env varAPP_NAME
) will be used in each log recordservice
field:{"service":"app"}
- if the config
app.debug=true
(or env varAPP_DEBUG=true
), thedebug
level will be used, no matter given configuration - if the config
app.env=test
(or env varAPP_ENV=test
), thetest
output will be used, no matter given configuration
Override
By default, the log.Logger
is created by the DefaultLoggerFactory.
If needed, you can provide your own factory and override the module:
package main
import (
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxlog"
"github.com/ankorstore/yokai/log"
"go.uber.org/fx"
)
type CustomLoggerFactory struct{}
func NewCustomLoggerFactory() log.LoggerFactory {
return &CustomLoggerFactory{}
}
func (f *CustomLoggerFactory) Create(options ...log.LoggerOption) (*log.Logger, error) {
return &log.Logger{...}, nil
}
func main() {
fx.New(
fxconfig.FxConfigModule, // load the module dependency
fxlog.FxLogModule, // load the module
fx.Decorate(NewCustomLoggerFactory), // override the module with a custom factory
fx.Invoke(func(logger *log.Logger) { // invoke the custom logger
logger.Info().Msg("custom message")
}),
).Run()
}
Testing
This module provides the possibility to easily test your log records, using the TestLogBuffer with modules.log.output=test
.
# ./configs/config.test.yaml
app:
name: test
modules:
log:
output: test # to send logs to test buffer
You can then test:
package main_test
import (
"testing"
"github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/fxconfig"
"github.com/ankorstore/yokai/fxlog"
"github.com/ankorstore/yokai/log"
"github.com/ankorstore/yokai/log/logtest"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
func TestLogger(t *testing.T) {
t.Setenv("APP_NAME", "test")
t.Setenv("APP_ENV", "test")
var buffer logtest.TestLogBuffer
fxtest.New(
t,
fx.NopLogger,
fxconfig.FxConfigModule,
fxlog.FxLogModule,
fx.Invoke(func(logger *log.Logger) {
logger.Debug().Msg("test message")
}),
fx.Populate(&buffer), // extracts the TestLogBuffer from the Fx container
).RequireStart().RequireStop()
// assertion success
logtest.AssertHasLogRecord(t, buffer, map[string]interface{}{
"level": "debug",
"service": "test",
"message": "test message",
})
}
See the log
module testing documentation for more details.
# Functions
NewFxEventLogger returns a new [NewFxEventLogger] from a provided [log.Logger].
NewFxLogger returns a [log.Logger].
# Constants
ModuleName is the module name.
# Variables
FxLogModule is the [Fx] log module.
# Structs
FxEventLogger is a logger compatible with [Fx], decorating [log.Logger].
FxLogParam allows injection of the required dependencies in [NewFxLogger].