forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- v1.20.6
- v1.20.5
- v1.20.4
- v1.20.3
- v1.20.2
- v1.20.1
- v1.20.0
- v1.9.19
- v0.2.38
- v0.2.37
- v0.2.36
- v0.2.35
- v0.2.34
- v0.2.33
- v0.2.32
- v0.2.31
- v0.2.30
- v0.2.29
- v0.2.28
- v0.2.27
- v0.2.26
- v0.2.25
- v0.2.24
- v0.2.23
- v0.2.22
- v0.2.21
- v0.2.20
- v0.2.19
- v0.2.18
- v0.2.17
- v0.2.16
- v0.2.15
- v0.2.14
- v0.2.13
- v0.2.12
- v0.2.11
- v0.2.10
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
Showing
4 changed files
with
97 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,131 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/jinzhu/gorm/utils" | ||
) | ||
|
||
type LogLevel int | ||
// Colors | ||
const ( | ||
Reset = "\033[0m" | ||
Red = "\033[31m" | ||
Green = "\033[32m" | ||
Yellow = "\033[33m" | ||
Blue = "\033[34m" | ||
Magenta = "\033[35m" | ||
Cyan = "\033[36m" | ||
White = "\033[37m" | ||
Redbold = "\033[31;1m" | ||
YellowBold = "\033[33;1m" | ||
) | ||
|
||
var Default Interface = Logger{Writer: log.New(os.Stdout, "\r\n", log.LstdFlags)} | ||
// LogLevel | ||
type LogLevel int | ||
|
||
const ( | ||
Error LogLevel = iota + 1 | ||
Warn | ||
Info | ||
) | ||
|
||
// Writer log writer interface | ||
type Writer interface { | ||
Printf(string, ...interface{}) | ||
} | ||
|
||
type Config struct { | ||
SlowThreshold time.Duration | ||
Colorful bool | ||
LogLevel LogLevel | ||
} | ||
|
||
// Interface logger interface | ||
type Interface interface { | ||
LogMode(LogLevel) Interface | ||
Info(string, ...interface{}) | ||
Warn(string, ...interface{}) | ||
Error(string, ...interface{}) | ||
RunWith(LogLevel, func()) | ||
Trace(begin time.Time, fc func() (string, int64), err error) | ||
} | ||
|
||
// Writer log writer interface | ||
type Writer interface { | ||
Print(...interface{}) | ||
var Default = New(log.New(os.Stdout, "\r\n", log.LstdFlags), Config{ | ||
SlowThreshold: 100 * time.Millisecond, | ||
Colorful: true, | ||
}) | ||
|
||
func New(writer Writer, config Config) Interface { | ||
var ( | ||
infoPrefix = "%s\n[info] " | ||
warnPrefix = "%s\n[warn] " | ||
errPrefix = "%s\n[error] " | ||
tracePrefix = "%s\n[%v] [rows:%d] %s" | ||
traceErrPrefix = "%s\n[%v] [rows:%d] %s" | ||
) | ||
|
||
if config.Colorful { | ||
infoPrefix = Green + "%s\n" + Reset + Green + "[info]" + Reset | ||
warnPrefix = Blue + "%s\n" + Reset + Magenta + "[warn]" + Reset | ||
errPrefix = Magenta + "%s\n" + Reset + Red + "[error]" + Reset | ||
tracePrefix = Green + "%s\n" + Reset + YellowBold + "[%.3fms] " + Green + "[rows:%d]" + Reset + " %s" | ||
traceErrPrefix = Magenta + "%s\n" + Reset + Redbold + "[%.3fms] " + Yellow + "[rows:%d]" + Reset + " %s" | ||
} | ||
|
||
return logger{ | ||
Writer: writer, | ||
Config: config, | ||
infoPrefix: infoPrefix, | ||
warnPrefix: warnPrefix, | ||
errPrefix: errPrefix, | ||
tracePrefix: tracePrefix, | ||
traceErrPrefix: traceErrPrefix, | ||
} | ||
} | ||
|
||
type Logger struct { | ||
type logger struct { | ||
Writer | ||
logLevel LogLevel | ||
Config | ||
infoPrefix, warnPrefix, errPrefix string | ||
tracePrefix, traceErrPrefix string | ||
} | ||
|
||
func (logger Logger) LogMode(level LogLevel) Interface { | ||
return Logger{Writer: logger.Writer, logLevel: level} | ||
// LogMode log mode | ||
func (l logger) LogMode(level LogLevel) Interface { | ||
config := l.Config | ||
config.LogLevel = level | ||
return logger{Writer: l.Writer, Config: config} | ||
} | ||
|
||
// Info print info | ||
func (logger Logger) Info(msg string, data ...interface{}) { | ||
if logger.logLevel >= Info { | ||
logger.Print("[info] " + fmt.Sprintf(msg, data...)) | ||
func (l logger) Info(msg string, data ...interface{}) { | ||
if l.LogLevel >= Info { | ||
l.Printf(l.infoPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)) | ||
} | ||
} | ||
|
||
// Warn print warn messages | ||
func (logger Logger) Warn(msg string, data ...interface{}) { | ||
if logger.logLevel >= Warn { | ||
logger.Print("[warn] " + fmt.Sprintf(msg, data...)) | ||
func (l logger) Warn(msg string, data ...interface{}) { | ||
if l.LogLevel >= Warn { | ||
l.Printf(l.warnPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)) | ||
} | ||
} | ||
|
||
// Error print error messages | ||
func (logger Logger) Error(msg string, data ...interface{}) { | ||
if logger.logLevel >= Error { | ||
logger.Print("[error] " + fmt.Sprintf(msg, data...)) | ||
func (l logger) Error(msg string, data ...interface{}) { | ||
if l.LogLevel >= Error { | ||
l.Printf(l.errPrefix+msg, append([]interface{}{utils.FileWithLineNum()}, data...)) | ||
} | ||
} | ||
|
||
func (logger Logger) RunWith(logLevel LogLevel, fc func()) { | ||
if logger.logLevel >= logLevel { | ||
fc() | ||
// Trace print sql message | ||
func (l logger) Trace(begin time.Time, fc func() (string, int64), err error) { | ||
if elapsed := time.Now().Sub(begin); err != nil || elapsed > l.SlowThreshold { | ||
sql, rows := fc() | ||
l.Printf(l.traceErrPrefix, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql) | ||
} else if l.LogLevel >= Info { | ||
sql, rows := fc() | ||
l.Printf(l.tracePrefix, utils.FileWithLineNum(), float64(elapsed.Nanoseconds())/1e6, rows, sql) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters