Skip to content

Commit

Permalink
chore: Update log_test.go + CI workflow (#12)
Browse files Browse the repository at this point in the history
* tests: Update log_test.go

* chore: Create workflow go.yml
  • Loading branch information
fsamin authored Jun 21, 2023
1 parent 7638ea5 commit 750b638
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
56 changes: 20 additions & 36 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"testing"

"github.com/pkg/errors"
"github.com/rockbears/log"
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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() {
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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
}

0 comments on commit 750b638

Please sign in to comment.