Added DefaultLog variables

This commit is contained in:
Milos Gajdos 2019-11-28 11:36:38 +00:00
parent 265e8ade05
commit 9fab47ecdb
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
2 changed files with 21 additions and 20 deletions

View File

@ -18,12 +18,13 @@ var (
type Debug struct { type Debug struct {
started int64 started int64
proto.DebugHandler proto.DebugHandler
log.Log log log.Log
} }
func newDebug() *Debug { func newDebug() *Debug {
return &Debug{ return &Debug{
started: time.Now().Unix(), started: time.Now().Unix(),
log: log.DefaultLog,
} }
} }
@ -48,9 +49,9 @@ func (d *Debug) Logs(ctx context.Context, req *proto.LogRequest, stream proto.De
var records []log.Record var records []log.Record
since := time.Unix(0, req.Since) since := time.Unix(0, req.Since)
if !since.IsZero() { if !since.IsZero() {
records = d.Log.Read(log.Since(since)) records = d.log.Read(log.Since(since))
} else { } else {
records = d.Log.Read(log.Count(int(req.Count))) records = d.log.Read(log.Count(int(req.Count)))
} }
defer stream.Close() defer stream.Close()

View File

@ -8,10 +8,10 @@ import (
) )
var ( var (
// logger is default Logger // DefaultLog logger
logger Log = NewLog() DefaultLog = NewLog()
// default log level is info // DefaultLevel is default log level
level = LevelInfo DefaultLevel = LevelInfo
// prefix for all messages // prefix for all messages
prefix string prefix string
) )
@ -49,38 +49,38 @@ const (
func init() { func init() {
switch os.Getenv("MICRO_LOG_LEVEL") { switch os.Getenv("MICRO_LOG_LEVEL") {
case "trace": case "trace":
level = LevelTrace DefaultLevel = LevelTrace
case "debug": case "debug":
level = LevelDebug DefaultLevel = LevelDebug
case "warn": case "warn":
level = LevelWarn DefaultLevel = LevelWarn
case "info": case "info":
level = LevelInfo DefaultLevel = LevelInfo
case "error": case "error":
level = LevelError DefaultLevel = LevelError
case "fatal": case "fatal":
level = LevelFatal DefaultLevel = LevelFatal
} }
} }
func log(v ...interface{}) { func log(v ...interface{}) {
if len(prefix) > 0 { if len(prefix) > 0 {
logger.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)) DefaultLog.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...))
return return
} }
logger.Write(fmt.Sprint(v...)) DefaultLog.Write(fmt.Sprint(v...))
} }
func logf(format string, v ...interface{}) { func logf(format string, v ...interface{}) {
if len(prefix) > 0 { if len(prefix) > 0 {
format = prefix + " " + format format = prefix + " " + format
} }
logger.Write(fmt.Sprintf(format, v...)) DefaultLog.Write(fmt.Sprintf(format, v...))
} }
// WithLevel logs with the level specified // WithLevel logs with the level specified
func WithLevel(l Level, v ...interface{}) { func WithLevel(l Level, v ...interface{}) {
if l > level { if l > DefaultLevel {
return return
} }
log(v...) log(v...)
@ -88,7 +88,7 @@ func WithLevel(l Level, v ...interface{}) {
// WithLevel logs with the level specified // WithLevel logs with the level specified
func WithLevelf(l Level, format string, v ...interface{}) { func WithLevelf(l Level, format string, v ...interface{}) {
if l > level { if l > DefaultLevel {
return return
} }
logf(format, v...) logf(format, v...)
@ -158,12 +158,12 @@ func Fatalf(format string, v ...interface{}) {
// SetLevel sets the log level // SetLevel sets the log level
func SetLevel(l Level) { func SetLevel(l Level) {
level = l DefaultLevel = l
} }
// GetLevel returns the current level // GetLevel returns the current level
func GetLevel() Level { func GetLevel() Level {
return level return DefaultLevel
} }
// Set a prefix for the logger // Set a prefix for the logger