You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
4.8 KiB
209 lines
4.8 KiB
8 years ago
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a BSD-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
5 years ago
|
package log
|
||
8 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"log"
|
||
5 years ago
|
)
|
||
|
|
||
|
// LogLevel defines a log level
|
||
|
type LogLevel int
|
||
8 years ago
|
|
||
5 years ago
|
// enumerate all LogLevels
|
||
|
const (
|
||
|
// !nashtsai! following level also match syslog.Priority value
|
||
|
LOG_DEBUG LogLevel = iota
|
||
|
LOG_INFO
|
||
|
LOG_WARNING
|
||
|
LOG_ERR
|
||
|
LOG_OFF
|
||
|
LOG_UNKNOWN
|
||
8 years ago
|
)
|
||
|
|
||
8 years ago
|
// default log options
|
||
8 years ago
|
const (
|
||
|
DEFAULT_LOG_PREFIX = "[xorm]"
|
||
|
DEFAULT_LOG_FLAG = log.Ldate | log.Lmicroseconds
|
||
5 years ago
|
DEFAULT_LOG_LEVEL = LOG_DEBUG
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
// Logger is a logger interface
|
||
|
type Logger interface {
|
||
|
Debug(v ...interface{})
|
||
|
Debugf(format string, v ...interface{})
|
||
|
Error(v ...interface{})
|
||
|
Errorf(format string, v ...interface{})
|
||
|
Info(v ...interface{})
|
||
|
Infof(format string, v ...interface{})
|
||
|
Warn(v ...interface{})
|
||
|
Warnf(format string, v ...interface{})
|
||
|
|
||
|
Level() LogLevel
|
||
|
SetLevel(l LogLevel)
|
||
|
|
||
|
ShowSQL(show ...bool)
|
||
|
IsShowSQL() bool
|
||
|
}
|
||
|
|
||
|
var _ Logger = DiscardLogger{}
|
||
8 years ago
|
|
||
5 years ago
|
// DiscardLogger don't log implementation for ILogger
|
||
8 years ago
|
type DiscardLogger struct{}
|
||
|
|
||
8 years ago
|
// Debug empty implementation
|
||
|
func (DiscardLogger) Debug(v ...interface{}) {}
|
||
|
|
||
|
// Debugf empty implementation
|
||
8 years ago
|
func (DiscardLogger) Debugf(format string, v ...interface{}) {}
|
||
8 years ago
|
|
||
|
// Error empty implementation
|
||
|
func (DiscardLogger) Error(v ...interface{}) {}
|
||
|
|
||
|
// Errorf empty implementation
|
||
8 years ago
|
func (DiscardLogger) Errorf(format string, v ...interface{}) {}
|
||
8 years ago
|
|
||
|
// Info empty implementation
|
||
|
func (DiscardLogger) Info(v ...interface{}) {}
|
||
|
|
||
|
// Infof empty implementation
|
||
|
func (DiscardLogger) Infof(format string, v ...interface{}) {}
|
||
|
|
||
|
// Warn empty implementation
|
||
|
func (DiscardLogger) Warn(v ...interface{}) {}
|
||
|
|
||
|
// Warnf empty implementation
|
||
|
func (DiscardLogger) Warnf(format string, v ...interface{}) {}
|
||
|
|
||
|
// Level empty implementation
|
||
5 years ago
|
func (DiscardLogger) Level() LogLevel {
|
||
|
return LOG_UNKNOWN
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
// SetLevel empty implementation
|
||
5 years ago
|
func (DiscardLogger) SetLevel(l LogLevel) {}
|
||
8 years ago
|
|
||
|
// ShowSQL empty implementation
|
||
|
func (DiscardLogger) ShowSQL(show ...bool) {}
|
||
|
|
||
|
// IsShowSQL empty implementation
|
||
8 years ago
|
func (DiscardLogger) IsShowSQL() bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
5 years ago
|
// SimpleLogger is the default implment of ILogger
|
||
8 years ago
|
type SimpleLogger struct {
|
||
|
DEBUG *log.Logger
|
||
|
ERR *log.Logger
|
||
|
INFO *log.Logger
|
||
|
WARN *log.Logger
|
||
5 years ago
|
level LogLevel
|
||
8 years ago
|
showSQL bool
|
||
|
}
|
||
|
|
||
5 years ago
|
var _ Logger = &SimpleLogger{}
|
||
8 years ago
|
|
||
|
// NewSimpleLogger use a special io.Writer as logger output
|
||
|
func NewSimpleLogger(out io.Writer) *SimpleLogger {
|
||
|
return NewSimpleLogger2(out, DEFAULT_LOG_PREFIX, DEFAULT_LOG_FLAG)
|
||
|
}
|
||
|
|
||
|
// NewSimpleLogger2 let you customrize your logger prefix and flag
|
||
|
func NewSimpleLogger2(out io.Writer, prefix string, flag int) *SimpleLogger {
|
||
|
return NewSimpleLogger3(out, prefix, flag, DEFAULT_LOG_LEVEL)
|
||
|
}
|
||
|
|
||
|
// NewSimpleLogger3 let you customrize your logger prefix and flag and logLevel
|
||
5 years ago
|
func NewSimpleLogger3(out io.Writer, prefix string, flag int, l LogLevel) *SimpleLogger {
|
||
8 years ago
|
return &SimpleLogger{
|
||
|
DEBUG: log.New(out, fmt.Sprintf("%s [debug] ", prefix), flag),
|
||
|
ERR: log.New(out, fmt.Sprintf("%s [error] ", prefix), flag),
|
||
|
INFO: log.New(out, fmt.Sprintf("%s [info] ", prefix), flag),
|
||
|
WARN: log.New(out, fmt.Sprintf("%s [warn] ", prefix), flag),
|
||
|
level: l,
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Error implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Error(v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_ERR {
|
||
|
s.ERR.Output(2, fmt.Sprintln(v...))
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Errorf implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Errorf(format string, v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_ERR {
|
||
8 years ago
|
s.ERR.Output(2, fmt.Sprintf(format, v...))
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Debug implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Debug(v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_DEBUG {
|
||
|
s.DEBUG.Output(2, fmt.Sprintln(v...))
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Debugf implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Debugf(format string, v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_DEBUG {
|
||
8 years ago
|
s.DEBUG.Output(2, fmt.Sprintf(format, v...))
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Info implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Info(v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_INFO {
|
||
|
s.INFO.Output(2, fmt.Sprintln(v...))
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Infof implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Infof(format string, v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_INFO {
|
||
8 years ago
|
s.INFO.Output(2, fmt.Sprintf(format, v...))
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Warn implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Warn(v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_WARNING {
|
||
|
s.WARN.Output(2, fmt.Sprintln(v...))
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Warnf implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) Warnf(format string, v ...interface{}) {
|
||
5 years ago
|
if s.level <= LOG_WARNING {
|
||
8 years ago
|
s.WARN.Output(2, fmt.Sprintf(format, v...))
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// Level implement ILogger
|
||
|
func (s *SimpleLogger) Level() LogLevel {
|
||
8 years ago
|
return s.level
|
||
|
}
|
||
|
|
||
5 years ago
|
// SetLevel implement ILogger
|
||
|
func (s *SimpleLogger) SetLevel(l LogLevel) {
|
||
8 years ago
|
s.level = l
|
||
|
}
|
||
|
|
||
5 years ago
|
// ShowSQL implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) ShowSQL(show ...bool) {
|
||
|
if len(show) == 0 {
|
||
|
s.showSQL = true
|
||
|
return
|
||
|
}
|
||
|
s.showSQL = show[0]
|
||
|
}
|
||
|
|
||
5 years ago
|
// IsShowSQL implement ILogger
|
||
8 years ago
|
func (s *SimpleLogger) IsShowSQL() bool {
|
||
|
return s.showSQL
|
||
|
}
|