2020-01-28 18:01:35 +03:00
|
|
|
package logger
|
|
|
|
|
2020-01-31 01:35:25 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-01-30 10:14:48 +03:00
|
|
|
type LogLevel int
|
|
|
|
|
2020-02-01 16:48:06 +03:00
|
|
|
var Default Interface = Logger{Writer: log.New(os.Stdout, "\r\n", log.LstdFlags)}
|
2020-01-31 01:35:25 +03:00
|
|
|
|
2020-01-30 10:14:48 +03:00
|
|
|
const (
|
|
|
|
Info LogLevel = iota + 1
|
|
|
|
Warn
|
|
|
|
Error
|
|
|
|
)
|
|
|
|
|
2020-01-28 18:01:35 +03:00
|
|
|
// Interface logger interface
|
|
|
|
type Interface interface {
|
2020-01-30 10:14:48 +03:00
|
|
|
LogMode(LogLevel) Interface
|
2020-01-31 01:35:25 +03:00
|
|
|
Info(string, ...interface{})
|
|
|
|
Warn(string, ...interface{})
|
|
|
|
Error(string, ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writer log writer interface
|
|
|
|
type Writer interface {
|
|
|
|
Print(...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
Writer
|
|
|
|
logLevel LogLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger Logger) LogMode(level LogLevel) Interface {
|
|
|
|
return Logger{Writer: logger.Writer, logLevel: level}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info print info
|
|
|
|
func (logger Logger) Info(msg string, data ...interface{}) {
|
2020-02-01 16:48:06 +03:00
|
|
|
if logger.logLevel <= Info {
|
2020-01-31 01:35:25 +03:00
|
|
|
logger.Print("[info] " + fmt.Sprintf(msg, data...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn print warn messages
|
|
|
|
func (logger Logger) Warn(msg string, data ...interface{}) {
|
2020-02-01 16:48:06 +03:00
|
|
|
if logger.logLevel <= Warn {
|
2020-01-31 01:35:25 +03:00
|
|
|
logger.Print("[warn] " + fmt.Sprintf(msg, data...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error print error messages
|
|
|
|
func (logger Logger) Error(msg string, data ...interface{}) {
|
2020-02-01 16:48:06 +03:00
|
|
|
if logger.logLevel <= Error {
|
2020-01-31 01:35:25 +03:00
|
|
|
logger.Print("[error] " + fmt.Sprintf(msg, data...))
|
|
|
|
}
|
2020-01-28 18:01:35 +03:00
|
|
|
}
|