Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add warn level to logging #43

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add warn level
  • Loading branch information
GerritErpenstein committed Oct 12, 2023
commit 0c8210b72daeacf4bdeb0634e61dcd8bfcdbd41c
19 changes: 16 additions & 3 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ var StdDebug = New(os.Stderr, newWriteMessageFunc("DEBUG"))
// StdInfo is the standard logger for info messages
var StdInfo = New(os.Stderr, newWriteMessageFunc("INFO"))

// StdWarn is the standard logger for warn messages
var StdWarn = New(os.Stderr, newWriteMessageFunc("WARN"))

// StdError is the standard logger for error messages
var StdError = New(os.Stderr, newWriteMessageFunc("ERROR"))

Expand All @@ -93,7 +96,7 @@ func Debug(ctx context.Context, v ...interface{}) {
StdDebug.Print(ctx, v...)
}

// Debug is equivalent to log.StdDebug.Printf()
// Debugf is equivalent to log.StdDebug.Printf()
func Debugf(ctx context.Context, format string, v ...interface{}) {
StdDebug.Printf(ctx, format, v...)
}
Expand All @@ -103,17 +106,27 @@ func Info(ctx context.Context, v ...interface{}) {
StdInfo.Print(ctx, v...)
}

// Info is equivalent to log.StdInfo.Printf()
// Infof is equivalent to log.StdInfo.Printf()
func Infof(ctx context.Context, format string, v ...interface{}) {
StdInfo.Printf(ctx, format, v...)
}

// Warn is equivalent to log.StdWarn.Print()
func Warn(ctx context.Context, v ...interface{}) {
StdWarn.Print(ctx, v...)
}

// Warnf is equivalent to log.StdWarn.Printf()
func Warnf(ctx context.Context, format string, v ...interface{}) {
StdWarn.Printf(ctx, format, v...)
}

// Error is equivalent to log.StdError.Print()
func Error(ctx context.Context, v ...interface{}) {
StdError.Print(ctx, v...)
}

// Error is equivalent to log.StdError.Printf()
// Errorf is equivalent to log.StdError.Printf()
func Errorf(ctx context.Context, format string, v ...interface{}) {
StdError.Printf(ctx, format, v...)
}
26 changes: 26 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ func TestStdInfoWriteMessageUnchanged_Infof_WritesMessageWithTimestampAndInfo(t
}
}

func TestStdInfoWriteMessageUnchanged_Warn_WritesMessageWithTimestampAndInfo(t *testing.T) {
rec := newOutputRecorder(t)
log.StdWarn.SetOutput(rec)

log.Warn(context.Background(), "Message")

r, _ := regexp.Compile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z WARN Message\n`)
actual := rec.String()
if !r.MatchString(actual) {
t.Errorf("'%v' doesn't match the pattern '<RFC3339 Timestamp> WARN Message\n'", actual)
}
}

func TestStdInfoWriteMessageUnchanged_Warnf_WritesMessageWithTimestampAndInfo(t *testing.T) {
rec := newOutputRecorder(t)
log.StdWarn.SetOutput(rec)

log.Warnf(context.Background(), "Message %v", 1)

r, _ := regexp.Compile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z WARN Message 1\n`)
actual := rec.String()
if !r.MatchString(actual) {
t.Errorf("'%v' doesn't match the pattern '<RFC3339 Timestamp> WARN Message\n'", actual)
}
}

func TestStdInfoWriteMessageUnchanged_Error_WritesMessageWithTimestampAndInfo(t *testing.T) {
rec := newOutputRecorder(t)
log.StdError.SetOutput(rec)
Expand Down
1 change: 1 addition & 0 deletions log/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const DEBUG = 7
const INFO = 6
const WARN = 4
const ERROR = 3

// NewWriter creates a new syslogwriter which writes to the given endpoint
Expand Down
25 changes: 23 additions & 2 deletions log/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestWriteHeaderDebugFunc_WritesAppnameAndDebugSeverity(t *testing.T) {
// Priority value is calculated as follows
// FACILITY * 8 + SEVERITY
// FACILITY is 16 (local use 0)
// SEVERITY is 7 (Info)
// SEVERITY is 7 (Debug)
// 16*8+7=135
// https://tools.ietf.org/html/rfc5424#section-6.2.1
regex := fmt.Sprintf(`<135>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z %v myapp %v - `, hostname, os.Getpid())
Expand All @@ -52,6 +52,27 @@ func TestWriteHeaderDebugFunc_WritesAppnameAndDebugSeverity(t *testing.T) {
}
}

func TestWriteHeaderDebugFunc_WritesAppnameAndWarnSeverity(t *testing.T) {
f := syslog.NewWriteHeaderFunc("myapp", syslog.WARN)

var buf []byte
buf = f(context.Background(), buf, "message")

hostname, _ := os.Hostname()
// Priority value is calculated as follows
// FACILITY * 8 + SEVERITY
// FACILITY is 16 (local use 0)
// SEVERITY is 4 (Warn)
// 16*8+4=132
// https://tools.ietf.org/html/rfc5424#section-6.2.1
regex := fmt.Sprintf(`<132>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z %v myapp %v - `, hostname, os.Getpid())
r, _ := regexp.Compile(regex)
actual := string(buf)
if !r.MatchString(actual) {
t.Errorf("\n'%v' doesn't match the pattern\n'%v'", actual, regex)
}
}

func TestWriteHeaderDebugFunc_WritesAppnameAndErrorSeverity(t *testing.T) {
f := syslog.NewWriteHeaderFunc("myapp", syslog.ERROR)

Expand All @@ -62,7 +83,7 @@ func TestWriteHeaderDebugFunc_WritesAppnameAndErrorSeverity(t *testing.T) {
// Priority value is calculated as follows
// FACILITY * 8 + SEVERITY
// FACILITY is 16 (local use 0)
// SEVERITY is 3 (Info)
// SEVERITY is 3 (Error)
// 16*8+3=131
// https://tools.ietf.org/html/rfc5424#section-6.2.1
regex := fmt.Sprintf(`<131>1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z %v myapp %v - `, hostname, os.Getpid())
Expand Down