Implement log package as drop-in replacement for handling Debug log-level

This commit is contained in:
Harshavardhana
2015-03-23 18:12:20 -07:00
parent 80892c5c9b
commit 914962bd93
11 changed files with 169 additions and 21 deletions

View File

@@ -0,0 +1,34 @@
package log
import (
internalLog "log"
)
// Fatal - prints string and terminates
var Fatal = internalLog.Fatal
// Fatalf - prints formatted string and terminates
var Fatalf = internalLog.Fatalf
// Fatalln - prints with newline and terminates
var Fatalln = internalLog.Fatalln
// Error - prints string but does not terminate
func Error(args ...interface{}) { internalLog.Print(args...) }
// Errorf - prints formatted string but does not terminate
func Errorf(s string, args ...interface{}) { internalLog.Printf(s, args...) }
// Errorln - prints string with newline but does not terminate
func Errorln(args ...interface{}) { internalLog.Println(args...) }
// Level - logging level
type Level int
// Different log levels
const (
QuietLOG Level = iota
NormalLOG
DebugLOG
TraceLOG
)

View File

@@ -0,0 +1,26 @@
package log
import (
internalLog "log"
)
// Debug logging
func Debug(args ...interface{}) {
if verify(DebugLOG) {
internalLog.Print(args...)
}
}
// Debugf formatted debug logging
func Debugf(s string, args ...interface{}) {
if verify(DebugLOG) {
internalLog.Printf(s, args...)
}
}
// Debugln logging with newline
func Debugln(args ...interface{}) {
if verify(DebugLOG) {
internalLog.Println(args...)
}
}

View File

@@ -0,0 +1,26 @@
package log
import (
internalLog "log"
)
// Print - Normal logging
func Print(args ...interface{}) {
if verify(NormalLOG) {
internalLog.Print(args...)
}
}
// Printf - Normal formatted logging
func Printf(s string, args ...interface{}) {
if verify(NormalLOG) {
internalLog.Printf(s, args...)
}
}
// Println - Normal logging with newline
func Println(args ...interface{}) {
if verify(NormalLOG) {
internalLog.Println(args...)
}
}

44
pkg/utils/log/log.go Normal file
View File

@@ -0,0 +1,44 @@
package log
import (
internalLog "log"
"sync"
)
// Global variable to be set
var verbosity = NormalLOG
var mutex sync.RWMutex
func verify(level Level) bool {
mutex.RLock()
defer mutex.RUnlock()
return Level(verbosity) >= level
}
// SetVerbosity - set log level value globally
func SetVerbosity(level Level) {
mutex.Lock()
defer mutex.Unlock()
verbosity = level
}
// Log - variable logging against global verbosity
func Log(level Level, args ...interface{}) {
if verify(level) {
internalLog.Print(args...)
}
}
// Logf - variable formatted logging against global verbosity
func Logf(level Level, s string, args ...interface{}) {
if verify(level) {
internalLog.Printf(s, args...)
}
}
// Logln - variable logging with newline against global verbosity
func Logln(level Level, args ...interface{}) {
if verify(level) {
internalLog.Println(args...)
}
}

19
pkg/utils/log/log_test.go Normal file
View File

@@ -0,0 +1,19 @@
package log
import (
"testing"
. "gopkg.in/check.v1"
)
type MySuite struct{}
var _ = Suite(&MySuite{})
func Test(t *testing.T) { TestingT(t) }
func (s *MySuite) TestLogLevel(c *C) {
c.Assert(verbosity, Equals, NormalLOG)
SetVerbosity(QuietLOG)
c.Assert(verbosity, Equals, QuietLOG)
}