|
| 1 | +# Fx Log Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/fxlog-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/fxlog) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxlog) |
| 6 | +[](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxlog) |
| 7 | +[](https://pkg.go.dev/github.com/ankorstore/yokai/fxlog) |
| 8 | + |
| 9 | +> [Fx](https://uber-go.github.io/fx/) module for [log](https://github.com/ankorstore/yokai/tree/main/log). |
| 10 | +
|
| 11 | +<!-- TOC --> |
| 12 | +* [Installation](#installation) |
| 13 | +* [Documentation](#documentation) |
| 14 | + * [Dependencies](#dependencies) |
| 15 | + * [Loading](#loading) |
| 16 | + * [Configuration](#configuration) |
| 17 | + * [Override](#override) |
| 18 | + * [Testing](#testing) |
| 19 | +<!-- TOC --> |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +```shell |
| 24 | +go get github.com/ankorstore/yokai/fxlog |
| 25 | +``` |
| 26 | + |
| 27 | +## Documentation |
| 28 | + |
| 29 | +### Dependencies |
| 30 | + |
| 31 | +This module is intended to be used alongside the [fxconfig](https://github.com/ankorstore/yokai/tree/main/fxconfig) module. |
| 32 | + |
| 33 | +### Loading |
| 34 | + |
| 35 | +To load the module in your Fx application: |
| 36 | + |
| 37 | +```go |
| 38 | +package main |
| 39 | + |
| 40 | +import ( |
| 41 | + "github.com/ankorstore/yokai/config" |
| 42 | + "github.com/ankorstore/yokai/fxconfig" |
| 43 | + "github.com/ankorstore/yokai/fxlog" |
| 44 | + "github.com/ankorstore/yokai/log" |
| 45 | + "go.uber.org/fx" |
| 46 | +) |
| 47 | + |
| 48 | +func main() { |
| 49 | + fx.New( |
| 50 | + fxconfig.FxConfigModule, // load the module dependency |
| 51 | + fxlog.FxLogModule, // load the module |
| 52 | + fx.Invoke(func(logger *log.Logger) { // invoke the logger |
| 53 | + logger.Info().Msg("message") |
| 54 | + }), |
| 55 | + ).Run() |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +If needed, you can also configure [Fx](https://uber-go.github.io/fx/) to use this logger for its own event logs: |
| 60 | + |
| 61 | +```go |
| 62 | +package main |
| 63 | + |
| 64 | +import ( |
| 65 | + "github.com/ankorstore/yokai/config" |
| 66 | + "github.com/ankorstore/yokai/fxconfig" |
| 67 | + "github.com/ankorstore/yokai/fxlog" |
| 68 | + "go.uber.org/fx" |
| 69 | +) |
| 70 | + |
| 71 | +func main() { |
| 72 | + fx.New( |
| 73 | + fxconfig.FxConfigModule, // load the module dependency |
| 74 | + fxlog.FxLogModule, // load the module |
| 75 | + fx.WithLogger(fxlog.NewFxEventLogger), // configure Fx event logging |
| 76 | + ).Run() |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Configuration |
| 81 | + |
| 82 | +This module provides the possibility to configure: |
| 83 | + |
| 84 | +- the `log level` (possible values: `trace`, `debug`, `info`, `warning`, `error`, `fatal`, `panic`, `no-level` or `disabled`) |
| 85 | +- the `log output` (possible values: `noop`, `stdout` or `test`) |
| 86 | + |
| 87 | +Regarding the output: |
| 88 | + |
| 89 | +- `stdout`: to send the log records to `os.Stdout` (default) |
| 90 | +- `noop`: to void the log records via `os.Discard` |
| 91 | +- `console`: [pretty prints](https://github.com/rs/zerolog#pretty-logging) logs record to `os.Stdout` |
| 92 | +- `test`: to send the log records to the [TestLogBuffer](https://github.com/ankorstore/yokai/blob/main/log/logtest/buffer.go) made available in the Fx container, for further assertions |
| 93 | + |
| 94 | +```yaml |
| 95 | +# ./configs/config.yaml |
| 96 | +app: |
| 97 | + name: app |
| 98 | + env: dev |
| 99 | + version: 0.1.0 |
| 100 | + debug: false |
| 101 | +modules: |
| 102 | + log: |
| 103 | + level: info # by default |
| 104 | + output: stdout # by default |
| 105 | +``` |
| 106 | +
|
| 107 | +Notes: |
| 108 | +
|
| 109 | +- the config `app.name` (or env var `APP_NAME`) will be used in each log record `service` field: `{"service":"app"}` |
| 110 | +- if the config `app.debug=true` (or env var `APP_DEBUG=true`), the `debug` level will be used, no matter given configuration |
| 111 | +- if the config `app.env=test` (or env var `APP_ENV=test`), the `test` output will be used, no matter given configuration |
| 112 | + |
| 113 | +### Override |
| 114 | + |
| 115 | +By default, the `log.Logger` is created by the [DefaultLoggerFactory](https://github.com/ankorstore/yokai/blob/main/log/factory.go). |
| 116 | + |
| 117 | +If needed, you can provide your own factory and override the module: |
| 118 | + |
| 119 | +```go |
| 120 | +package main |
| 121 | +
|
| 122 | +import ( |
| 123 | + "github.com/ankorstore/yokai/fxconfig" |
| 124 | + "github.com/ankorstore/yokai/fxlog" |
| 125 | + "github.com/ankorstore/yokai/log" |
| 126 | + "go.uber.org/fx" |
| 127 | +) |
| 128 | +
|
| 129 | +type CustomLoggerFactory struct{} |
| 130 | +
|
| 131 | +func NewCustomLoggerFactory() log.LoggerFactory { |
| 132 | + return &CustomLoggerFactory{} |
| 133 | +} |
| 134 | +
|
| 135 | +func (f *CustomLoggerFactory) Create(options ...log.LoggerOption) (*log.Logger, error) { |
| 136 | + return &log.Logger{...}, nil |
| 137 | +} |
| 138 | +
|
| 139 | +func main() { |
| 140 | + fx.New( |
| 141 | + fxconfig.FxConfigModule, // load the module dependency |
| 142 | + fxlog.FxLogModule, // load the module |
| 143 | + fx.Decorate(NewCustomLoggerFactory), // override the module with a custom factory |
| 144 | + fx.Invoke(func(logger *log.Logger) { // invoke the custom logger |
| 145 | + logger.Info().Msg("custom message") |
| 146 | + }), |
| 147 | + ).Run() |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### Testing |
| 152 | + |
| 153 | +This module provides the possibility to easily test your log records, using the [TestLogBuffer](https://github.com/ankorstore/yokai/blob/main/log/logtest/buffer.go) with `modules.log.output=test`. |
| 154 | + |
| 155 | +```yaml |
| 156 | +# ./configs/config.test.yaml |
| 157 | +app: |
| 158 | + name: test |
| 159 | +modules: |
| 160 | + log: |
| 161 | + output: test # to send logs to test buffer |
| 162 | +``` |
| 163 | + |
| 164 | +You can then test: |
| 165 | + |
| 166 | +```go |
| 167 | +package main_test |
| 168 | +
|
| 169 | +import ( |
| 170 | + "testing" |
| 171 | +
|
| 172 | + "github.com/ankorstore/yokai/config" |
| 173 | + "github.com/ankorstore/yokai/fxconfig" |
| 174 | + "github.com/ankorstore/yokai/fxlog" |
| 175 | + "github.com/ankorstore/yokai/log" |
| 176 | + "github.com/ankorstore/yokai/log/logtest" |
| 177 | + "go.uber.org/fx" |
| 178 | + "go.uber.org/fx/fxtest" |
| 179 | +) |
| 180 | +
|
| 181 | +func TestLogger(t *testing.T) { |
| 182 | + t.Setenv("APP_NAME", "test") |
| 183 | + t.Setenv("APP_ENV", "test") |
| 184 | +
|
| 185 | + var buffer logtest.TestLogBuffer |
| 186 | +
|
| 187 | + fxtest.New( |
| 188 | + t, |
| 189 | + fx.NopLogger, |
| 190 | + fxconfig.FxConfigModule, |
| 191 | + fxlog.FxLogModule, |
| 192 | + fx.Invoke(func(logger *log.Logger) { |
| 193 | + logger.Debug().Msg("test message") |
| 194 | + }), |
| 195 | + fx.Populate(&buffer), // extracts the TestLogBuffer from the Fx container |
| 196 | + ).RequireStart().RequireStop() |
| 197 | +
|
| 198 | + // assertion success |
| 199 | + logtest.AssertHasLogRecord(t, buffer, map[string]interface{}{ |
| 200 | + "level": "debug", |
| 201 | + "service": "test", |
| 202 | + "message": "test message", |
| 203 | + }) |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +See the `log` module testing [documentation](https://github.com/ankorstore/yokai/tree/main/log#testing) for more details. |
0 commit comments