Commit a6379481 authored by astaxie's avatar astaxie

Merge pull request #691 from FGM/logger-rfc5424

Issue #682: convert logs package to RFC5424 logging levels.
parents dbe75f90 7d09ac25
......@@ -48,7 +48,7 @@ func (c *ConnWriter) Init(jsonconfig string) error {
// write message in connection.
// if connection is down, try to re-connect.
func (c *ConnWriter) WriteMsg(msg string, level int) error {
if level < c.Level {
if level > c.Level {
return nil
}
if c.neddedConnectOnMsg() {
......
......@@ -16,5 +16,5 @@ import (
func TestConn(t *testing.T) {
log := NewLogger(1000)
log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`)
log.Info("info")
log.Informational("informational")
}
......@@ -27,12 +27,14 @@ func NewBrush(color string) Brush {
}
var colors = []Brush{
NewBrush("1;36"), // Trace cyan
NewBrush("1;34"), // Debug blue
NewBrush("1;32"), // Info green
NewBrush("1;33"), // Warn yellow
NewBrush("1;37"), // Emergency white
NewBrush("1;36"), // Alert cyan
NewBrush("1;35"), // Critical magenta
NewBrush("1;31"), // Error red
NewBrush("1;35"), // Critical purple
NewBrush("1;33"), // Warning yellow
NewBrush("1;32"), // Notice green
NewBrush("1;34"), // Informational green
NewBrush("1;30"), // Debug black
}
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
......@@ -45,7 +47,7 @@ type ConsoleWriter struct {
func NewConsole() LoggerInterface {
cw := new(ConsoleWriter)
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime)
cw.Level = LevelTrace
cw.Level = LevelDebug
return cw
}
......@@ -64,7 +66,7 @@ func (c *ConsoleWriter) Init(jsonconfig string) error {
// write message in console.
func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
if level < c.Level {
if level > c.Level {
return nil
}
if goos := runtime.GOOS; goos == "windows" {
......
......@@ -13,22 +13,29 @@ import (
"testing"
)
// Try each log level in decreasing order of priority.
func testConsoleCalls(bl *BeeLogger) {
bl.Emergency("emergency")
bl.Alert("alert")
bl.Critical("critical")
bl.Error("error")
bl.Warning("warning")
bl.Notice("notice")
bl.Informational("informational")
bl.Debug("debug")
}
// Test console logging by visually comparing the lines being output with and
// without a log level specification.
func TestConsole(t *testing.T) {
log := NewLogger(10000)
log.EnableFuncCallDepth(true)
log.SetLogger("console", "")
log.Trace("trace")
log.Info("info")
log.Warn("warning")
log.Debug("debug")
log.Critical("critical")
log1 := NewLogger(10000)
log1.EnableFuncCallDepth(true)
log1.SetLogger("console", "")
testConsoleCalls(log1)
log2 := NewLogger(100)
log2.SetLogger("console", `{"level":1}`)
log.Trace("trace")
log.Info("info")
log.Warn("warning")
log.Debug("debug")
log.Critical("critical")
log2.SetLogger("console", `{"level":3}`)
testConsoleCalls(log2)
}
func BenchmarkConsole(b *testing.B) {
......@@ -36,6 +43,6 @@ func BenchmarkConsole(b *testing.B) {
log.EnableFuncCallDepth(true)
log.SetLogger("console", "")
for i := 0; i < b.N; i++ {
log.Trace("trace")
log.Debug("debug")
}
}
......@@ -141,7 +141,7 @@ func (w *FileLogWriter) docheck(size int) {
// write logger message into file.
func (w *FileLogWriter) WriteMsg(msg string, level int) error {
if level < w.Level {
if level > w.Level {
return nil
}
n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] "
......
......@@ -13,6 +13,7 @@ import (
"bufio"
"fmt"
"os"
"strconv"
"testing"
"time"
)
......@@ -20,12 +21,14 @@ import (
func TestFile(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`)
log.Trace("test")
log.Info("info")
log.Debug("debug")
log.Warn("warning")
log.Informational("info")
log.Notice("notice")
log.Warning("warning")
log.Error("error")
log.Alert("alert")
log.Critical("critical")
log.Emergency("emergency")
time.Sleep(time.Second * 4)
f, err := os.Open("test.log")
if err != nil {
......@@ -42,21 +45,24 @@ func TestFile(t *testing.T) {
linenum++
}
}
if linenum != 6 {
t.Fatal(linenum, "not line 6")
var expected = LevelDebug + 1
if linenum != expected {
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines")
}
os.Remove("test.log")
}
func TestFile2(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test2.log","level":2}`)
log.Trace("test")
log.Info("info")
log.SetLogger("file", fmt.Sprintf(`{"filename":"test2.log","level":%d}`, LevelError))
log.Debug("debug")
log.Warn("warning")
log.Info("info")
log.Notice("notice")
log.Warning("warning")
log.Error("error")
log.Alert("alert")
log.Critical("critical")
log.Emergency("emergency")
time.Sleep(time.Second * 4)
f, err := os.Open("test2.log")
if err != nil {
......@@ -73,8 +79,9 @@ func TestFile2(t *testing.T) {
linenum++
}
}
if linenum != 4 {
t.Fatal(linenum, "not line 4")
var expected = LevelError + 1
if linenum != expected {
t.Fatal(linenum, "not "+strconv.Itoa(expected)+" lines")
}
os.Remove("test2.log")
}
......@@ -82,17 +89,19 @@ func TestFile2(t *testing.T) {
func TestFileRotate(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`)
log.Trace("test")
log.Info("info")
log.Debug("debug")
log.Warn("warning")
log.Info("info")
log.Notice("notice")
log.Warning("warning")
log.Error("error")
log.Alert("alert")
log.Critical("critical")
log.Emergency("emergency")
time.Sleep(time.Second * 4)
rotatename := "test3.log" + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), 1)
b, err := exists(rotatename)
if !b || err != nil {
t.Fatal("rotate not gen")
t.Fatal("rotate not generated")
}
os.Remove(rotatename)
os.Remove("test3.log")
......@@ -113,7 +122,7 @@ func BenchmarkFile(b *testing.B) {
log := NewLogger(100000)
log.SetLogger("file", `{"filename":"test4.log"}`)
for i := 0; i < b.N; i++ {
log.Trace("trace")
log.Debug("debug")
}
os.Remove("test4.log")
}
......@@ -16,14 +16,25 @@ import (
"sync"
)
// RFC5424 log message levels.
const (
// log message levels
LevelTrace = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelEmergency = iota
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug
)
// Legacy loglevel constants to ensure backwards compatibility.
//
// Deprecated: will be removed in 1.5.0.
const (
LevelInfo = LevelInformational
LevelTrace = LevelDebug
LevelWarn = LevelWarning
)
type loggerType func() LoggerInterface
......@@ -72,6 +83,7 @@ type logMsg struct {
// if the buffering chan is full, logger adapters write to file or other way.
func NewLogger(channellen int64) *BeeLogger {
bl := new(BeeLogger)
bl.level = LevelDebug
bl.loggerFuncCallDepth = 2
bl.msg = make(chan *logMsg, channellen)
bl.outputs = make(map[string]LoggerInterface)
......@@ -113,7 +125,7 @@ func (bl *BeeLogger) DelLogger(adaptername string) error {
}
func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
if bl.level > loglevel {
if loglevel > bl.level {
return nil
}
lm := new(logMsg)
......@@ -133,8 +145,10 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
return nil
}
// set log message level.
// if message level (such as LevelTrace) is less than logger level (such as LevelWarn), ignore message.
// Set log message level.
//
// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning),
// log providers will not even be sent the message.
func (bl *BeeLogger) SetLevel(l int) {
bl.level = l
}
......@@ -162,40 +176,73 @@ func (bl *BeeLogger) startLogger() {
}
}
// log trace level message.
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
msg := fmt.Sprintf("[T] "+format, v...)
bl.writerMsg(LevelTrace, msg)
// Log EMERGENCY level message.
func (bl *BeeLogger) Emergency(format string, v ...interface{}) {
msg := fmt.Sprintf("[D] "+format, v...)
bl.writerMsg(LevelEmergency, msg)
}
// log debug level message.
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
// Log ALERT level message.
func (bl *BeeLogger) Alert(format string, v ...interface{}) {
msg := fmt.Sprintf("[D] "+format, v...)
bl.writerMsg(LevelDebug, msg)
bl.writerMsg(LevelAlert, msg)
}
// log info level message.
func (bl *BeeLogger) Info(format string, v ...interface{}) {
// Log CRITICAL level message.
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
msg := fmt.Sprintf("[C] "+format, v...)
bl.writerMsg(LevelCritical, msg)
}
// Log ERROR level message.
func (bl *BeeLogger) Error(format string, v ...interface{}) {
msg := fmt.Sprintf("[E] "+format, v...)
bl.writerMsg(LevelError, msg)
}
// Log WARNING level message.
func (bl *BeeLogger) Warning(format string, v ...interface{}) {
msg := fmt.Sprintf("[W] "+format, v...)
bl.writerMsg(LevelWarning, msg)
}
// Log NOTICE level message.
func (bl *BeeLogger) Notice(format string, v ...interface{}) {
msg := fmt.Sprintf("[W] "+format, v...)
bl.writerMsg(LevelNotice, msg)
}
// Log INFORMATIONAL level message.
func (bl *BeeLogger) Informational(format string, v ...interface{}) {
msg := fmt.Sprintf("[I] "+format, v...)
bl.writerMsg(LevelInfo, msg)
bl.writerMsg(LevelInformational, msg)
}
// log warn level message.
// Log DEBUG level message.
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
msg := fmt.Sprintf("[D] "+format, v...)
bl.writerMsg(LevelDebug, msg)
}
// Log WARN level message.
//
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0.
func (bl *BeeLogger) Warn(format string, v ...interface{}) {
msg := fmt.Sprintf("[W] "+format, v...)
bl.writerMsg(LevelWarn, msg)
bl.Warning(format, v...)
}
// log error level message.
func (bl *BeeLogger) Error(format string, v ...interface{}) {
msg := fmt.Sprintf("[E] "+format, v...)
bl.writerMsg(LevelError, msg)
// Log INFO level message.
//
// Deprecated: compatibility alias for Informational(), Will be removed in 1.5.0.
func (bl *BeeLogger) Info(format string, v ...interface{}) {
bl.Informational(format, v...)
}
// log critical level message.
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
msg := fmt.Sprintf("[C] "+format, v...)
bl.writerMsg(LevelCritical, msg)
// Log TRACE level message.
//
// Deprecated: compatibility alias for Debug(), Will be removed in 1.5.0.
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
bl.Debug(format, v...)
}
// flush all chan data.
......
......@@ -57,7 +57,7 @@ func (s *SmtpWriter) Init(jsonconfig string) error {
// write message in smtp writer.
// it will send an email with subject and only this message.
func (s *SmtpWriter) WriteMsg(msg string, level int) error {
if level < s.Level {
if level > s.Level {
return nil
}
......
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