add: logger tests and coverage
This commit is contained in:
parent
f3733e18a4
commit
9ec0d1b676
51
logger.go
51
logger.go
@ -12,20 +12,26 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
RESET = "\033[0m"
|
||||||
|
|
||||||
|
CYAN = "\033[36;1m"
|
||||||
|
MAGENTA = "\033[35m"
|
||||||
|
RED = "\033[36;31m"
|
||||||
|
REDBOLD = "\033[31;1m"
|
||||||
|
YELLOW = "\033[33m"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
|
defaultLogger = Logger{log.New(os.Stdout, "\r\n", 0)}
|
||||||
sqlRegexp = regexp.MustCompile(`\?`)
|
sqlRegexp = regexp.MustCompile(`\?`)
|
||||||
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
|
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`)
|
||||||
)
|
reset = ""
|
||||||
|
cyan = ""
|
||||||
var (
|
magenta = ""
|
||||||
reset = "\033[0m"
|
red = ""
|
||||||
|
redBold = ""
|
||||||
cyan = "\033[36;1m"
|
yellow = ""
|
||||||
magenta = "\033[35m"
|
|
||||||
red = "\033[36;31m"
|
|
||||||
redBold = "\033[31;1m"
|
|
||||||
yellow = "\033[33m"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isPrintable(s string) bool {
|
func isPrintable(s string) bool {
|
||||||
@ -37,21 +43,30 @@ func isPrintable(s string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func noColor() bool {
|
func NoColor() bool {
|
||||||
// https://no-color.org/
|
// https://no-color.org/
|
||||||
return os.Getenv("NO_COLOR") != ""
|
return os.Getenv("NO_COLOR") != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogFormatter is a default logger with timestamps, sql error handling, loglevel and NO_COLOR support
|
// LogFormatter is a default logger with timestamps, sql error handling, loglevel and NO_COLOR support
|
||||||
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
|
var LogFormatter = func(values ...interface{}) (messages []interface{}) {
|
||||||
suppressColor := noColor()
|
suppressColor := NoColor()
|
||||||
if suppressColor {
|
if suppressColor {
|
||||||
reset = ""
|
// To avoid string templates with _and_ without colors, just overwrite color values
|
||||||
redBold = ""
|
// when no color will print out.
|
||||||
yellow = ""
|
|
||||||
magenta = ""
|
|
||||||
cyan = ""
|
|
||||||
red = ""
|
red = ""
|
||||||
|
cyan = ""
|
||||||
|
reset = ""
|
||||||
|
yellow = ""
|
||||||
|
redBold = ""
|
||||||
|
magenta = ""
|
||||||
|
} else {
|
||||||
|
red = RED
|
||||||
|
cyan = CYAN
|
||||||
|
reset = RESET
|
||||||
|
yellow = YELLOW
|
||||||
|
redBold = REDBOLD
|
||||||
|
magenta = MAGENTA
|
||||||
}
|
}
|
||||||
if len(values) > 1 {
|
if len(values) > 1 {
|
||||||
var (
|
var (
|
||||||
@ -76,8 +91,8 @@ var LogFormatter = func(values ...interface{}) (messages []interface{}) {
|
|||||||
if level == "sql" {
|
if level == "sql" {
|
||||||
// duration
|
// duration
|
||||||
messages = append(messages, fmt.Sprintf(" %s[%.2fms]%s ", cyan, float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0, reset))
|
messages = append(messages, fmt.Sprintf(" %s[%.2fms]%s ", cyan, float64(values[2].(time.Duration).Nanoseconds()/1e4)/100.0, reset))
|
||||||
// sql
|
|
||||||
|
|
||||||
|
// sql
|
||||||
for _, value := range values[4].([]interface{}) {
|
for _, value := range values[4].([]interface{}) {
|
||||||
indirectValue := reflect.Indirect(reflect.ValueOf(value))
|
indirectValue := reflect.Indirect(reflect.ValueOf(value))
|
||||||
if indirectValue.IsValid() {
|
if indirectValue.IsValid() {
|
||||||
|
24
logger_test.go
Normal file
24
logger_test.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package gorm_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNoColor(t *testing.T) {
|
||||||
|
os.Setenv("NO_COLOR", "1")
|
||||||
|
gorm.NoColor()
|
||||||
|
l := gorm.Logger{log.New(os.Stdout, "\r\n", 0)}
|
||||||
|
l.Print("info", "[info] NO_COLOR log test")
|
||||||
|
os.Setenv("NO_COLOR", "")
|
||||||
|
gorm.NoColor()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSQLLog(t *testing.T) {
|
||||||
|
l := gorm.Logger{log.New(os.Stdout, "\r\n", 0)}
|
||||||
|
l.Print("sql", "SQL log test", time.Duration(9990000), "sql: log format test type ", []interface{}{"cover"}, int64(0))
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user