Commit 6b7491e1 by Torkel Ödegaard

Merge pull request #3903 from grafana/log-fixes

Log fixes
parents 2e20c680 fbc90e03
......@@ -46,8 +46,8 @@ var (
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
type ConsoleWriter struct {
lg *log.Logger
Level int `json:"level"`
Formatting bool `json:"formatting"`
Level LogLevel `json:"level"`
Formatting bool `json:"formatting"`
}
// create ConsoleWriter returning as LoggerInterface.
......@@ -63,7 +63,7 @@ func (cw *ConsoleWriter) Init(config string) error {
return json.Unmarshal([]byte(config), cw)
}
func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
func (cw *ConsoleWriter) WriteMsg(msg string, skip int, level LogLevel) error {
if cw.Level > level {
return nil
}
......@@ -82,11 +82,11 @@ func (_ *ConsoleWriter) Flush() {
func (_ *ConsoleWriter) Destroy() {
}
func printConsole(level int, msg string) {
func printConsole(level LogLevel, msg string) {
consoleWriter.WriteMsg(msg, 0, level)
}
func printfConsole(level int, format string, v ...interface{}) {
func printfConsole(level LogLevel, format string, v ...interface{}) {
consoleWriter.WriteMsg(fmt.Sprintf(format, v...), 0, level)
}
......
......@@ -41,7 +41,7 @@ type FileLogWriter struct {
startLock sync.Mutex // Only one log can write to the file
Level int `json:"level"`
Level LogLevel `json:"level"`
}
// an *os.File writer with locker.
......@@ -132,7 +132,7 @@ func (w *FileLogWriter) docheck(size int) {
}
// write logger message into file.
func (w *FileLogWriter) WriteMsg(msg string, skip, level int) error {
func (w *FileLogWriter) WriteMsg(msg string, skip int, level LogLevel) error {
if level < w.Level {
return nil
}
......
......@@ -99,7 +99,7 @@ func Close() {
type LogLevel int
const (
TRACE = iota
TRACE LogLevel = iota
DEBUG
INFO
WARN
......@@ -111,7 +111,7 @@ const (
// LoggerInterface represents behaviors of a logger provider.
type LoggerInterface interface {
Init(config string) error
WriteMsg(msg string, skip, level int) error
WriteMsg(msg string, skip int, level LogLevel) error
Destroy()
Flush()
}
......@@ -132,8 +132,9 @@ func Register(name string, log loggerType) {
}
type logMsg struct {
skip, level int
msg string
skip int
level LogLevel
msg string
}
// Logger is default logger in beego application.
......@@ -141,7 +142,7 @@ type logMsg struct {
type Logger struct {
adapter string
lock sync.Mutex
level int
level LogLevel
msg chan *logMsg
outputs map[string]LoggerInterface
quit chan bool
......@@ -188,10 +189,7 @@ func (l *Logger) DelLogger(adapter string) error {
return nil
}
func (l *Logger) writerMsg(skip, level int, msg string) error {
if l.level > level {
return nil
}
func (l *Logger) writerMsg(skip int, level LogLevel, msg string) error {
lm := &logMsg{
skip: skip,
level: level,
......@@ -266,36 +264,57 @@ func (l *Logger) Close() {
}
func (l *Logger) Trace(format string, v ...interface{}) {
if l.level > TRACE {
return
}
msg := fmt.Sprintf("[T] "+format, v...)
l.writerMsg(0, TRACE, msg)
}
func (l *Logger) Debug(format string, v ...interface{}) {
if l.level > DEBUG {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
l.writerMsg(0, DEBUG, msg)
}
func (l *Logger) Info(format string, v ...interface{}) {
if l.level > INFO {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
l.writerMsg(0, INFO, msg)
}
func (l *Logger) Warn(format string, v ...interface{}) {
if l.level > WARN {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
l.writerMsg(0, WARN, msg)
}
func (l *Logger) Error(skip int, format string, v ...interface{}) {
if l.level > ERROR {
return
}
msg := fmt.Sprintf("[E] "+format, v...)
l.writerMsg(skip, ERROR, msg)
}
func (l *Logger) Critical(skip int, format string, v ...interface{}) {
if l.level > CRITICAL {
return
}
msg := fmt.Sprintf("[C] "+format, v...)
l.writerMsg(skip, CRITICAL, msg)
}
func (l *Logger) Fatal(skip int, format string, v ...interface{}) {
if l.level > FATAL {
return
}
msg := fmt.Sprintf("[F] "+format, v...)
l.writerMsg(skip, FATAL, msg)
l.Close()
......
......@@ -39,7 +39,7 @@ func (sw *SyslogWriter) Init(config string) error {
return nil
}
func (sw *SyslogWriter) WriteMsg(msg string, skip, level int) error {
func (sw *SyslogWriter) WriteMsg(msg string, skip int, level LogLevel) error {
var err error
switch level {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment