From a80346b1a92673a51fc010a467a6dbb13a7ee113 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Sat, 19 Jan 2019 14:22:07 -0500 Subject: [PATCH] gkelog: Add some convenience functions for logging with severity --- emitter/gkelog/emitter.go | 13 -------- emitter/gkelog/emitter_test.go | 14 ++++++++ emitter/gkelog/severity.go | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 emitter/gkelog/severity.go diff --git a/emitter/gkelog/emitter.go b/emitter/gkelog/emitter.go index 1649d8f..215e84b 100644 --- a/emitter/gkelog/emitter.go +++ b/emitter/gkelog/emitter.go @@ -30,19 +30,6 @@ var ( spanKey = contextKey("span") ) -// Severity levels -const ( - SeverityDefault = "DEFAULT" // The log entry has no assigned severity level. - SeverityDebug = "DEBUG" // Debug or trace information. - SeverityInfo = "INFO" // Routine information, such as ongoing status or performance. - SeverityNotice = "NOTICE" // Normal but significant events, such as start up, shut down, or a configuration change. - SeverityWarning = "WARNING" // Warning events might cause problems. - SeverityError = "ERROR" // Error events are likely to cause problems. - SeverityCritical = "CRITICAL" // Critical events cause more severe problems or outages. - SeverityAlert = "ALERT" // A person must take an action immediately. - SeverityEmergency = "EMERGENCY" // One or more systems are unusable. -) - // WithSeverity returns a copy of parent with the specified severity value. func WithSeverity(parent context.Context, severity string) context.Context { return context.WithValue(parent, severityKey, severity) diff --git a/emitter/gkelog/emitter_test.go b/emitter/gkelog/emitter_test.go index 4ffeebd..af5091d 100644 --- a/emitter/gkelog/emitter_test.go +++ b/emitter/gkelog/emitter_test.go @@ -75,6 +75,20 @@ func TestSeverity(t *testing.T) { } } +func TestLogSeverity(t *testing.T) { + b := &bytes.Buffer{} + ctx := context.Background() + l := alog.New(alog.WithEmitter(Emitter(WithWriter(b))), zeroTimeOpt) + + LogInfo(ctx, l, "test") + + want := `{"time":"0001-01-01T00:00:00Z", "severity":"INFO", "message":"test"}` + "\n" + got := b.String() + if got != want { + t.Errorf("got:\n%s\nwant:\n%s", got, want) + } +} + func TestRequest(t *testing.T) { b := &bytes.Buffer{} ctx := context.Background() diff --git a/emitter/gkelog/severity.go b/emitter/gkelog/severity.go new file mode 100644 index 0000000..960b2d3 --- /dev/null +++ b/emitter/gkelog/severity.go @@ -0,0 +1,60 @@ +package gkelog + +import ( + "context" + "fmt" + + "github.com/vimeo/alog/v3" +) + +// Severity levels +const ( + SeverityDefault = "DEFAULT" // The log entry has no assigned severity level. + SeverityDebug = "DEBUG" // Debug or trace information. + SeverityInfo = "INFO" // Routine information, such as ongoing status or performance. + SeverityNotice = "NOTICE" // Normal but significant events, such as start up, shut down, or a configuration change. + SeverityWarning = "WARNING" // Warning events might cause problems. + SeverityError = "ERROR" // Error events are likely to cause problems. + SeverityCritical = "CRITICAL" // Critical events cause more severe problems or outages. + SeverityAlert = "ALERT" // A person must take an action immediately. + SeverityEmergency = "EMERGENCY" // One or more systems are unusable. +) + +// Separate private function so that LogSeverity and the other logs functions +// will have the same stack frame depth and thus use the same calldepth value. +// See https://golang.org/pkg/runtime/#Caller and +// https://godoc.org/github.com/vimeo/alog#Logger.Output +func logSeverity(ctx context.Context, logger *alog.Logger, s string, f string, v ...interface{}) { + ctx = WithSeverity(ctx, s) + logger.Output(ctx, 3, fmt.Sprintf(f, v...)) +} + +// LogSeverity writes a log entry using the specified severity +func LogSeverity(ctx context.Context, logger *alog.Logger, severity string, f string, v ...interface{}) { + logSeverity(ctx, logger, severity, f, v...) +} + +// LogDebug writes a log entry using SeverityDebug +func LogDebug(ctx context.Context, logger *alog.Logger, f string, v ...interface{}) { + logSeverity(ctx, logger, SeverityDebug, f, v...) +} + +// LogInfo writes a log entry using SeverityInfo +func LogInfo(ctx context.Context, logger *alog.Logger, f string, v ...interface{}) { + logSeverity(ctx, logger, SeverityInfo, f, v...) +} + +// LogWarning writes a log entry using SeverityWarning +func LogWarning(ctx context.Context, logger *alog.Logger, f string, v ...interface{}) { + logSeverity(ctx, logger, SeverityWarning, f, v...) +} + +// LogError writes a log entry using SeverityError +func LogError(ctx context.Context, logger *alog.Logger, f string, v ...interface{}) { + logSeverity(ctx, logger, SeverityError, f, v...) +} + +// LogCritical writes a log entry using SeverityCritical +func LogCritical(ctx context.Context, logger *alog.Logger, f string, v ...interface{}) { + logSeverity(ctx, logger, SeverityCritical, f, v...) +}