-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests, export how to set log level and how to get the logger in a…
… context
- Loading branch information
1 parent
03921c3
commit 73c46e6
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | ||
} |