1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

Add ability to set log prefix

This commit is contained in:
Asim Aslam 2019-10-02 17:42:34 +01:00
parent 2370fb1209
commit b6db0d2663

View File

@ -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)
}