gorm/logger/logger.go

61 lines
1.1 KiB
Go
Raw Normal View History

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-01-31 01:35:25 +03:00
var Default Interface = Logger{Writer: log.New(os.Stdout, "\r\n", 0)}
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{}) {
if logger.logLevel >= Info {
logger.Print("[info] " + fmt.Sprintf(msg, data...))
}
}
// Warn print warn messages
func (logger Logger) Warn(msg string, data ...interface{}) {
if logger.logLevel >= Warn {
logger.Print("[warn] " + fmt.Sprintf(msg, data...))
}
}
// Error print error messages
func (logger Logger) Error(msg string, data ...interface{}) {
if logger.logLevel >= Error {
logger.Print("[error] " + fmt.Sprintf(msg, data...))
}
2020-01-28 18:01:35 +03:00
}