You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
3.7 KiB
Go

package log
import (
"fmt"
"io"
"os"
"sync"
)
7 years ago
var Exit = os.Exit
type Logger struct {
7 years ago
m sync.Mutex
Level Level
Formatter Formatter
Out io.Writer
}
7 years ago
func New() *Logger {
return &Logger{
Level: INFO,
Formatter: new(simpleFormatter),
Out: os.Stdout,
}
}
7 years ago
func (l *Logger) IsDebugEnabled() bool {
return l.Level >= DEBUG
}
7 years ago
func (l *Logger) IsInfoEnabled() bool {
return l.Level >= INFO
}
7 years ago
func (l *Logger) IsPrintEnabled() bool {
return l.Level > OFF
}
7 years ago
func (l *Logger) IsWarnEnabled() bool {
return l.Level >= WARN
}
7 years ago
func (l *Logger) IsErrorEnabled() bool {
return l.Level >= ERROR
}
7 years ago
func (l *Logger) IsPanicEnabled() bool {
return l.Level >= PANIC
}
7 years ago
func (l *Logger) IsFatalEnabled() bool {
return l.Level >= FATAL
}
7 years ago
func (l *Logger) IsDisabled() bool {
7 years ago
return l.Level <= OFF
}
7 years ago
func (l *Logger) Debug(obj ...interface{}) {
if l.Level >= DEBUG {
l.log(DEBUG, fmt.Sprint(obj...))
}
}
7 years ago
func (l *Logger) Info(obj ...interface{}) {
if l.Level >= INFO {
l.log(INFO, fmt.Sprint(obj...))
}
}
7 years ago
func (l *Logger) Print(obj ...interface{}) {
if l.Level != OFF {
l.log(INFO, fmt.Sprint(obj...))
}
}
7 years ago
func (l *Logger) Warn(obj ...interface{}) {
if l.Level >= WARN {
l.log(WARN, fmt.Sprint(obj...))
}
}
7 years ago
func (l *Logger) Error(obj ...interface{}) {
if l.Level >= ERROR {
l.log(ERROR, fmt.Sprint(obj...))
}
}
7 years ago
func (l *Logger) Panic(obj ...interface{}) {
if l.Level >= PANIC {
l.log(PANIC, fmt.Sprint(obj...))
}
panic(fmt.Sprint(obj...))
}
7 years ago
func (l *Logger) Fatal(obj ...interface{}) {
if l.Level >= FATAL {
l.log(FATAL, fmt.Sprint(obj...))
}
7 years ago
Exit(1)
}
7 years ago
func (l *Logger) Debugln(obj ...interface{}) {
if l.Level >= DEBUG {
l.log(DEBUG, vsprintln(obj...))
}
}
7 years ago
func (l *Logger) Infoln(obj ...interface{}) {
if l.Level >= INFO {
l.log(INFO, vsprintln(obj...))
}
}
7 years ago
func (l *Logger) Println(obj ...interface{}) {
if l.Level != OFF {
l.log(INFO, vsprintln(obj...))
}
}
7 years ago
func (l *Logger) Warnln(obj ...interface{}) {
if l.Level >= WARN {
l.log(WARN, vsprintln(obj...))
}
}
7 years ago
func (l *Logger) Errorln(obj ...interface{}) {
if l.Level >= ERROR {
l.log(ERROR, vsprintln(obj...))
}
}
7 years ago
func (l *Logger) Panicln(obj ...interface{}) {
if l.Level >= PANIC {
l.log(PANIC, vsprintln(obj...))
}
7 years ago
panic(vsprintln(obj...))
}
7 years ago
func (l *Logger) Fatalln(obj ...interface{}) {
if l.Level >= FATAL {
l.log(FATAL, vsprintln(obj...))
}
7 years ago
Exit(1)
}
func (l *Logger) Debugf(msg string, args ...interface{}) {
7 years ago
if l.Level >= DEBUG {
l.log(DEBUG, fmt.Sprintf(msg, args...))
}
}
func (l *Logger) Infof(msg string, args ...interface{}) {
7 years ago
if l.Level >= INFO {
l.log(INFO, fmt.Sprintf(msg, args...))
}
}
func (l *Logger) Printf(msg string, args ...interface{}) {
if l.Level != OFF {
l.log(INFO, fmt.Sprintf(msg, args...))
}
}
func (l *Logger) Warnf(msg string, args ...interface{}) {
7 years ago
if l.Level >= WARN {
l.log(WARN, fmt.Sprintf(msg, args...))
}
}
func (l *Logger) Errorf(msg string, args ...interface{}) {
7 years ago
if l.Level >= ERROR {
l.log(ERROR, fmt.Sprintf(msg, args...))
}
}
7 years ago
func (l *Logger) Panicf(msg string, args ...interface{}) {
if l.Level >= PANIC {
l.log(PANIC, fmt.Sprintf(msg, args...))
}
7 years ago
panic(fmt.Sprintf(msg, args...))
}
7 years ago
func (l *Logger) Fatalf(msg string, args ...interface{}) {
if l.Level >= FATAL {
l.log(FATAL, fmt.Sprintf(msg, args...))
}
7 years ago
Exit(1)
7 years ago
}
7 years ago
func (l *Logger) log(level Level, msg string) {
line := l.Formatter.Format(level, msg, l)
l.m.Lock()
defer l.m.Unlock()
7 years ago
_, err := l.Out.Write(line)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write log, %v\n", err)
}
}
// vsprintln => spaces are always added between operands
func vsprintln(obj ...interface{}) string {
msg := fmt.Sprintln(obj...)
return msg[:len(msg)-1]
}