2019-11-27 19:02:16 +03:00
|
|
|
// Package log provides debug logging
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-27 20:31:35 +03:00
|
|
|
"time"
|
2019-11-27 19:02:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-28 14:36:38 +03:00
|
|
|
// DefaultLog logger
|
|
|
|
DefaultLog = NewLog()
|
|
|
|
// DefaultLevel is default log level
|
|
|
|
DefaultLevel = LevelInfo
|
2019-11-27 19:02:16 +03:00
|
|
|
// prefix for all messages
|
|
|
|
prefix string
|
|
|
|
)
|
|
|
|
|
2019-11-27 20:31:35 +03:00
|
|
|
// Log is event log
|
|
|
|
type Log interface {
|
|
|
|
// Read reads log entries from the logger
|
|
|
|
Read(...ReadOption) []Record
|
2019-11-27 19:02:16 +03:00
|
|
|
// Write writes logs to logger
|
|
|
|
Write(...interface{})
|
2019-11-30 15:39:29 +03:00
|
|
|
// Stream logs
|
|
|
|
Stream(chan bool) <-chan Record
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 20:31:35 +03:00
|
|
|
// Record is log record entry
|
|
|
|
type Record struct {
|
|
|
|
// Timestamp of logged event
|
|
|
|
Timestamp time.Time
|
|
|
|
// Value contains log entry
|
|
|
|
Value interface{}
|
|
|
|
// Metadata to enrich log record
|
|
|
|
Metadata map[string]string
|
|
|
|
}
|
|
|
|
|
2019-11-27 19:02:16 +03:00
|
|
|
// level is a log level
|
|
|
|
type Level int
|
|
|
|
|
|
|
|
const (
|
|
|
|
LevelFatal Level = iota
|
|
|
|
LevelError
|
|
|
|
LevelInfo
|
|
|
|
LevelWarn
|
|
|
|
LevelDebug
|
|
|
|
LevelTrace
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
switch os.Getenv("MICRO_LOG_LEVEL") {
|
|
|
|
case "trace":
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = LevelTrace
|
2019-11-27 19:02:16 +03:00
|
|
|
case "debug":
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = LevelDebug
|
2019-11-27 19:02:16 +03:00
|
|
|
case "warn":
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = LevelWarn
|
2019-11-27 19:02:16 +03:00
|
|
|
case "info":
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = LevelInfo
|
2019-11-27 19:02:16 +03:00
|
|
|
case "error":
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = LevelError
|
2019-11-27 19:02:16 +03:00
|
|
|
case "fatal":
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = LevelFatal
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 20:31:35 +03:00
|
|
|
func log(v ...interface{}) {
|
2019-11-27 19:02:16 +03:00
|
|
|
if len(prefix) > 0 {
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLog.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...))
|
2019-11-27 19:02:16 +03:00
|
|
|
return
|
|
|
|
}
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLog.Write(fmt.Sprint(v...))
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 20:31:35 +03:00
|
|
|
func logf(format string, v ...interface{}) {
|
2019-11-27 19:02:16 +03:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
format = prefix + " " + format
|
|
|
|
}
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLog.Write(fmt.Sprintf(format, v...))
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel logs with the level specified
|
|
|
|
func WithLevel(l Level, v ...interface{}) {
|
2019-11-28 14:36:38 +03:00
|
|
|
if l > DefaultLevel {
|
2019-11-27 19:02:16 +03:00
|
|
|
return
|
|
|
|
}
|
2019-11-27 20:31:35 +03:00
|
|
|
log(v...)
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel logs with the level specified
|
|
|
|
func WithLevelf(l Level, format string, v ...interface{}) {
|
2019-11-28 14:36:38 +03:00
|
|
|
if l > DefaultLevel {
|
2019-11-27 19:02:16 +03:00
|
|
|
return
|
|
|
|
}
|
2019-11-27 20:31:35 +03:00
|
|
|
logf(format, v...)
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trace provides trace level logging
|
|
|
|
func Trace(v ...interface{}) {
|
|
|
|
WithLevel(LevelTrace, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tracef provides trace level logging
|
|
|
|
func Tracef(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelTrace, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug provides debug level logging
|
|
|
|
func Debug(v ...interface{}) {
|
|
|
|
WithLevel(LevelDebug, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf provides debug level logging
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelDebug, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn provides warn level logging
|
|
|
|
func Warn(v ...interface{}) {
|
|
|
|
WithLevel(LevelWarn, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf provides warn level logging
|
|
|
|
func Warnf(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelWarn, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info provides info level logging
|
|
|
|
func Info(v ...interface{}) {
|
|
|
|
WithLevel(LevelInfo, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Infof provides info level logging
|
|
|
|
func Infof(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelInfo, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error provides warn level logging
|
|
|
|
func Error(v ...interface{}) {
|
|
|
|
WithLevel(LevelError, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf provides warn level logging
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelError, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal logs with Log and then exits with os.Exit(1)
|
|
|
|
func Fatal(v ...interface{}) {
|
|
|
|
WithLevel(LevelFatal, v...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf logs with Logf and then exits with os.Exit(1)
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelFatal, format, v...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel sets the log level
|
|
|
|
func SetLevel(l Level) {
|
2019-11-28 14:36:38 +03:00
|
|
|
DefaultLevel = l
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel returns the current level
|
|
|
|
func GetLevel() Level {
|
2019-11-28 14:36:38 +03:00
|
|
|
return DefaultLevel
|
2019-11-27 19:02:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set a prefix for the logger
|
|
|
|
func SetPrefix(p string) {
|
|
|
|
prefix = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set service name
|
|
|
|
func Name(name string) {
|
|
|
|
prefix = fmt.Sprintf("[%s]", name)
|
|
|
|
}
|