Skip to content

Commit

Permalink
gkelog: Add some convenience functions for logging with severity
Browse files Browse the repository at this point in the history
  • Loading branch information
justinruggles committed Jan 23, 2019
1 parent a4a0a9b commit a80346b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 13 deletions.
13 changes: 0 additions & 13 deletions emitter/gkelog/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions emitter/gkelog/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
60 changes: 60 additions & 0 deletions emitter/gkelog/severity.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit a80346b

Please sign in to comment.