Skip to content

Commit

Permalink
add tests, export how to set log level and how to get the logger in a…
Browse files Browse the repository at this point in the history
… context
  • Loading branch information
StephanHCB committed Mar 26, 2022
1 parent 03921c3 commit 73c46e6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package go_autumn_logging_zerolog

import (
"context"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func SetLogLevel(level zerolog.Level) {
zerolog.SetGlobalLevel(level)
}

func AddLoggerToCtx(ctx context.Context) context.Context {
return log.Logger.WithContext(ctx)
}
52 changes: 52 additions & 0 deletions usage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package go_autumn_logging_zerolog

import (
"context"
"errors"
aulogging "github.com/StephanHCB/go-autumn-logging"
"testing"
)

func TestPlaintextLogging(t *testing.T) {
SetupPlaintextLogging()

ctx := AddLoggerToCtx(context.Background())
err := errors.New("some wonderful error")

aulogging.Logger.NoCtx().Error().Print("no context print error severity, no error object")
aulogging.Logger.Ctx(ctx).Error().WithErr(err).Print("with context print error severity, with error object")

aulogging.Logger.NoCtx().Warn().Print("no context print warn severity")
aulogging.Logger.Ctx(ctx).Warn().Print("with context print warn severity")

aulogging.Logger.NoCtx().Info().Print("no context print info severity")
aulogging.Logger.Ctx(ctx).Info().Print("with context print info severity")

aulogging.Logger.NoCtx().Debug().Print("no context print debug severity (should not show)")
aulogging.Logger.Ctx(ctx).Debug().Print("with context print debug severity (should not show)")

aulogging.Logger.NoCtx().Trace().Print("no context print trace severity (should not show)")
aulogging.Logger.Ctx(ctx).Trace().Print("with context print trace severity (should not show)")
}

func TestJsonLogging(t *testing.T) {
SetupJsonLogging("my-service")

ctx := AddLoggerToCtx(context.Background())
err := errors.New("some wonderful error")

aulogging.Logger.NoCtx().Error().Print("no context print error severity, no error object")
aulogging.Logger.Ctx(ctx).Error().WithErr(err).Print("with context print error severity, with error object")

aulogging.Logger.NoCtx().Warn().Print("no context print warn severity")
aulogging.Logger.Ctx(ctx).Warn().Print("with context print warn severity")

aulogging.Logger.NoCtx().Info().Print("no context print info severity")
aulogging.Logger.Ctx(ctx).Info().Print("with context print info severity")

aulogging.Logger.NoCtx().Debug().Print("no context print debug severity (should not show)")
aulogging.Logger.Ctx(ctx).Debug().Print("with context print debug severity (should not show)")

aulogging.Logger.NoCtx().Trace().Print("no context print trace severity (should not show)")
aulogging.Logger.Ctx(ctx).Trace().Print("with context print trace severity (should not show)")
}

0 comments on commit 73c46e6

Please sign in to comment.