Deprecate code.google.com/p/log4go
- Add new internal `rqlite/log` package - Replace `log4go` with `rqlite/log` - Adapt log formatting in existing logs Fixes #21master
parent
cdc6021ae7
commit
eba40187a9
@ -0,0 +1,103 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Log levels
|
||||
const (
|
||||
TRACE = iota // 0
|
||||
DEBUG = iota // 1
|
||||
INFO = iota // 2
|
||||
WARN = iota // 3
|
||||
ERROR = iota // 4
|
||||
)
|
||||
|
||||
// Level set for logs
|
||||
var Level = TRACE
|
||||
|
||||
// SetLevel sets the log level
|
||||
// given a string
|
||||
func SetLevel(level string) {
|
||||
switch level {
|
||||
case "TRACE":
|
||||
Level = TRACE
|
||||
case "DEBUG":
|
||||
Level = DEBUG
|
||||
case "INFO":
|
||||
Level = INFO
|
||||
case "WARN":
|
||||
Level = WARN
|
||||
case "ERROR":
|
||||
Level = ERROR
|
||||
default:
|
||||
Level = TRACE
|
||||
}
|
||||
}
|
||||
|
||||
// SetOutput set the output destination
|
||||
// of logs
|
||||
func SetOutput(w io.Writer) {
|
||||
log.SetOutput(w)
|
||||
}
|
||||
|
||||
// Tracef writes a formatted log on TRACE level
|
||||
func Tracef(format string, v ...interface{}) {
|
||||
if Level <= TRACE {
|
||||
log.Printf("[TRACE] "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Trace writes a log on TRACE level
|
||||
func Trace(s string) {
|
||||
Tracef(s)
|
||||
}
|
||||
|
||||
// Debugf writes a formatted log on DEBUG level
|
||||
func Debugf(format string, v ...interface{}) {
|
||||
if Level <= DEBUG {
|
||||
log.Printf("[DEBUG] "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Debug writes a log on DEBUG level
|
||||
func Debug(s string) {
|
||||
Debugf(s)
|
||||
}
|
||||
|
||||
// Infof writes a formatted log on INFO level
|
||||
func Infof(format string, v ...interface{}) {
|
||||
if Level <= INFO {
|
||||
log.Printf("[INFO ] "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Info write a log on INFO level
|
||||
func Info(s string) {
|
||||
Infof(s)
|
||||
}
|
||||
|
||||
// Warnf writes a formatted log on WARN level
|
||||
func Warnf(format string, v ...interface{}) {
|
||||
if Level <= WARN {
|
||||
log.Printf("[WARN ] "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Warn write a log on WARN level
|
||||
func Warn(s string) {
|
||||
Warnf(s)
|
||||
}
|
||||
|
||||
// Errorf writes a formatted log on ERROR level
|
||||
func Errorf(format string, v ...interface{}) {
|
||||
if Level <= ERROR {
|
||||
log.Printf("[ERROR] "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Error write a log on ERROR level
|
||||
func Error(s string) {
|
||||
Errorf(s)
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
// Hook up gocheck into the "go test" runner.
|
||||
func Test(t *testing.T) {
|
||||
TestingT(t)
|
||||
}
|
||||
|
||||
type LogSuite struct{}
|
||||
|
||||
var _ = Suite(&LogSuite{})
|
||||
|
||||
func (s *LogSuite) TestSetLevel(c *C) {
|
||||
SetLevel("DEBUG")
|
||||
c.Assert(Level, Equals, DEBUG)
|
||||
|
||||
SetLevel("INFO")
|
||||
c.Assert(Level, Equals, INFO)
|
||||
|
||||
SetLevel("WARN")
|
||||
c.Assert(Level, Equals, WARN)
|
||||
|
||||
SetLevel("ERROR")
|
||||
c.Assert(Level, Equals, ERROR)
|
||||
|
||||
SetLevel("TRACE")
|
||||
c.Assert(Level, Equals, TRACE)
|
||||
}
|
||||
|
||||
func (s *LogSuite) TestLog(c *C) {
|
||||
logFile, err := os.OpenFile("tmptestfile.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
c.Check(err, IsNil)
|
||||
defer logFile.Close()
|
||||
defer os.Remove("tmptestfile.txt")
|
||||
|
||||
SetOutput(logFile)
|
||||
|
||||
num, err := lineCounter(logFile)
|
||||
c.Check(num, Equals, 0)
|
||||
c.Check(err, IsNil)
|
||||
|
||||
tmpFile, _ := os.Open("tmptestfile.txt")
|
||||
Level = ERROR
|
||||
Trace("a")
|
||||
Debug("a")
|
||||
Info("a")
|
||||
Warn("a")
|
||||
Error("a")
|
||||
num, err = lineCounter(tmpFile)
|
||||
c.Check(num, Equals, 1)
|
||||
c.Check(err, IsNil)
|
||||
tmpFile.Close()
|
||||
|
||||
tmpFile, _ = os.Open("tmptestfile.txt")
|
||||
Level = WARN
|
||||
Trace("a")
|
||||
Debug("a")
|
||||
Info("a")
|
||||
Warn("a")
|
||||
Error("a")
|
||||
num, err = lineCounter(tmpFile)
|
||||
c.Check(num, Equals, 3)
|
||||
c.Check(err, IsNil)
|
||||
tmpFile.Close()
|
||||
|
||||
tmpFile, _ = os.Open("tmptestfile.txt")
|
||||
Level = INFO
|
||||
Trace("a")
|
||||
Debug("a")
|
||||
Info("a")
|
||||
Warn("a")
|
||||
Error("a")
|
||||
num, err = lineCounter(tmpFile)
|
||||
c.Check(num, Equals, 6)
|
||||
c.Check(err, IsNil)
|
||||
tmpFile.Close()
|
||||
|
||||
tmpFile, _ = os.Open("tmptestfile.txt")
|
||||
Level = DEBUG
|
||||
Trace("a")
|
||||
Debug("a")
|
||||
Info("a")
|
||||
Warn("a")
|
||||
Error("a")
|
||||
num, err = lineCounter(tmpFile)
|
||||
c.Check(num, Equals, 10)
|
||||
c.Check(err, IsNil)
|
||||
tmpFile.Close()
|
||||
|
||||
tmpFile, _ = os.Open("tmptestfile.txt")
|
||||
Level = TRACE
|
||||
Trace("a")
|
||||
Debug("a")
|
||||
Info("a")
|
||||
Warn("a")
|
||||
Error("a")
|
||||
num, err = lineCounter(tmpFile)
|
||||
c.Check(num, Equals, 15)
|
||||
c.Check(err, IsNil)
|
||||
tmpFile.Close()
|
||||
}
|
||||
|
||||
// Taken from http://stackoverflow.com/a/24563853/1187471
|
||||
func lineCounter(r io.Reader) (int, error) {
|
||||
buf := make([]byte, 8196)
|
||||
count := 0
|
||||
lineSep := []byte{'\n'}
|
||||
|
||||
for {
|
||||
c, err := r.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return count, err
|
||||
}
|
||||
|
||||
count += bytes.Count(buf[:c], lineSep)
|
||||
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
Loading…
Reference in New Issue