From 750b6384932583988699e2d14d76459bff97b2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Wed, 21 Jun 2023 17:26:11 +0200 Subject: [PATCH] chore: Update log_test.go + CI workflow (#12) * tests: Update log_test.go * chore: Create workflow go.yml --- .github/workflows/go.yml | 28 ++++++++++++++++++++ log_test.go | 56 ++++++++++++++-------------------------- 2 files changed, 48 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/go.yml diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..3e9a76d --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,28 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.19 + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/log_test.go b/log_test.go index faf7e03..f497033 100644 --- a/log_test.go +++ b/log_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "testing" "github.com/pkg/errors" "github.com/rockbears/log" @@ -24,7 +25,7 @@ func init() { func ExampleNewLogrusWrapper() { // Init the wrapper log.Factory = log.NewLogrusWrapper - log.UnregisterField(log.FieldSourceLine) + log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) // Init the logrus logger logrus.StandardLogger().SetLevel(logrus.InfoLevel) @@ -44,15 +45,15 @@ func ExampleNewLogrusWrapper() { log.Error(ctx, "this is error") // Output: - // level=info msg="this is \"info\"" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go - // level=warning msg="this is warn" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go - // level=error msg="this is error" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go + // level=info msg="this is \"info\"" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log + // level=warning msg="this is warn" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log + // level=error msg="this is error" asset=ExampleWithLogrus caller=github.com/rockbears/log_test.ExampleNewLogrusWrapper component=rockbears/log } func ExampleNewStdWrapper() { // Init the wrapper log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true}) - log.UnregisterField(log.FieldSourceLine) + log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) // Init the context ctx := context.Background() ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") @@ -62,9 +63,9 @@ func ExampleNewStdWrapper() { log.Warn(ctx, "this is warn") log.Error(ctx, "this is error") // Output: - // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is "info" - // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is warn - // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is error + // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is "info" + // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is warn + // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapper][component=rockbears/log] this is error } func ExampleNewZapWrapper() { @@ -79,7 +80,7 @@ func ExampleNewZapWrapper() { } core := zapcore.NewCore(zapcore.NewJSONEncoder(encoderCfg), os.Stdout, zap.InfoLevel) log.Factory = log.NewZapWrapper(zap.New(core)) - log.UnregisterField(log.FieldSourceLine) + log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) // Init the context ctx := context.Background() ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") @@ -89,44 +90,27 @@ func ExampleNewZapWrapper() { log.Warn(ctx, "this is warn") log.Error(ctx, "this is error") // Output: - // {"level":"info","msg":"this is \"info\"","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log","source_file":"/Users/fsamin/go/src/github.com/rockbears/log/log_test.go"} - // {"level":"warn","msg":"this is warn","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log","source_file":"/Users/fsamin/go/src/github.com/rockbears/log/log_test.go"} - // {"level":"error","msg":"this is error","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log","source_file":"/Users/fsamin/go/src/github.com/rockbears/log/log_test.go"} + // {"level":"info","msg":"this is \"info\"","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log"} + // {"level":"warn","msg":"this is warn","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log"} + // {"level":"error","msg":"this is error","asset":"ExampleNewZapWrapper","caller":"github.com/rockbears/log_test.ExampleNewZapWrapper","component":"rockbears/log"} } -func ExampleErrorWithStackTrace() { +func TestErrorWithStackTrace(t *testing.T) { // Init the wrapper - log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true}) - log.UnregisterField(log.FieldSourceLine) + log.Factory = log.NewTestingWrapper(t) + log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) // Init the context ctx := context.Background() ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") ctx = context.WithValue(ctx, fieldAsset, "ExampleErrorWithStackTrace") log.ErrorWithStackTrace(ctx, fmt.Errorf("this is an error")) log.ErrorWithStackTrace(ctx, errors.WithStack(fmt.Errorf("this is an error"))) - // Output: - // [ERROR] [asset=ExampleErrorWithStackTrace][caller=github.com/rockbears/log_test.ExampleErrorWithStackTrace][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is an error - // [ERROR] [asset=ExampleErrorWithStackTrace][caller=github.com/rockbears/log_test.ExampleErrorWithStackTrace][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go][stack_trace=this is an error - // github.com/rockbears/log_test.ExampleErrorWithStackTrace - // /Users/fsamin/go/src/github.com/rockbears/log/log_test.go:106 - // testing.runExample - // /Users/fsamin/Applications/go/src/testing/run_example.go:64 - // testing.runExamples - // /Users/fsamin/Applications/go/src/testing/example.go:44 - // testing.(*M).Run - // /Users/fsamin/Applications/go/src/testing/testing.go:1505 - // main.main - // _testmain.go:51 - // runtime.main - // /Users/fsamin/Applications/go/src/runtime/proc.go:255 - // runtime.goexit - // /Users/fsamin/Applications/go/src/runtime/asm_amd64.s:1581] this is an error } func ExampleNewStdWrapperAndSkip() { // Init the wrapper log.Factory = log.NewStdWrapper(log.StdWrapperOptions{Level: log.LevelInfo, DisableTimestamp: true}) - log.UnregisterField(log.FieldSourceLine) + log.UnregisterField(log.FieldSourceLine, log.FieldSourceFile) // Init the context ctx := context.Background() ctx = context.WithValue(ctx, fieldComponent, "rockbears/log") @@ -138,7 +122,7 @@ func ExampleNewStdWrapperAndSkip() { log.Skip(fieldAsset, "ExampleNewStdWrapper") log.Info(ctx, "this log should not be displayed because is should be skipped") // Output: - // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is "info" - // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is warn - // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log][source_file=/Users/fsamin/go/src/github.com/rockbears/log/log_test.go] this is error + // [INFO] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is "info" + // [WARN] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is warn + // [ERROR] [asset=ExampleNewStdWrapper][caller=github.com/rockbears/log_test.ExampleNewStdWrapperAndSkip][component=rockbears/log] this is error }