From b6db0d2663e74f91858a74ddb9f8def9a40e491d Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 2 Oct 2019 17:42:34 +0100 Subject: [PATCH] Add ability to set log prefix --- util/log/log.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/util/log/log.go b/util/log/log.go index c8ae4acc..32992545 100644 --- a/util/log/log.go +++ b/util/log/log.go @@ -2,6 +2,7 @@ package log import ( + "fmt" "os" "github.com/go-log/log" @@ -25,6 +26,9 @@ var ( // default log level is info level = LevelInfo + + // prefix for all messages + prefix string ) func init() { @@ -44,11 +48,18 @@ func init() { // Log makes use of github.com/go-log/log.Log func Log(v ...interface{}) { + if len(prefix) > 0 { + logger.Log(append([]interface{}{prefix, " "}, v...)...) + return + } logger.Log(v...) } // Logf makes use of github.com/go-log/log.Logf func Logf(format string, v ...interface{}) { + if len(prefix) > 0 { + format = prefix + " " + format + } logger.Logf(format, v...) } @@ -139,3 +150,13 @@ func SetLevel(l Level) { func GetLevel() Level { return level } + +// Set a prefix for the logger +func SetPrefix(p string) { + prefix = p +} + +// Set service name +func Name(name string) { + prefix = fmt.Sprintf("[%s]", name) +}