Skip to content

Commit 3872ee4

Browse files
authored
feat(fxlog): Provided module (#26)
1 parent 87540be commit 3872ee4

File tree

15 files changed

+1103
-3
lines changed

15 files changed

+1103
-3
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- "trace"
3232
- "fxconfig"
3333
- "fxgenerate"
34+
- "fxlog"
3435
steps:
3536
- name: Checkout
3637
uses: actions/checkout@v3

.github/workflows/fxlog-ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "fxlog-ci"
2+
3+
on:
4+
push:
5+
branches:
6+
- "feat**"
7+
- "fix**"
8+
- "hotfix**"
9+
- "chore**"
10+
paths:
11+
- "fxlog/**.go"
12+
- "fxlog/go.mod"
13+
- "fxlog/go.sum"
14+
pull_request:
15+
types:
16+
- opened
17+
- synchronize
18+
- reopened
19+
branches:
20+
- main
21+
paths:
22+
- "fxlog/**.go"
23+
- "fxlog/go.mod"
24+
- "fxlog/go.sum"
25+
26+
jobs:
27+
ci:
28+
uses: ./.github/workflows/common-ci.yml
29+
secrets: inherit
30+
with:
31+
module: "fxlog"

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Yokai
22

3-
[![Go version](https://img.shields.io/badge/Go-1.20-blue)](https://go.dev/)
43
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4+
[![Go version](https://img.shields.io/badge/Go-1.20-blue)](https://go.dev/)
55
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR)](https://codecov.io/gh/ankorstore/yokai)
66

77
> Simple, modular, and observable Go framework.
@@ -16,14 +16,15 @@ Yokai's documentation will be available soon.
1616

1717
## Fx Modules
1818

19-
Yokai's `Fx modules` are the plugins for your Yokai application.
20-
2119
Yokai is using [Fx](https://github.com/uber-go/fx) for its plugin system.
2220

21+
Yokai's `Fx modules` are the plugins for your Yokai application.
22+
2323
| Fx Module | Description |
2424
|--------------------------|------------------------------------|
2525
| [fxconfig](fxconfig) | Fx module for [config](config) |
2626
| [fxgenerate](fxgenerate) | Fx module for [generate](generate) |
27+
| [fxlog](fxlog) | Fx module for [log](log) |
2728

2829
They can also be used in any [Fx](https://github.com/uber-go/fx) based Go application.
2930

fxlog/.golangci.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
run:
2+
timeout: 5m
3+
concurrency: 8
4+
5+
linters:
6+
enable:
7+
- asasalint
8+
- asciicheck
9+
- bidichk
10+
- bodyclose
11+
- containedctx
12+
- contextcheck
13+
- decorder
14+
- dogsled
15+
- dupl
16+
- durationcheck
17+
- errcheck
18+
- errchkjson
19+
- errname
20+
- errorlint
21+
- forbidigo
22+
- forcetypeassert
23+
- gocognit
24+
- goconst
25+
- gocritic
26+
- gocyclo
27+
- godot
28+
- godox
29+
- gofmt
30+
- goheader
31+
- gomoddirectives
32+
- gomodguard
33+
- goprintffuncname
34+
- gosec
35+
- gosimple
36+
- govet
37+
- grouper
38+
- importas
39+
- ineffassign
40+
- interfacebloat
41+
- logrlint
42+
- maintidx
43+
- makezero
44+
- misspell
45+
- nestif
46+
- nilerr
47+
- nilnil
48+
- nlreturn
49+
- nolintlint
50+
- nosprintfhostport
51+
- prealloc
52+
- predeclared
53+
- promlinter
54+
- reassign
55+
- staticcheck
56+
- tenv
57+
- thelper
58+
- tparallel
59+
- typecheck
60+
- unconvert
61+
- unparam
62+
- unused
63+
- usestdlibvars
64+
- whitespace

fxlog/README.md

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
# Fx Log Module
2+
3+
[![ci](https://github.com/ankorstore/yokai/actions/workflows/fxlog-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/fxlog-ci.yml)
4+
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/fxlog)](https://goreportcard.com/report/github.com/ankorstore/yokai/fxlog)
5+
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=fxlog)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxlog)
6+
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxlog)
7+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxlog)](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

Comments
 (0)