新增数值切片类型的日志输出

This commit is contained in:
boyce
2021-07-01 16:41:54 +08:00
parent d9333e31c0
commit a0594cb945

View File

@@ -3,17 +3,17 @@ package log
import ( import (
"errors" "errors"
"fmt" "fmt"
jsoniter "github.com/json-iterator/go"
"io"
"log" "log"
syslog "log"
"os" "os"
"path" "path"
"runtime"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time"
"sync" "sync"
"io" "time"
"runtime"
syslog "log"
jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
@@ -186,30 +186,211 @@ func (logger *Logger) doSPrintf(level int, printLevel string, a []interface{}) {
case int: case int:
logger.buf.AppendInt(int64(s.(int))) logger.buf.AppendInt(int64(s.(int)))
case []int:
intSlice := s.([]int)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendInt(int64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case int8: case int8:
logger.buf.AppendInt(int64(s.(int8))) logger.buf.AppendInt(int64(s.(int8)))
case []int8:
intSlice := s.([]int8)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendInt(int64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case int16: case int16:
logger.buf.AppendInt(int64(s.(int16))) logger.buf.AppendInt(int64(s.(int16)))
case []int16:
intSlice := s.([]int16)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendInt(int64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case int32: case int32:
logger.buf.AppendInt(int64(s.(int32))) logger.buf.AppendInt(int64(s.(int32)))
case []int32:
intSlice := s.([]int32)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendInt(int64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case int64: case int64:
logger.buf.AppendInt(s.(int64)) logger.buf.AppendInt(s.(int64))
case []int64:
intSlice := s.([]int64)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendInt(v)
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case uint: case uint:
logger.buf.AppendUint(uint64(s.(uint))) logger.buf.AppendUint(uint64(s.(uint)))
case []uint:
intSlice := s.([]uint)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendUint(uint64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case uint8: case uint8:
logger.buf.AppendUint(uint64(s.(uint8))) logger.buf.AppendUint(uint64(s.(uint8)))
case []uint8:
intSlice := s.([]uint8)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendUint(uint64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case uint16: case uint16:
logger.buf.AppendUint(uint64(s.(uint16))) logger.buf.AppendUint(uint64(s.(uint16)))
case []uint16:
intSlice := s.([]uint16)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendUint(uint64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case uint32: case uint32:
logger.buf.AppendUint(uint64(s.(uint32))) logger.buf.AppendUint(uint64(s.(uint32)))
case []uint32:
intSlice := s.([]uint32)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendUint(uint64(v))
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case uint64: case uint64:
logger.buf.AppendUint(s.(uint64)) logger.buf.AppendUint(s.(uint64))
case []uint64:
intSlice := s.([]uint64)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendUint(v)
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case float32: case float32:
logger.buf.AppendFloat(float64(s.(float32)),32) logger.buf.AppendFloat(float64(s.(float32)),32)
case []float32:
intSlice := s.([]float32)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendFloat(float64(v),32)
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case float64: case float64:
logger.buf.AppendFloat(s.(float64),64) logger.buf.AppendFloat(s.(float64),64)
case []float64:
intSlice := s.([]float64)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendFloat(v,64)
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case bool: case bool:
logger.buf.AppendBool(s.(bool)) logger.buf.AppendBool(s.(bool))
case []bool:
intSlice := s.([]bool)
logger.buf.AppendByte('[')
for _,v := range intSlice {
logger.buf.AppendBool(v)
logger.buf.AppendByte(',')
}
lastIdx := logger.buf.Len()-1
if logger.buf.Bytes()[lastIdx] == ',' {
logger.buf.Bytes()[lastIdx] = ']'
}else{
logger.buf.AppendByte(']')
}
case string: case string:
logger.buf.AppendString(s.(string)) logger.buf.AppendString(s.(string))
case *int: case *int:
@@ -310,8 +491,8 @@ func (logger *Logger) doSPrintf(level int, printLevel string, a []interface{}) {
}else{ }else{
logger.buf.AppendString("nil<*string>") logger.buf.AppendString("nil<*string>")
} }
case []byte: //case []byte:
logger.buf.AppendBytes(s.([]byte)) // logger.buf.AppendBytes(s.([]byte))
default: default:
//b,err := json.MarshalToString(s) //b,err := json.MarshalToString(s)
//if err != nil { //if err != nil {
@@ -422,10 +603,10 @@ func SFatal(a ...interface{}) {
} }
const timeFlag = syslog.Ldate|syslog.Ltime|syslog.Lmicroseconds const timeFlag = syslog.Ldate|syslog.Ltime|syslog.Lmicroseconds
func (l *Logger) formatHeader(calldepth int,t *time.Time) { func (logger *Logger) formatHeader(calldepth int,t *time.Time) {
var file string var file string
var line int var line int
if l.flag&(syslog.Lshortfile|syslog.Llongfile) != 0 { if logger.flag&(syslog.Lshortfile|syslog.Llongfile) != 0 {
// Release lock while getting caller info - it's expensive. // Release lock while getting caller info - it's expensive.
var ok bool var ok bool
_, file, line, ok = runtime.Caller(calldepth) _, file, line, ok = runtime.Caller(calldepth)
@@ -435,38 +616,38 @@ func (l *Logger) formatHeader(calldepth int,t *time.Time) {
} }
} }
if l.flag&syslog.Lmsgprefix != 0 { if logger.flag&syslog.Lmsgprefix != 0 {
l.buf.AppendString(l.filepre) logger.buf.AppendString(logger.filepre)
} }
if l.flag&timeFlag != 0 { if logger.flag&timeFlag != 0 {
if l.flag&syslog.Ldate != 0 { if logger.flag&syslog.Ldate != 0 {
year, month, day := t.Date() year, month, day := t.Date()
l.buf.AppendInt(int64(year)) logger.buf.AppendInt(int64(year))
l.buf.AppendByte('/') logger.buf.AppendByte('/')
l.buf.AppendInt(int64(month)) logger.buf.AppendInt(int64(month))
l.buf.AppendByte('/') logger.buf.AppendByte('/')
l.buf.AppendInt(int64(day)) logger.buf.AppendInt(int64(day))
l.buf.AppendByte(' ') logger.buf.AppendByte(' ')
} }
if l.flag&(syslog.Ltime|syslog.Lmicroseconds) != 0 { if logger.flag&(syslog.Ltime|syslog.Lmicroseconds) != 0 {
hour, min, sec := t.Clock() hour, min, sec := t.Clock()
l.buf.AppendInt(int64(hour)) logger.buf.AppendInt(int64(hour))
l.buf.AppendByte(':') logger.buf.AppendByte(':')
l.buf.AppendInt(int64(min)) logger.buf.AppendInt(int64(min))
l.buf.AppendByte(':') logger.buf.AppendByte(':')
l.buf.AppendInt(int64(sec)) logger.buf.AppendInt(int64(sec))
if l.flag&syslog.Lmicroseconds != 0 { if logger.flag&syslog.Lmicroseconds != 0 {
l.buf.AppendByte('.') logger.buf.AppendByte('.')
l.buf.AppendInt(int64(t.Nanosecond()/1e3)) logger.buf.AppendInt(int64(t.Nanosecond()/1e3))
} }
l.buf.AppendByte(' ') logger.buf.AppendByte(' ')
} }
} }
if l.flag&(syslog.Lshortfile|syslog.Llongfile) != 0 { if logger.flag&(syslog.Lshortfile|syslog.Llongfile) != 0 {
if l.flag&syslog.Lshortfile != 0 { if logger.flag&syslog.Lshortfile != 0 {
short := file short := file
for i := len(file) - 1; i > 0; i-- { for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' { if file[i] == '/' {
@@ -476,13 +657,13 @@ func (l *Logger) formatHeader(calldepth int,t *time.Time) {
} }
file = short file = short
} }
l.buf.AppendString(file) logger.buf.AppendString(file)
l.buf.AppendByte(':') logger.buf.AppendByte(':')
l.buf.AppendInt(int64(line)) logger.buf.AppendInt(int64(line))
l.buf.AppendString(": ") logger.buf.AppendString(": ")
} }
if l.flag&syslog.Lmsgprefix != 0 { if logger.flag&syslog.Lmsgprefix != 0 {
l.buf.AppendString(l.filepre) logger.buf.AppendString(logger.filepre)
} }
} }