2017-07-21 20:29:08 +00:00
|
|
|
package hclog
|
|
|
|
|
|
|
|
import (
|
2019-10-07 20:41:52 +00:00
|
|
|
"bytes"
|
2018-07-27 02:26:04 +00:00
|
|
|
"encoding"
|
2017-07-21 20:29:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-10-07 20:41:52 +00:00
|
|
|
"io"
|
2017-07-21 20:29:08 +00:00
|
|
|
"log"
|
2019-10-15 19:14:25 +00:00
|
|
|
"os"
|
2019-10-07 20:41:52 +00:00
|
|
|
"reflect"
|
2019-10-28 12:25:47 +00:00
|
|
|
"regexp"
|
2017-07-21 20:29:08 +00:00
|
|
|
"runtime"
|
2018-07-27 02:26:04 +00:00
|
|
|
"sort"
|
2017-07-21 20:29:08 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2018-07-27 02:26:04 +00:00
|
|
|
"sync/atomic"
|
2017-07-21 20:29:08 +00:00
|
|
|
"time"
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2017-07-21 20:29:08 +00:00
|
|
|
)
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
// TimeFormat to use for logging. This is a version of RFC3339 that contains
|
|
|
|
// contains millisecond precision
|
|
|
|
const TimeFormat = "2006-01-02T15:04:05.000Z0700"
|
|
|
|
|
|
|
|
// errJsonUnsupportedTypeMsg is included in log json entries, if an arg cannot be serialized to json
|
|
|
|
const errJsonUnsupportedTypeMsg = "logging contained values that don't serialize to json"
|
|
|
|
|
2017-07-21 20:29:08 +00:00
|
|
|
var (
|
|
|
|
_levelToBracket = map[Level]string{
|
|
|
|
Debug: "[DEBUG]",
|
|
|
|
Trace: "[TRACE]",
|
2019-10-07 20:41:52 +00:00
|
|
|
Info: "[INFO] ",
|
|
|
|
Warn: "[WARN] ",
|
2017-07-21 20:29:08 +00:00
|
|
|
Error: "[ERROR]",
|
|
|
|
}
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
_levelToColor = map[Level]*color.Color{
|
|
|
|
Debug: color.New(color.FgHiWhite),
|
|
|
|
Trace: color.New(color.FgHiGreen),
|
|
|
|
Info: color.New(color.FgHiBlue),
|
|
|
|
Warn: color.New(color.FgHiYellow),
|
|
|
|
Error: color.New(color.FgHiRed),
|
|
|
|
}
|
2017-07-21 20:29:08 +00:00
|
|
|
)
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
// Make sure that intLogger is a Logger
|
|
|
|
var _ Logger = &intLogger{}
|
|
|
|
|
|
|
|
// intLogger is an internal logger implementation. Internal in that it is
|
|
|
|
// defined entirely by this package.
|
|
|
|
type intLogger struct {
|
|
|
|
json bool
|
|
|
|
caller bool
|
|
|
|
name string
|
|
|
|
timeFormat string
|
|
|
|
|
|
|
|
// This is a pointer so that it's shared by any derived loggers, since
|
|
|
|
// those derived loggers share the bufio.Writer as well.
|
|
|
|
mutex *sync.Mutex
|
|
|
|
writer *writer
|
|
|
|
level *int32
|
|
|
|
|
|
|
|
implied []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a configured logger.
|
2017-07-21 20:29:08 +00:00
|
|
|
func New(opts *LoggerOptions) Logger {
|
2019-10-15 19:14:25 +00:00
|
|
|
return newLogger(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSinkAdapter returns a SinkAdapter with configured settings
|
|
|
|
// defined by LoggerOptions
|
|
|
|
func NewSinkAdapter(opts *LoggerOptions) SinkAdapter {
|
|
|
|
return newLogger(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogger(opts *LoggerOptions) *intLogger {
|
2017-07-21 20:29:08 +00:00
|
|
|
if opts == nil {
|
|
|
|
opts = &LoggerOptions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
output := opts.Output
|
|
|
|
if output == nil {
|
2019-10-07 20:41:52 +00:00
|
|
|
output = DefaultOutput
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
level := opts.Level
|
|
|
|
if level == NoLevel {
|
|
|
|
level = DefaultLevel
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
mutex := opts.Mutex
|
|
|
|
if mutex == nil {
|
|
|
|
mutex = new(sync.Mutex)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
2018-07-27 02:26:04 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l := &intLogger{
|
2018-07-27 02:26:04 +00:00
|
|
|
json: opts.JSONFormat,
|
|
|
|
caller: opts.IncludeLocation,
|
|
|
|
name: opts.Name,
|
|
|
|
timeFormat: TimeFormat,
|
2019-10-07 20:41:52 +00:00
|
|
|
mutex: mutex,
|
2019-10-15 19:14:25 +00:00
|
|
|
writer: newWriter(output, opts.Color),
|
2018-07-27 02:26:04 +00:00
|
|
|
level: new(int32),
|
|
|
|
}
|
2019-10-07 20:41:52 +00:00
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
l.setColorization(opts)
|
|
|
|
|
2018-07-27 02:26:04 +00:00
|
|
|
if opts.TimeFormat != "" {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.timeFormat = opts.TimeFormat
|
2018-07-27 02:26:04 +00:00
|
|
|
}
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
atomic.StoreInt32(l.level, int32(level))
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
return l
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log a message and a set of key/value pairs if the given level is at
|
|
|
|
// or more severe that the threshold configured in the Logger.
|
2019-10-15 19:14:25 +00:00
|
|
|
func (l *intLogger) Log(name string, level Level, msg string, args ...interface{}) {
|
|
|
|
if level < Level(atomic.LoadInt32(l.level)) {
|
2017-07-21 20:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.Now()
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.mutex.Lock()
|
|
|
|
defer l.mutex.Unlock()
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
if l.json {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.logJSON(t, name, level, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
} else {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.log(t, name, level, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.Flush(level)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup a path by returning the last 2 segments of the path only.
|
|
|
|
func trimCallerPath(path string) string {
|
|
|
|
// lovely borrowed from zap
|
|
|
|
// nb. To make sure we trim the path correctly on Windows too, we
|
|
|
|
// counter-intuitively need to use '/' and *not* os.PathSeparator here,
|
|
|
|
// because the path given originates from Go stdlib, specifically
|
|
|
|
// runtime.Caller() which (as of Mar/17) returns forward slashes even on
|
|
|
|
// Windows.
|
|
|
|
//
|
|
|
|
// See https://github.com/golang/go/issues/3335
|
|
|
|
// and https://github.com/golang/go/issues/18151
|
|
|
|
//
|
|
|
|
// for discussion on the issue on Go side.
|
|
|
|
|
|
|
|
// Find the last separator.
|
|
|
|
idx := strings.LastIndexByte(path, '/')
|
|
|
|
if idx == -1 {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the penultimate separator.
|
|
|
|
idx = strings.LastIndexByte(path[:idx], '/')
|
|
|
|
if idx == -1 {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
return path[idx+1:]
|
|
|
|
}
|
|
|
|
|
2019-10-28 12:25:47 +00:00
|
|
|
var logImplFile = regexp.MustCompile(`github.com/hashicorp/go-hclog/.+logger.go$`)
|
|
|
|
|
2017-07-21 20:29:08 +00:00
|
|
|
// Non-JSON logging format function
|
2019-10-15 19:14:25 +00:00
|
|
|
func (l *intLogger) log(t time.Time, name string, level Level, msg string, args ...interface{}) {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString(t.Format(l.timeFormat))
|
|
|
|
l.writer.WriteByte(' ')
|
2017-07-21 20:29:08 +00:00
|
|
|
|
|
|
|
s, ok := _levelToBracket[level]
|
|
|
|
if ok {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString(s)
|
2017-07-21 20:29:08 +00:00
|
|
|
} else {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString("[?????]")
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 12:25:47 +00:00
|
|
|
offset := 3
|
2019-10-07 20:41:52 +00:00
|
|
|
if l.caller {
|
2019-10-28 12:25:47 +00:00
|
|
|
// Check if the caller is inside our package and inside
|
|
|
|
// a logger implementation file
|
|
|
|
if _, file, _, ok := runtime.Caller(3); ok {
|
|
|
|
match := logImplFile.MatchString(file)
|
|
|
|
if match {
|
|
|
|
offset = 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, file, line, ok := runtime.Caller(offset); ok {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteByte(' ')
|
|
|
|
l.writer.WriteString(trimCallerPath(file))
|
|
|
|
l.writer.WriteByte(':')
|
|
|
|
l.writer.WriteString(strconv.Itoa(line))
|
|
|
|
l.writer.WriteByte(':')
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteByte(' ')
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
if name != "" {
|
|
|
|
l.writer.WriteString(name)
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString(": ")
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString(msg)
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
args = append(l.implied, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
|
|
|
|
var stacktrace CapturedStacktrace
|
|
|
|
|
|
|
|
if args != nil && len(args) > 0 {
|
|
|
|
if len(args)%2 != 0 {
|
|
|
|
cs, ok := args[len(args)-1].(CapturedStacktrace)
|
|
|
|
if ok {
|
|
|
|
args = args[:len(args)-1]
|
|
|
|
stacktrace = cs
|
|
|
|
} else {
|
2019-12-18 17:53:33 +00:00
|
|
|
extra := args[len(args)-1]
|
|
|
|
args = append(args[:len(args)-1], MissingKey, extra)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteByte(':')
|
2017-07-21 20:29:08 +00:00
|
|
|
|
|
|
|
FOR:
|
|
|
|
for i := 0; i < len(args); i = i + 2 {
|
2019-10-07 20:41:52 +00:00
|
|
|
var (
|
|
|
|
val string
|
|
|
|
raw bool
|
|
|
|
)
|
2017-07-21 20:29:08 +00:00
|
|
|
|
|
|
|
switch st := args[i+1].(type) {
|
|
|
|
case string:
|
|
|
|
val = st
|
|
|
|
case int:
|
|
|
|
val = strconv.FormatInt(int64(st), 10)
|
|
|
|
case int64:
|
|
|
|
val = strconv.FormatInt(int64(st), 10)
|
|
|
|
case int32:
|
|
|
|
val = strconv.FormatInt(int64(st), 10)
|
|
|
|
case int16:
|
|
|
|
val = strconv.FormatInt(int64(st), 10)
|
|
|
|
case int8:
|
|
|
|
val = strconv.FormatInt(int64(st), 10)
|
|
|
|
case uint:
|
|
|
|
val = strconv.FormatUint(uint64(st), 10)
|
|
|
|
case uint64:
|
|
|
|
val = strconv.FormatUint(uint64(st), 10)
|
|
|
|
case uint32:
|
|
|
|
val = strconv.FormatUint(uint64(st), 10)
|
|
|
|
case uint16:
|
|
|
|
val = strconv.FormatUint(uint64(st), 10)
|
|
|
|
case uint8:
|
|
|
|
val = strconv.FormatUint(uint64(st), 10)
|
|
|
|
case CapturedStacktrace:
|
|
|
|
stacktrace = st
|
|
|
|
continue FOR
|
2018-07-27 02:26:04 +00:00
|
|
|
case Format:
|
|
|
|
val = fmt.Sprintf(st[0].(string), st[1:]...)
|
2017-07-21 20:29:08 +00:00
|
|
|
default:
|
2019-10-07 20:41:52 +00:00
|
|
|
v := reflect.ValueOf(st)
|
|
|
|
if v.Kind() == reflect.Slice {
|
|
|
|
val = l.renderSlice(v)
|
|
|
|
raw = true
|
|
|
|
} else {
|
|
|
|
val = fmt.Sprintf("%v", st)
|
|
|
|
}
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteByte(' ')
|
2019-12-18 17:53:33 +00:00
|
|
|
switch st := args[i].(type) {
|
|
|
|
case string:
|
|
|
|
l.writer.WriteString(st)
|
|
|
|
default:
|
|
|
|
l.writer.WriteString(fmt.Sprintf("%s", st))
|
|
|
|
}
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteByte('=')
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
if !raw && strings.ContainsAny(val, " \t\n\r") {
|
|
|
|
l.writer.WriteByte('"')
|
|
|
|
l.writer.WriteString(val)
|
|
|
|
l.writer.WriteByte('"')
|
2017-07-21 20:29:08 +00:00
|
|
|
} else {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString(val)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString("\n")
|
2017-07-21 20:29:08 +00:00
|
|
|
|
|
|
|
if stacktrace != "" {
|
2019-10-07 20:41:52 +00:00
|
|
|
l.writer.WriteString(string(stacktrace))
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) renderSlice(v reflect.Value) string {
|
|
|
|
var buf bytes.Buffer
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
buf.WriteRune('[')
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
buf.WriteString(", ")
|
|
|
|
}
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
sv := v.Index(i)
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
var val string
|
|
|
|
|
|
|
|
switch sv.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
val = sv.String()
|
|
|
|
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
val = strconv.FormatInt(sv.Int(), 10)
|
|
|
|
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
val = strconv.FormatUint(sv.Uint(), 10)
|
|
|
|
default:
|
|
|
|
val = fmt.Sprintf("%v", sv.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ContainsAny(val, " \t\n\r") {
|
|
|
|
buf.WriteByte('"')
|
|
|
|
buf.WriteString(val)
|
|
|
|
buf.WriteByte('"')
|
|
|
|
} else {
|
|
|
|
buf.WriteString(val)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
buf.WriteRune(']')
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSON logging function
|
2019-10-15 19:14:25 +00:00
|
|
|
func (l *intLogger) logJSON(t time.Time, name string, level Level, msg string, args ...interface{}) {
|
|
|
|
vals := l.jsonMapEntry(t, name, level, msg)
|
2019-10-07 20:41:52 +00:00
|
|
|
args = append(l.implied, args...)
|
2018-07-27 02:26:04 +00:00
|
|
|
|
2017-07-21 20:29:08 +00:00
|
|
|
if args != nil && len(args) > 0 {
|
|
|
|
if len(args)%2 != 0 {
|
|
|
|
cs, ok := args[len(args)-1].(CapturedStacktrace)
|
|
|
|
if ok {
|
|
|
|
args = args[:len(args)-1]
|
|
|
|
vals["stacktrace"] = cs
|
|
|
|
} else {
|
2019-12-18 17:53:33 +00:00
|
|
|
extra := args[len(args)-1]
|
|
|
|
args = append(args[:len(args)-1], MissingKey, extra)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(args); i = i + 2 {
|
2018-07-27 02:26:04 +00:00
|
|
|
val := args[i+1]
|
|
|
|
switch sv := val.(type) {
|
|
|
|
case error:
|
|
|
|
// Check if val is of type error. If error type doesn't
|
|
|
|
// implement json.Marshaler or encoding.TextMarshaler
|
|
|
|
// then set val to err.Error() so that it gets marshaled
|
|
|
|
switch sv.(type) {
|
|
|
|
case json.Marshaler, encoding.TextMarshaler:
|
|
|
|
default:
|
|
|
|
val = sv.Error()
|
|
|
|
}
|
|
|
|
case Format:
|
|
|
|
val = fmt.Sprintf(sv[0].(string), sv[1:]...)
|
|
|
|
}
|
|
|
|
|
2019-12-18 17:53:33 +00:00
|
|
|
var key string
|
|
|
|
|
|
|
|
switch st := args[i].(type) {
|
|
|
|
case string:
|
|
|
|
key = st
|
|
|
|
default:
|
|
|
|
key = fmt.Sprintf("%s", st)
|
|
|
|
}
|
|
|
|
vals[key] = val
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
err := json.NewEncoder(l.writer).Encode(vals)
|
2017-07-21 20:29:08 +00:00
|
|
|
if err != nil {
|
2019-10-07 20:41:52 +00:00
|
|
|
if _, ok := err.(*json.UnsupportedTypeError); ok {
|
2019-10-15 19:14:25 +00:00
|
|
|
plainVal := l.jsonMapEntry(t, name, level, msg)
|
2019-10-07 20:41:52 +00:00
|
|
|
plainVal["@warn"] = errJsonUnsupportedTypeMsg
|
|
|
|
|
|
|
|
json.NewEncoder(l.writer).Encode(plainVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
func (l intLogger) jsonMapEntry(t time.Time, name string, level Level, msg string) map[string]interface{} {
|
2019-10-07 20:41:52 +00:00
|
|
|
vals := map[string]interface{}{
|
|
|
|
"@message": msg,
|
|
|
|
"@timestamp": t.Format("2006-01-02T15:04:05.000000Z07:00"),
|
|
|
|
}
|
|
|
|
|
|
|
|
var levelStr string
|
|
|
|
switch level {
|
|
|
|
case Error:
|
|
|
|
levelStr = "error"
|
|
|
|
case Warn:
|
|
|
|
levelStr = "warn"
|
|
|
|
case Info:
|
|
|
|
levelStr = "info"
|
|
|
|
case Debug:
|
|
|
|
levelStr = "debug"
|
|
|
|
case Trace:
|
|
|
|
levelStr = "trace"
|
|
|
|
default:
|
|
|
|
levelStr = "all"
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
2019-10-07 20:41:52 +00:00
|
|
|
|
|
|
|
vals["@level"] = levelStr
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
if name != "" {
|
|
|
|
vals["@module"] = name
|
2019-10-07 20:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if l.caller {
|
|
|
|
if _, file, line, ok := runtime.Caller(4); ok {
|
|
|
|
vals["@caller"] = fmt.Sprintf("%s:%d", file, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vals
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the message and args at DEBUG level
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) Debug(msg string, args ...interface{}) {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.Log(l.Name(), Debug, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the message and args at TRACE level
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) Trace(msg string, args ...interface{}) {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.Log(l.Name(), Trace, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the message and args at INFO level
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) Info(msg string, args ...interface{}) {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.Log(l.Name(), Info, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the message and args at WARN level
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) Warn(msg string, args ...interface{}) {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.Log(l.Name(), Warn, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the message and args at ERROR level
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) Error(msg string, args ...interface{}) {
|
2019-10-15 19:14:25 +00:00
|
|
|
l.Log(l.Name(), Error, msg, args...)
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate that the logger would emit TRACE level logs
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) IsTrace() bool {
|
|
|
|
return Level(atomic.LoadInt32(l.level)) == Trace
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate that the logger would emit DEBUG level logs
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) IsDebug() bool {
|
|
|
|
return Level(atomic.LoadInt32(l.level)) <= Debug
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate that the logger would emit INFO level logs
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) IsInfo() bool {
|
|
|
|
return Level(atomic.LoadInt32(l.level)) <= Info
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate that the logger would emit WARN level logs
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) IsWarn() bool {
|
|
|
|
return Level(atomic.LoadInt32(l.level)) <= Warn
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate that the logger would emit ERROR level logs
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) IsError() bool {
|
|
|
|
return Level(atomic.LoadInt32(l.level)) <= Error
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 17:53:33 +00:00
|
|
|
const MissingKey = "EXTRA_VALUE_AT_END"
|
|
|
|
|
2017-07-21 20:29:08 +00:00
|
|
|
// Return a sub-Logger for which every emitted log message will contain
|
|
|
|
// the given key/value pairs. This is used to create a context specific
|
|
|
|
// Logger.
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) With(args ...interface{}) Logger {
|
2019-12-18 17:53:33 +00:00
|
|
|
var extra interface{}
|
|
|
|
|
2018-07-27 02:26:04 +00:00
|
|
|
if len(args)%2 != 0 {
|
2019-12-18 17:53:33 +00:00
|
|
|
extra = args[len(args)-1]
|
|
|
|
args = args[:len(args)-1]
|
2018-07-27 02:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
sl := *l
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
result := make(map[string]interface{}, len(l.implied)+len(args))
|
|
|
|
keys := make([]string, 0, len(l.implied)+len(args))
|
2018-07-27 02:26:04 +00:00
|
|
|
|
|
|
|
// Read existing args, store map and key for consistent sorting
|
2019-10-07 20:41:52 +00:00
|
|
|
for i := 0; i < len(l.implied); i += 2 {
|
|
|
|
key := l.implied[i].(string)
|
2018-07-27 02:26:04 +00:00
|
|
|
keys = append(keys, key)
|
2019-10-07 20:41:52 +00:00
|
|
|
result[key] = l.implied[i+1]
|
2018-07-27 02:26:04 +00:00
|
|
|
}
|
|
|
|
// Read new args, store map and key for consistent sorting
|
|
|
|
for i := 0; i < len(args); i += 2 {
|
|
|
|
key := args[i].(string)
|
|
|
|
_, exists := result[key]
|
|
|
|
if !exists {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
result[key] = args[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort keys to be consistent
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
sl.implied = make([]interface{}, 0, len(l.implied)+len(args))
|
2018-07-27 02:26:04 +00:00
|
|
|
for _, k := range keys {
|
2019-10-07 20:41:52 +00:00
|
|
|
sl.implied = append(sl.implied, k)
|
|
|
|
sl.implied = append(sl.implied, result[k])
|
2018-07-27 02:26:04 +00:00
|
|
|
}
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-12-18 17:53:33 +00:00
|
|
|
if extra != nil {
|
|
|
|
sl.implied = append(sl.implied, MissingKey, extra)
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
return &sl
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new sub-Logger that a name decending from the current name.
|
|
|
|
// This is used to create a subsystem specific Logger.
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) Named(name string) Logger {
|
|
|
|
sl := *l
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
if sl.name != "" {
|
|
|
|
sl.name = sl.name + "." + name
|
2018-07-27 02:26:04 +00:00
|
|
|
} else {
|
2019-10-07 20:41:52 +00:00
|
|
|
sl.name = name
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
return &sl
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new sub-Logger with an explicit name. This ignores the current
|
|
|
|
// name. This is used to create a standalone logger that doesn't fall
|
|
|
|
// within the normal hierarchy.
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) ResetNamed(name string) Logger {
|
|
|
|
sl := *l
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
sl.name = name
|
2017-07-21 20:29:08 +00:00
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
return &sl
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 02:26:04 +00:00
|
|
|
// Update the logging level on-the-fly. This will affect all subloggers as
|
|
|
|
// well.
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) SetLevel(level Level) {
|
|
|
|
atomic.StoreInt32(l.level, int32(level))
|
2018-07-27 02:26:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 20:29:08 +00:00
|
|
|
// Create a *log.Logger that will send it's data through this Logger. This
|
|
|
|
// allows packages that expect to be using the standard library log to actually
|
|
|
|
// use this logger.
|
2019-10-07 20:41:52 +00:00
|
|
|
func (l *intLogger) StandardLogger(opts *StandardLoggerOptions) *log.Logger {
|
2017-07-21 20:29:08 +00:00
|
|
|
if opts == nil {
|
|
|
|
opts = &StandardLoggerOptions{}
|
|
|
|
}
|
|
|
|
|
2019-10-07 20:41:52 +00:00
|
|
|
return log.New(l.StandardWriter(opts), "", 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *intLogger) StandardWriter(opts *StandardLoggerOptions) io.Writer {
|
|
|
|
return &stdlogAdapter{
|
|
|
|
log: l,
|
|
|
|
inferLevels: opts.InferLevels,
|
|
|
|
forceLevel: opts.ForceLevel,
|
|
|
|
}
|
2017-07-21 20:29:08 +00:00
|
|
|
}
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
// checks if the underlying io.Writer is a file, and
|
|
|
|
// panics if not. For use by colorization.
|
|
|
|
func (l *intLogger) checkWriterIsFile() *os.File {
|
|
|
|
fi, ok := l.writer.w.(*os.File)
|
|
|
|
if !ok {
|
|
|
|
panic("Cannot enable coloring of non-file Writers")
|
|
|
|
}
|
|
|
|
return fi
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept implements the SinkAdapter interface
|
|
|
|
func (i *intLogger) Accept(name string, level Level, msg string, args ...interface{}) {
|
|
|
|
i.Log(name, level, msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImpliedArgs returns the loggers implied args
|
|
|
|
func (i *intLogger) ImpliedArgs() []interface{} {
|
|
|
|
return i.implied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the loggers name
|
|
|
|
func (i *intLogger) Name() string {
|
|
|
|
return i.name
|
|
|
|
}
|