Merge pull request #809 from micro/log-prefix

Add ability to set log prefix
This commit is contained in:
Asim Aslam 2019-10-02 17:47:27 +01:00 committed by GitHub
commit 36675aff1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
package log package log
import ( import (
"fmt"
"os" "os"
"github.com/go-log/log" "github.com/go-log/log"
@ -25,6 +26,9 @@ var (
// default log level is info // default log level is info
level = LevelInfo level = LevelInfo
// prefix for all messages
prefix string
) )
func init() { func init() {
@ -44,11 +48,18 @@ func init() {
// Log makes use of github.com/go-log/log.Log // Log makes use of github.com/go-log/log.Log
func Log(v ...interface{}) { func Log(v ...interface{}) {
if len(prefix) > 0 {
logger.Log(append([]interface{}{prefix, " "}, v...)...)
return
}
logger.Log(v...) logger.Log(v...)
} }
// Logf makes use of github.com/go-log/log.Logf // Logf makes use of github.com/go-log/log.Logf
func Logf(format string, v ...interface{}) { func Logf(format string, v ...interface{}) {
if len(prefix) > 0 {
format = prefix + " " + format
}
logger.Logf(format, v...) logger.Logf(format, v...)
} }
@ -139,3 +150,13 @@ func SetLevel(l Level) {
func GetLevel() Level { func GetLevel() Level {
return 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)
}