Merge pull request #809 from micro/log-prefix
Add ability to set log prefix
This commit is contained in:
commit
36675aff1e
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user