From 4fa3c70429d54de23c12430a2f1969167bf1bf26 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 10 Apr 2017 15:03:10 +0200 Subject: [PATCH] simplify log handling --- app/log/internal/log_entry.go | 4 +-- app/log/log.go | 54 ++++++++++------------------------- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/app/log/internal/log_entry.go b/app/log/internal/log_entry.go index 0beb80db8..a07d52f06 100644 --- a/app/log/internal/log_entry.go +++ b/app/log/internal/log_entry.go @@ -13,11 +13,11 @@ type LogEntry interface { type ErrorLog struct { Prefix string - Values []interface{} + Error error } func (v *ErrorLog) String() string { - return v.Prefix + serial.Concat(v.Values...) + return v.Prefix + v.Error.Error() } type AccessLog struct { diff --git a/app/log/log.go b/app/log/log.go index 82b53b508..1ddcd7364 100644 --- a/app/log/log.go +++ b/app/log/log.go @@ -51,54 +51,30 @@ func InitErrorLogger(file string) error { return nil } -// writeDebug outputs a debug log with given format and optional arguments. -func writeDebug(val ...interface{}) { - debugLogger.Log(&internal.ErrorLog{ - Prefix: "[Debug]", - Values: val, - }) -} - -// writeInfo outputs an info log with given format and optional arguments. -func writeInfo(val ...interface{}) { - infoLogger.Log(&internal.ErrorLog{ - Prefix: "[Info]", - Values: val, - }) -} - -// writeWarning outputs a warning log with given format and optional arguments. -func writeWarning(val ...interface{}) { - warningLogger.Log(&internal.ErrorLog{ - Prefix: "[Warning]", - Values: val, - }) -} - -// writeError outputs an error log with given format and optional arguments. -func writeError(val ...interface{}) { - errorLogger.Log(&internal.ErrorLog{ - Prefix: "[Error]", - Values: val, - }) -} - -func Trace(err error) { - s := errors.GetSeverity(err) +func getLoggerAndPrefix(s errors.Severity) (internal.LogWriter, string) { switch s { case errors.SeverityDebug: - writeDebug(err) + return debugLogger, "[Debug]" case errors.SeverityInfo: - writeInfo(err) + return infoLogger, "[Info]" case errors.SeverityWarning: - writeWarning(err) + return infoLogger, "[Warning]" case errors.SeverityError: - writeError(err) + return errorLogger, "[Error]" default: - writeInfo(err) + return infoLogger, "[Info]" } } +// Trace logs an error message based on its severity. +func Trace(err error) { + logger, prefix := getLoggerAndPrefix(errors.GetSeverity(err)) + logger.Log(&internal.ErrorLog{ + Prefix: prefix, + Error: err, + }) +} + type Instance struct { config *Config }