2016-11-04 03:46:01 +05:30
|
|
|
// 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.
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
package log
|
2016-11-04 03:46:01 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2020-03-22 20:42:55 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// LogLevel defines a log level
|
|
|
|
type LogLevel int
|
2016-11-04 03:46:01 +05:30
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// 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
|
2016-11-04 03:46:01 +05:30
|
|
|
)
|
|
|
|
|
2017-01-23 14:41:18 +05:30
|
|
|
// default log options
|
2016-11-04 03:46:01 +05:30
|
|
|
const (
|
|
|
|
DEFAULT_LOG_PREFIX = "[xorm]"
|
|
|
|
DEFAULT_LOG_FLAG = log.Ldate | log.Lmicroseconds
|
2020-03-22 20:42:55 +05:30
|
|
|
DEFAULT_LOG_LEVEL = LOG_DEBUG
|
2016-11-04 03:46:01 +05:30
|
|
|
)
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// 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{}
|
2016-11-04 03:46:01 +05:30
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// DiscardLogger don't log implementation for ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
type DiscardLogger struct{}
|
|
|
|
|
2017-01-03 13:50:28 +05:30
|
|
|
// Debug empty implementation
|
|
|
|
func (DiscardLogger) Debug(v ...interface{}) {}
|
|
|
|
|
|
|
|
// Debugf empty implementation
|
2016-11-04 03:46:01 +05:30
|
|
|
func (DiscardLogger) Debugf(format string, v ...interface{}) {}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
|
|
|
// Error empty implementation
|
|
|
|
func (DiscardLogger) Error(v ...interface{}) {}
|
|
|
|
|
|
|
|
// Errorf empty implementation
|
2016-11-04 03:46:01 +05:30
|
|
|
func (DiscardLogger) Errorf(format string, v ...interface{}) {}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
|
|
|
// 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
|
2020-03-22 20:42:55 +05:30
|
|
|
func (DiscardLogger) Level() LogLevel {
|
|
|
|
return LOG_UNKNOWN
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
|
|
|
// SetLevel empty implementation
|
2020-03-22 20:42:55 +05:30
|
|
|
func (DiscardLogger) SetLevel(l LogLevel) {}
|
2017-01-03 13:50:28 +05:30
|
|
|
|
|
|
|
// ShowSQL empty implementation
|
|
|
|
func (DiscardLogger) ShowSQL(show ...bool) {}
|
|
|
|
|
|
|
|
// IsShowSQL empty implementation
|
2016-11-04 03:46:01 +05:30
|
|
|
func (DiscardLogger) IsShowSQL() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// SimpleLogger is the default implment of ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
type SimpleLogger struct {
|
|
|
|
DEBUG *log.Logger
|
|
|
|
ERR *log.Logger
|
|
|
|
INFO *log.Logger
|
|
|
|
WARN *log.Logger
|
2020-03-22 20:42:55 +05:30
|
|
|
level LogLevel
|
2016-11-04 03:46:01 +05:30
|
|
|
showSQL bool
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
var _ Logger = &SimpleLogger{}
|
2016-11-04 03:46:01 +05:30
|
|
|
|
|
|
|
// 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
|
2020-03-22 20:42:55 +05:30
|
|
|
func NewSimpleLogger3(out io.Writer, prefix string, flag int, l LogLevel) *SimpleLogger {
|
2016-11-04 03:46:01 +05:30
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Error implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Error(v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_ERR {
|
|
|
|
s.ERR.Output(2, fmt.Sprintln(v...))
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Errorf implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Errorf(format string, v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_ERR {
|
2016-11-04 03:46:01 +05:30
|
|
|
s.ERR.Output(2, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Debug implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Debug(v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_DEBUG {
|
|
|
|
s.DEBUG.Output(2, fmt.Sprintln(v...))
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Debugf implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Debugf(format string, v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_DEBUG {
|
2016-11-04 03:46:01 +05:30
|
|
|
s.DEBUG.Output(2, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Info implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Info(v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_INFO {
|
|
|
|
s.INFO.Output(2, fmt.Sprintln(v...))
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Infof implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Infof(format string, v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_INFO {
|
2016-11-04 03:46:01 +05:30
|
|
|
s.INFO.Output(2, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Warn implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Warn(v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_WARNING {
|
|
|
|
s.WARN.Output(2, fmt.Sprintln(v...))
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Warnf implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) Warnf(format string, v ...interface{}) {
|
2020-03-22 20:42:55 +05:30
|
|
|
if s.level <= LOG_WARNING {
|
2016-11-04 03:46:01 +05:30
|
|
|
s.WARN.Output(2, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// Level implement ILogger
|
|
|
|
func (s *SimpleLogger) Level() LogLevel {
|
2016-11-04 03:46:01 +05:30
|
|
|
return s.level
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// SetLevel implement ILogger
|
|
|
|
func (s *SimpleLogger) SetLevel(l LogLevel) {
|
2016-11-04 03:46:01 +05:30
|
|
|
s.level = l
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// ShowSQL implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) ShowSQL(show ...bool) {
|
|
|
|
if len(show) == 0 {
|
|
|
|
s.showSQL = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.showSQL = show[0]
|
|
|
|
}
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
// IsShowSQL implement ILogger
|
2016-11-04 03:46:01 +05:30
|
|
|
func (s *SimpleLogger) IsShowSQL() bool {
|
|
|
|
return s.showSQL
|
|
|
|
}
|