astraproxy/condlog.go

59 lines
1.2 KiB
Go
Raw Normal View History

2020-05-19 21:53:13 +02:00
package main
import (
2021-02-26 08:09:55 +01:00
"fmt"
"log"
2020-05-19 21:53:13 +02:00
)
const (
2021-02-26 08:09:55 +01:00
CRITICAL = 50
ERROR = 40
WARNING = 30
INFO = 20
DEBUG = 10
NOTSET = 0
2020-05-19 21:53:13 +02:00
)
type CondLogger struct {
2021-02-26 08:09:55 +01:00
logger *log.Logger
verbosity int
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) Log(verb int, format string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
if verb >= cl.verbosity {
return cl.logger.Output(2, fmt.Sprintf(format, v...))
}
return nil
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) log(verb int, format string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
if verb >= cl.verbosity {
return cl.logger.Output(3, fmt.Sprintf(format, v...))
}
return nil
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) Critical(s string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
return cl.log(CRITICAL, "CRITICAL "+s, v...)
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) Error(s string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
return cl.log(ERROR, "ERROR "+s, v...)
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) Warning(s string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
return cl.log(WARNING, "WARNING "+s, v...)
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) Info(s string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
return cl.log(INFO, "INFO "+s, v...)
2020-05-19 21:53:13 +02:00
}
func (cl *CondLogger) Debug(s string, v ...interface{}) error {
2021-02-26 08:09:55 +01:00
return cl.log(DEBUG, "DEBUG "+s, v...)
2020-05-19 21:53:13 +02:00
}
func NewCondLogger(logger *log.Logger, verbosity int) *CondLogger {
2021-02-26 08:09:55 +01:00
return &CondLogger{verbosity: verbosity, logger: logger}
2020-05-19 21:53:13 +02:00
}