|
|
@ -1,6 +1,7 @@
|
|
|
|
package logx
|
|
|
|
package logx
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
@ -31,6 +32,15 @@ const (
|
|
|
|
SevereLevel
|
|
|
|
SevereLevel
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
|
|
jsonEncodingType = iota
|
|
|
|
|
|
|
|
plainEncodingType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
jsonEncoding = "json"
|
|
|
|
|
|
|
|
plainEncoding = "plain"
|
|
|
|
|
|
|
|
plainEncodingSep = '\t'
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
accessFilename = "access.log"
|
|
|
|
accessFilename = "access.log"
|
|
|
|
errorFilename = "error.log"
|
|
|
|
errorFilename = "error.log"
|
|
|
@ -65,6 +75,7 @@ var (
|
|
|
|
timeFormat = "2006-01-02T15:04:05.000Z07"
|
|
|
|
timeFormat = "2006-01-02T15:04:05.000Z07"
|
|
|
|
writeConsole bool
|
|
|
|
writeConsole bool
|
|
|
|
logLevel uint32
|
|
|
|
logLevel uint32
|
|
|
|
|
|
|
|
encoding = jsonEncodingType
|
|
|
|
// use uint32 for atomic operations
|
|
|
|
// use uint32 for atomic operations
|
|
|
|
disableStat uint32
|
|
|
|
disableStat uint32
|
|
|
|
infoLog io.WriteCloser
|
|
|
|
infoLog io.WriteCloser
|
|
|
@ -124,6 +135,12 @@ func SetUp(c LogConf) error {
|
|
|
|
if len(c.TimeFormat) > 0 {
|
|
|
|
if len(c.TimeFormat) > 0 {
|
|
|
|
timeFormat = c.TimeFormat
|
|
|
|
timeFormat = c.TimeFormat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Encoding {
|
|
|
|
|
|
|
|
case plainEncoding:
|
|
|
|
|
|
|
|
encoding = plainEncodingType
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
encoding = jsonEncodingType
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch c.Mode {
|
|
|
|
switch c.Mode {
|
|
|
|
case consoleMode:
|
|
|
|
case consoleMode:
|
|
|
@ -407,21 +424,31 @@ func infoTextSync(msg string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func outputAny(writer io.Writer, level string, val interface{}) {
|
|
|
|
func outputAny(writer io.Writer, level string, val interface{}) {
|
|
|
|
|
|
|
|
switch encoding {
|
|
|
|
|
|
|
|
case plainEncodingType:
|
|
|
|
|
|
|
|
writePlainAny(writer, level, val)
|
|
|
|
|
|
|
|
default:
|
|
|
|
info := logEntry{
|
|
|
|
info := logEntry{
|
|
|
|
Timestamp: getTimestamp(),
|
|
|
|
Timestamp: getTimestamp(),
|
|
|
|
Level: level,
|
|
|
|
Level: level,
|
|
|
|
Content: val,
|
|
|
|
Content: val,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outputJson(writer, info)
|
|
|
|
outputJson(writer, info)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func outputText(writer io.Writer, level, msg string) {
|
|
|
|
func outputText(writer io.Writer, level, msg string) {
|
|
|
|
|
|
|
|
switch encoding {
|
|
|
|
|
|
|
|
case plainEncodingType:
|
|
|
|
|
|
|
|
writePlainText(writer, level, msg)
|
|
|
|
|
|
|
|
default:
|
|
|
|
info := logEntry{
|
|
|
|
info := logEntry{
|
|
|
|
Timestamp: getTimestamp(),
|
|
|
|
Timestamp: getTimestamp(),
|
|
|
|
Level: level,
|
|
|
|
Level: level,
|
|
|
|
Content: msg,
|
|
|
|
Content: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outputJson(writer, info)
|
|
|
|
outputJson(writer, info)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func outputError(writer io.Writer, msg string, callDepth int) {
|
|
|
|
func outputError(writer io.Writer, msg string, callDepth int) {
|
|
|
@ -565,6 +592,62 @@ func statSync(msg string) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func writePlainAny(writer io.Writer, level string, val interface{}, fields ...string) {
|
|
|
|
|
|
|
|
switch v := val.(type) {
|
|
|
|
|
|
|
|
case string:
|
|
|
|
|
|
|
|
writePlainText(writer, level, v, fields...)
|
|
|
|
|
|
|
|
case error:
|
|
|
|
|
|
|
|
writePlainText(writer, level, v.Error(), fields...)
|
|
|
|
|
|
|
|
case fmt.Stringer:
|
|
|
|
|
|
|
|
writePlainText(writer, level, v.String(), fields...)
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
buf.WriteString(getTimestamp())
|
|
|
|
|
|
|
|
buf.WriteByte(plainEncodingSep)
|
|
|
|
|
|
|
|
buf.WriteString(level)
|
|
|
|
|
|
|
|
for _, item := range fields {
|
|
|
|
|
|
|
|
buf.WriteByte(plainEncodingSep)
|
|
|
|
|
|
|
|
buf.WriteString(item)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteByte(plainEncodingSep)
|
|
|
|
|
|
|
|
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
|
|
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
|
|
|
|
if atomic.LoadUint32(&initialized) == 0 || writer == nil {
|
|
|
|
|
|
|
|
log.Println(buf.String())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := writer.Write(buf.Bytes()); err != nil {
|
|
|
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
buf.WriteString(getTimestamp())
|
|
|
|
|
|
|
|
buf.WriteByte(plainEncodingSep)
|
|
|
|
|
|
|
|
buf.WriteString(level)
|
|
|
|
|
|
|
|
for _, item := range fields {
|
|
|
|
|
|
|
|
buf.WriteByte(plainEncodingSep)
|
|
|
|
|
|
|
|
buf.WriteString(item)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteByte(plainEncodingSep)
|
|
|
|
|
|
|
|
buf.WriteString(msg)
|
|
|
|
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
|
|
|
|
if atomic.LoadUint32(&initialized) == 0 || writer == nil {
|
|
|
|
|
|
|
|
log.Println(buf.String())
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := writer.Write(buf.Bytes()); err != nil {
|
|
|
|
|
|
|
|
log.Println(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type logWriter struct {
|
|
|
|
type logWriter struct {
|
|
|
|
logger *log.Logger
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|