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

Set custom log level, and added formatting fields #340

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
76 changes: 75 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"fmt"
"os"
"time"

Expand All @@ -16,24 +17,53 @@ type Handler interface {
Warn(err error)
Error(err error)

Infof(format string, args ...interface{})
Debugf(format string, args ...interface{})
Errorf(err error, format string, args ...interface{})
Warnf(err error, format string, args ...interface{})

// Kubernetes Controller compliant logger
ControllerLogger() logr.Logger
DatabaseLogger() gormlogger.Interface

SetLevel(level logrus.Level)
}

type Logger struct {
handler *logrus.Entry
}

func (l *Logger) SetLevel(level logrus.Level) {
l.handler.Logger.SetLevel(level)
}

// TerminalFormatter is exported
type TerminalFormatter struct{}

// Format defined the format of output for Logrus logs
// Format is exported
func (f *TerminalFormatter) Format(entry *logrus.Entry) ([]byte, error) {
if entry.Level == logrus.ErrorLevel || entry.Level == logrus.WarnLevel {
return f.formatDetails(entry), nil
}
return append([]byte(entry.Message), '\n'), nil
}

func (f *TerminalFormatter) formatDetails(entry *logrus.Entry) []byte {
level := "Error"
if entry.Level == logrus.WarnLevel {
level = "Warning"
}

output := fmt.Sprintf("%s: %s\n", level, entry.Message)
output += fmt.Sprintf(" Code: %v\n", entry.Data["code"])
output += fmt.Sprintf(" Severity: %v\n", entry.Data["severity"])
output += fmt.Sprintf(" Short Description: %v\n", entry.Data["short-description"])
output += fmt.Sprintf(" Probable Cause: %v\n", entry.Data["probable-cause"])
output += fmt.Sprintf(" Suggested Remediation: %v\n", entry.Data["suggested-remediation"])

return []byte(output)
}

func New(appname string, opts Options) (Handler, error) {
log := logrus.New()

Expand Down Expand Up @@ -80,18 +110,45 @@ func (l *Logger) Error(err error) {
}).Log(logrus.ErrorLevel, err.Error())
}

func (l *Logger) Errorf(err error, format string, args ...interface{}) {
if err == nil {
return
}

message := fmt.Sprintf(format, args...)
fullMessage := fmt.Sprintf("%s: %s", message, err.Error())

l.handler.WithFields(logrus.Fields{
"code": errors.GetCode(err),
"severity": errors.GetSeverity(err),
"short-description": errors.GetSDescription(err),
"probable-cause": errors.GetCause(err),
"suggested-remediation": errors.GetRemedy(err),
}).Log(logrus.ErrorLevel, fullMessage)
}

func (l *Logger) Info(description ...interface{}) {
l.handler.Log(logrus.InfoLevel,
description...,
)
}

func (l *Logger) Infof(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
l.handler.Log(logrus.InfoLevel, message)
}

func (l *Logger) Debug(description ...interface{}) {
l.handler.Log(logrus.DebugLevel,
description...,
)
}

func (l *Logger) Debugf(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...)
l.handler.Log(logrus.DebugLevel, message)
}

func (l *Logger) Warn(err error) {
if err == nil {
return
Expand All @@ -105,3 +162,20 @@ func (l *Logger) Warn(err error) {
"suggested-remediation": errors.GetRemedy(err),
}).Log(logrus.WarnLevel, err.Error())
}

func (l *Logger) Warnf(err error, format string, args ...interface{}) {
if err == nil {
return
}

message := fmt.Sprintf(format, args...)
fullMessage := fmt.Sprintf("%s: %s", message, err.Error())

l.handler.WithFields(logrus.Fields{
"code": errors.GetCode(err),
"severity": errors.GetSeverity(err),
"short-description": errors.GetSDescription(err),
"probable-cause": errors.GetCause(err),
"suggested-remediation": errors.GetRemedy(err),
}).Log(logrus.WarnLevel, fullMessage)
}
Loading