mirror of
https://github.com/minio/minio.git
synced 2025-11-07 12:52:58 -05:00
Implement log package as drop-in replacement for handling Debug log-level
This commit is contained in:
34
pkg/utils/log/definitions.go
Normal file
34
pkg/utils/log/definitions.go
Normal 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
|
||||
)
|
||||
26
pkg/utils/log/log-debug.go
Normal file
26
pkg/utils/log/log-debug.go
Normal 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...)
|
||||
}
|
||||
}
|
||||
26
pkg/utils/log/log-normal.go
Normal file
26
pkg/utils/log/log-normal.go
Normal 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
44
pkg/utils/log/log.go
Normal 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
19
pkg/utils/log/log_test.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user