134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
package logging
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
)
|
|
|
|
// Loglevel is used to set the current loglevel of Logger.
|
|
type Loglevel int
|
|
|
|
// These are available loglevels for Logger.
|
|
const (
|
|
NONE Loglevel = 0
|
|
ERROR Loglevel = 1
|
|
WARN Loglevel = 2
|
|
INFO Loglevel = 3
|
|
DEBUG Loglevel = 4
|
|
)
|
|
|
|
// Logger uses multiple instances of log.Logger to provide logging methods for multiple loglevels.
|
|
type Logger struct {
|
|
debug *log.Logger
|
|
info *log.Logger
|
|
warn *log.Logger
|
|
error *log.Logger
|
|
logDebug bool
|
|
logInfo bool
|
|
logWarn bool
|
|
logError bool
|
|
}
|
|
|
|
// NewLogger prepares a new instance of Logger for custom use.
|
|
func NewLogger(stdOut io.Writer, stdErr io.Writer, logLevel Loglevel) Logger {
|
|
newLogger := Logger{}
|
|
newLogger.debug = log.New(stdOut, "DEBUG ", 0)
|
|
newLogger.info = log.New(stdOut, "INFO ", 0)
|
|
newLogger.warn = log.New(stdErr, "WARN ", 0)
|
|
newLogger.error = log.New(stdErr, "ERROR ", 0)
|
|
newLogger.SetLogLevel(logLevel)
|
|
return newLogger
|
|
}
|
|
|
|
// SetLogLevel can be used to change the loglevel of Logger on the fly.
|
|
func (l *Logger) SetLogLevel(logLevel Loglevel) {
|
|
l.logDebug = (logLevel >= DEBUG)
|
|
l.logInfo = (logLevel >= INFO)
|
|
l.logWarn = (logLevel >= WARN)
|
|
l.logError = (logLevel >= ERROR)
|
|
}
|
|
|
|
// Debug is an alias for log.Logger.Print() with severity DEBUG.
|
|
func (l *Logger) Debug(v ...interface{}) {
|
|
if l.logDebug {
|
|
l.debug.Print(v...)
|
|
}
|
|
}
|
|
|
|
// Debugln is an alias for log.Logger.Println() with severity DEBUG.
|
|
func (l *Logger) Debugln(v ...interface{}) {
|
|
if l.logDebug {
|
|
l.debug.Println(v...)
|
|
}
|
|
}
|
|
|
|
// Debugf is an alias for log.Logger.Printf() with severity DEBUG.
|
|
func (l *Logger) Debugf(format string, v ...interface{}) {
|
|
if l.logDebug {
|
|
l.debug.Printf(format, v...)
|
|
}
|
|
}
|
|
|
|
// Info is an alias for log.Logger.Print() with severity INFO.
|
|
func (l *Logger) Info(v ...interface{}) {
|
|
if l.logInfo {
|
|
l.info.Print(v...)
|
|
}
|
|
}
|
|
|
|
// Infoln is an alias for log.Logger.Println() with severity INFO.
|
|
func (l *Logger) Infoln(v ...interface{}) {
|
|
if l.logInfo {
|
|
l.info.Println(v...)
|
|
}
|
|
}
|
|
|
|
// Infof is an alias for log.Logger.Printf() with severity INFO.
|
|
func (l *Logger) Infof(format string, v ...interface{}) {
|
|
if l.logInfo {
|
|
l.info.Printf(format, v...)
|
|
}
|
|
}
|
|
|
|
// Warn is an alias for log.Logger.Print() with severity WARN.
|
|
func (l *Logger) Warn(v ...interface{}) {
|
|
if l.logWarn {
|
|
l.warn.Print(v...)
|
|
}
|
|
}
|
|
|
|
// Warnln is an alias for log.Logger.Println() with severity WARN.
|
|
func (l *Logger) Warnln(v ...interface{}) {
|
|
if l.logWarn {
|
|
l.warn.Println(v...)
|
|
}
|
|
}
|
|
|
|
// Warnf is an alias for log.Logger.Printf() with severity WARN.
|
|
func (l *Logger) Warnf(format string, v ...interface{}) {
|
|
if l.logWarn {
|
|
l.warn.Printf(format, v...)
|
|
}
|
|
}
|
|
|
|
// Error is an alias for log.Logger.Print() with severity ERROR.
|
|
func (l *Logger) Error(v ...interface{}) {
|
|
if l.logError {
|
|
l.error.Print(v...)
|
|
}
|
|
}
|
|
|
|
// Errorln is an alias for log.Logger.Println() with severity ERROR.
|
|
func (l *Logger) Errorln(v ...interface{}) {
|
|
if l.logError {
|
|
l.error.Println(v...)
|
|
}
|
|
}
|
|
|
|
// Errorf is an alias for log.Logger.Printf() with severity ERROR.
|
|
func (l *Logger) Errorf(format string, v ...interface{}) {
|
|
if l.logError {
|
|
l.error.Printf(format, v...)
|
|
}
|
|
}
|