Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/bricks/libs/log"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogLevelFlagDefault(t *testing.T) {
|
|
|
|
f := NewLogLevelFlag()
|
2023-03-21 15:34:16 +00:00
|
|
|
assert.Equal(t, log.LevelInfo, f.Level())
|
|
|
|
assert.Equal(t, "info", f.String())
|
Add structured logging infrastructure (#246)
New global flags:
* `--log-file FILE`: can be literal `stdout`, `stderr`, or a file name (default `stderr`)
* `--log-level LEVEL`: can be `error`, `warn`, `info`, `debug`, `trace`, or `disabled` (default `disabled`)
* `--log-format TYPE`: can be `text` or `json` (default `text`)
New functions in the `log` package take a `context.Context` and retrieve
the logger from said context.
Because we carry the logger in a context, adding
[attributes](https://pkg.go.dev/golang.org/x/exp/slog#hdr-Attrs_and_Values)
to the logger can be done as follows:
```go
ctx = log.NewContext(ctx, log.GetLogger(ctx).With("foo", "bar"))
```
2023-03-16 13:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogLevelFlagSetValid(t *testing.T) {
|
|
|
|
f := NewLogLevelFlag()
|
|
|
|
err := f.Set("info")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, log.LevelInfo, f.Level())
|
|
|
|
assert.Equal(t, "info", f.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogLevelFlagSetInvalid(t *testing.T) {
|
|
|
|
f := NewLogLevelFlag()
|
|
|
|
err := f.Set("invalid")
|
|
|
|
assert.ErrorContains(t, err, "accepted arguments are ")
|
|
|
|
}
|