go/log/log.go

117 lines
2.2 KiB
Go
Raw Normal View History

2014-01-03 10:26:29 +04:00
package log
import (
"fmt"
"log"
"os"
)
const (
LevelTrace = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
)
var LevelName [6]string = [6]string{"Trace", "Debug", "Info", "Warn", "Error", "Fatal"}
2014-03-24 17:45:22 +04:00
type Logger struct {
2014-01-03 10:26:29 +04:00
logger *log.Logger
level int
handler Handler
}
2014-03-24 17:45:22 +04:00
func New(handler Handler, flag int) *Logger {
var l = new(Logger)
2014-01-03 10:26:29 +04:00
l.logger = log.New(handler, "", flag) //log.LstdFlags|log.Lshortfile)
l.level = LevelInfo
l.handler = handler
return l
}
2014-03-24 17:45:22 +04:00
func NewDefault(handler Handler) *Logger {
2014-01-03 10:26:29 +04:00
return New(handler, log.LstdFlags|log.Lshortfile)
}
func newStdHandler() *StreamHandler {
h, _ := NewDefaultStreamHandler(os.Stdout)
return h
}
var std = NewDefault(newStdHandler())
2014-03-24 17:45:22 +04:00
func (l *Logger) Close() {
2014-01-03 10:26:29 +04:00
l.handler.Close()
}
2014-03-24 17:45:22 +04:00
func (l *Logger) SetLevel(level int) {
2014-01-03 10:26:29 +04:00
l.level = level
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Output(callDepth int, level int, format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
if l.level <= level {
f := fmt.Sprintf("[%s] %s", LevelName[level], format)
s := fmt.Sprintf(f, v...)
l.logger.Output(callDepth, s)
}
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Write(s string) {
2014-01-03 10:26:29 +04:00
l.logger.Output(3, s)
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Trace(format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
l.Output(3, LevelTrace, format, v...)
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Debug(format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
l.Output(3, LevelDebug, format, v...)
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Info(format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
l.Output(3, LevelInfo, format, v...)
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Warn(format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
l.Output(3, LevelWarn, format, v...)
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Error(format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
l.Output(3, LevelError, format, v...)
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Fatal(format string, v ...interface{}) {
2014-01-03 10:26:29 +04:00
l.Output(3, LevelFatal, format, v...)
}
func SetLevel(level int) {
std.SetLevel(level)
}
func Trace(format string, v ...interface{}) {
std.Output(3, LevelTrace, format, v...)
}
func Debug(format string, v ...interface{}) {
std.Output(3, LevelDebug, format, v...)
}
func Info(format string, v ...interface{}) {
std.Output(3, LevelInfo, format, v...)
}
func Warn(format string, v ...interface{}) {
std.Output(3, LevelWarn, format, v...)
}
func Error(format string, v ...interface{}) {
std.Output(3, LevelError, format, v...)
}
func Fatal(format string, v ...interface{}) {
std.Output(3, LevelFatal, format, v...)
}