|
| 1 | +# Log Module |
| 2 | + |
| 3 | +[](https://github.com/ankorstore/yokai/actions/workflows/log-ci.yml) |
| 4 | +[](https://goreportcard.com/report/github.com/ankorstore/yokai/log) |
| 5 | +[](https://app.codecov.io/gh/ankorstore/yokai/tree/main/log) |
| 6 | +[](https://pkg.go.dev/github.com/ankorstore/yokai/log) |
| 7 | + |
| 8 | +> Logging module based on [Zerolog](https://github.com/rs/zerolog). |
| 9 | +
|
| 10 | +<!-- TOC --> |
| 11 | +* [Installation](#installation) |
| 12 | +* [Documentation](#documentation) |
| 13 | + * [Usage](#usage) |
| 14 | + * [Context](#context) |
| 15 | + * [Testing](#testing) |
| 16 | +<!-- TOC --> |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```shell |
| 21 | +go get github.com/ankorstore/yokai/log |
| 22 | +``` |
| 23 | + |
| 24 | +## Documentation |
| 25 | + |
| 26 | +This module provides a [Logger](logger.go), offering all [Zerolog](https://github.com/rs/zerolog) methods. |
| 27 | + |
| 28 | +### Usage |
| 29 | + |
| 30 | +To create a `Logger`: |
| 31 | + |
| 32 | +```go |
| 33 | +package main |
| 34 | + |
| 35 | +import ( |
| 36 | + "os" |
| 37 | + |
| 38 | + "github.com/ankorstore/yokai/log" |
| 39 | + "github.com/rs/zerolog" |
| 40 | +) |
| 41 | + |
| 42 | +var logger, _ = log.NewDefaultLoggerFactory().Create() |
| 43 | + |
| 44 | +// equivalent to: |
| 45 | +var logger, _ = log.NewDefaultLoggerFactory().Create( |
| 46 | + log.WithServiceName("default"), // adds {"service":"default"} to log records |
| 47 | + log.WithLevel(zerolog.InfoLevel), // logs records with level >= info |
| 48 | + log.WithOutputWriter(os.Stdout), // sends logs records to stdout |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +To use the `Logger`: |
| 53 | + |
| 54 | +```go |
| 55 | +package main |
| 56 | + |
| 57 | +import ( |
| 58 | + "github.com/ankorstore/yokai/log" |
| 59 | +) |
| 60 | + |
| 61 | +func main() { |
| 62 | + logger, _ := log.NewDefaultLoggerFactory().Create() |
| 63 | + |
| 64 | + logger.Info().Msg("some message") // {"level:"info", "service":"default", "message":"some message"} |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +See [Zerolog](https://github.com/rs/zerolog) documentation for more details about available methods. |
| 69 | + |
| 70 | +### Context |
| 71 | + |
| 72 | +This module provides the `log.CtxLogger()` function that allow to extract the logger from a `context.Context`. |
| 73 | + |
| 74 | +If no logger is found in context, a [default](https://github.com/rs/zerolog/blob/master/ctx.go) Zerolog based logger will be used. |
| 75 | + |
| 76 | +### Testing |
| 77 | + |
| 78 | +This module provides a [TestLogBuffer](logtest/buffer.go), recording log records to be able to assert on them after logging: |
| 79 | + |
| 80 | +```go |
| 81 | +package main |
| 82 | + |
| 83 | +import ( |
| 84 | + "fmt" |
| 85 | + |
| 86 | + "github.com/ankorstore/yokai/log" |
| 87 | + "github.com/ankorstore/yokai/log/logtest" |
| 88 | +) |
| 89 | + |
| 90 | +func main() { |
| 91 | + buffer := logtest.NewDefaultTestLogBuffer() |
| 92 | + |
| 93 | + logger, _ := log.NewDefaultLoggerFactory().Create(log.WithOutputWriter(buffer)) |
| 94 | + |
| 95 | + logger.Info().Msg("some message example") |
| 96 | + |
| 97 | + // test on attributes exact matching |
| 98 | + hasRecord, _ := buffer.HasRecord(map[string]interface{}{ |
| 99 | + "level": "info", |
| 100 | + "message": "some message example", |
| 101 | + }) |
| 102 | + |
| 103 | + fmt.Printf("has record: %v", hasRecord) // has record: true |
| 104 | + |
| 105 | + // test on attributes partial matching |
| 106 | + containRecord, _ := buffer.ContainRecord(map[string]interface{}{ |
| 107 | + "level": "info", |
| 108 | + "message": "message", |
| 109 | + }) |
| 110 | + |
| 111 | + fmt.Printf("contain record: %v", containRecord) // contain record: true |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +You can also use the provided [test assertion helpers](logtest/assert.go) in your tests: |
| 116 | +- `AssertHasLogRecord`: to assert on exact attributes match |
| 117 | +- `AssertHasNotLogRecord`: to assert on exact attributes non match |
| 118 | +- `AssertContainLogRecord`: to assert on partial attributes match |
| 119 | +- `AssertContainNotLogRecord`: to assert on partial attributes non match |
| 120 | + |
| 121 | +```go |
| 122 | +package main_test |
| 123 | + |
| 124 | +import ( |
| 125 | + "fmt" |
| 126 | + "testing" |
| 127 | + |
| 128 | + "github.com/ankorstore/yokai/log" |
| 129 | + "github.com/ankorstore/yokai/log/logtest" |
| 130 | +) |
| 131 | + |
| 132 | +func TestLogger(t *testing.T) { |
| 133 | + buffer := logtest.NewDefaultTestLogBuffer() |
| 134 | + |
| 135 | + logger, _ := log.NewDefaultLoggerFactory().Create(log.WithOutputWriter(buffer)) |
| 136 | + |
| 137 | + logger.Info().Msg("some message example") |
| 138 | + |
| 139 | + // assertion success |
| 140 | + logtest.AssertHasLogRecord(t, buffer, map[string]interface{}{ |
| 141 | + "level": "info", |
| 142 | + "message": "some message example", |
| 143 | + }) |
| 144 | + |
| 145 | + // assertion success |
| 146 | + logtest.AssertHasNotLogRecord(t, buffer, map[string]interface{}{ |
| 147 | + "level": "info", |
| 148 | + "message": "some invalid example", |
| 149 | + }) |
| 150 | + |
| 151 | + // assertion success |
| 152 | + logtest.AssertContainLogRecord(t, buffer, map[string]interface{}{ |
| 153 | + "level": "info", |
| 154 | + "message": "message", |
| 155 | + }) |
| 156 | + |
| 157 | + // assertion success |
| 158 | + logtest.AssertContainNotLogRecord(t, buffer, map[string]interface{}{ |
| 159 | + "level": "info", |
| 160 | + "message": "invalid", |
| 161 | + }) |
| 162 | +} |
| 163 | +``` |
0 commit comments