aboutsummaryrefslogtreecommitdiff
path: root/logger/logger.go
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-09-15 01:06:38 +0200
committern-peugnet <n.peugnet@free.fr>2021-09-15 01:17:43 +0200
commit6b64e08ed77e7752af307b82bbe1aa14200f269a (patch)
tree9f761c8d74cd32da98ac4fbadfcbf875eae98b7c /logger/logger.go
parentf21468b03329a3751a50eb829e07271d23ce4720 (diff)
downloaddna-backup-6b64e08ed77e7752af307b82bbe1aa14200f269a.tar.gz
dna-backup-6b64e08ed77e7752af307b82bbe1aa14200f269a.zip
logger fixes and better tests
Diffstat (limited to 'logger/logger.go')
-rw-r--r--logger/logger.go50
1 files changed, 18 insertions, 32 deletions
diff --git a/logger/logger.go b/logger/logger.go
index 9cd4b33..bc2b3c8 100644
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -29,8 +29,8 @@ const (
// Severity tags.
const (
tagInfo = "\033[0m[INFO] "
- tagWarning = "\033[1;33m[WARN] "
- tagError = "\033[1;31m[ERROR] "
+ tagWarning = "\033[33m[WARN] "
+ tagError = "\033[31m[ERROR] "
tagFatal = "\033[1;31m[FATAL] "
)
@@ -46,13 +46,13 @@ var (
// initialize resets defaultLogger. Which allows tests to reset environment.
func initialize() {
defaultLogger = &Logger{
- loggers: []logger{
+ loggers: [4]logger{
log.New(os.Stderr, tagInfo, flags),
log.New(os.Stderr, tagWarning, flags),
log.New(os.Stderr, tagError, flags),
log.New(os.Stderr, tagFatal, flags),
},
- level: 3,
+ minSeverity: 0,
}
}
@@ -67,14 +67,16 @@ func init() {
// generated logger, subsequent calls to Init will only return the generated
// logger.
func Init(level int) *Logger {
-
- loggers := []logger{
- log.New(os.Stderr, tagInfo, flags),
- log.New(os.Stderr, tagWarning, flags),
- log.New(os.Stderr, tagError, flags),
- log.New(os.Stderr, tagFatal, flags),
+ l := Logger{
+ loggers: [4]logger{
+ log.New(os.Stderr, tagInfo, flags),
+ log.New(os.Stderr, tagWarning, flags),
+ log.New(os.Stderr, tagError, flags),
+ log.New(os.Stderr, tagFatal, flags),
+ },
+ minSeverity: sFatal - severity(level),
+ initialized: true,
}
- l := Logger{loggers: loggers, level: level, initialized: true}
logLock.Lock()
defer logLock.Unlock()
@@ -88,13 +90,13 @@ func Init(level int) *Logger {
// A Logger represents an active logging object. Multiple loggers can be used
// simultaneously even if they are using the same writers.
type Logger struct {
- loggers []logger
- level int
+ loggers [4]logger
+ minSeverity severity
initialized bool
}
func (l *Logger) output(s severity, depth int, txt string) {
- if s < sFatal-severity(l.level) {
+ if s < l.minSeverity {
return
}
logLock.Lock()
@@ -125,12 +127,6 @@ func (l *Logger) Info(v ...interface{}) {
l.output(sInfo, 0, fmt.Sprint(v...))
}
-// InfoDepth acts as Info but uses depth to determine which call frame to log.
-// InfoDepth(0, "msg") is the same as Info("msg").
-func (l *Logger) InfoDepth(depth int, v ...interface{}) {
- l.output(sInfo, depth, fmt.Sprint(v...))
-}
-
// Infof logs with the Info severity.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Infof(format string, v ...interface{}) {
@@ -143,12 +139,6 @@ func (l *Logger) Warning(v ...interface{}) {
l.output(sWarning, 0, fmt.Sprint(v...))
}
-// WarningDepth acts as Warning but uses depth to determine which call frame to log.
-// WarningDepth(0, "msg") is the same as Warning("msg").
-func (l *Logger) WarningDepth(depth int, v ...interface{}) {
- l.output(sWarning, depth, fmt.Sprint(v...))
-}
-
// Warningf logs with the Warning severity.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Warningf(format string, v ...interface{}) {
@@ -161,12 +151,6 @@ func (l *Logger) Error(v ...interface{}) {
l.output(sError, 0, fmt.Sprint(v...))
}
-// ErrorDepth acts as Error but uses depth to determine which call frame to log.
-// ErrorDepth(0, "msg") is the same as Error("msg").
-func (l *Logger) ErrorDepth(depth int, v ...interface{}) {
- l.output(sError, depth, fmt.Sprint(v...))
-}
-
// Errorf logs with the Error severity.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Errorf(format string, v ...interface{}) {
@@ -193,6 +177,7 @@ func (l *Logger) Panicf(format string, v ...interface{}) {
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Fatal(v ...interface{}) {
l.output(sFatal, 0, fmt.Sprint(v...))
+ debug.PrintStack()
os.Exit(1)
}
@@ -200,6 +185,7 @@ func (l *Logger) Fatal(v ...interface{}) {
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.output(sFatal, 0, fmt.Sprintf(format, v...))
+ debug.PrintStack()
os.Exit(1)
}