2018-01-16 13:59:52 +00:00
|
|
|
// Package mlog is a generic logging library. The log methods come in different
|
|
|
|
// severities: Debug, Info, Warn, Error, and Fatal.
|
|
|
|
//
|
|
|
|
// The log methods take in a string describing the error, and a set of key/value
|
|
|
|
// pairs giving the specific context around the error. The string is intended to
|
|
|
|
// always be the same no matter what, while the key/value pairs give information
|
|
|
|
// like which userID the error happened to, or any other relevant contextual
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
2019-01-25 21:29:22 +00:00
|
|
|
// log := mlog.NewLogger()
|
|
|
|
// log.Info("Something important has occurred")
|
|
|
|
// log.Error("Could not open file", mlog.KV{"filename": filename}, merr.KV(err))
|
2018-01-16 13:59:52 +00:00
|
|
|
//
|
|
|
|
package mlog
|
|
|
|
|
|
|
|
import (
|
2018-11-30 21:27:18 +00:00
|
|
|
"bufio"
|
2018-01-16 13:59:52 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2019-02-03 00:27:10 +00:00
|
|
|
"strings"
|
2018-01-16 13:59:52 +00:00
|
|
|
"sync"
|
2019-01-15 04:55:22 +00:00
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
2018-01-16 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Truncate is a helper function to truncate a string to a given size. It will
|
|
|
|
// add 3 trailing elipses, so the returned string will be at most size+3
|
|
|
|
// characters long
|
|
|
|
func Truncate(s string, size int) string {
|
|
|
|
if len(s) <= size {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return s[:size] + "..."
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-16 13:59:52 +00:00
|
|
|
// Level describes the severity of a particular log message, and can be compared
|
|
|
|
// to the severity of any other Level
|
|
|
|
type Level interface {
|
|
|
|
// String gives the string form of the level, e.g. "INFO" or "ERROR"
|
|
|
|
String() string
|
|
|
|
|
|
|
|
// Uint gives an integer indicator of the severity of the level, with zero
|
|
|
|
// being most severe. If a Level with Uint of zero is logged then the Logger
|
|
|
|
// implementation provided by this package will exit the process (i.e. zero
|
|
|
|
// is used as Fatal).
|
|
|
|
Uint() uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type level struct {
|
|
|
|
s string
|
|
|
|
i uint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l level) String() string {
|
|
|
|
return l.s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l level) Uint() uint {
|
|
|
|
return l.i
|
|
|
|
}
|
|
|
|
|
|
|
|
// All pre-defined log levels
|
|
|
|
var (
|
|
|
|
DebugLevel Level = level{s: "DEBUG", i: 40}
|
|
|
|
InfoLevel Level = level{s: "INFO", i: 30}
|
|
|
|
WarnLevel Level = level{s: "WARN", i: 20}
|
|
|
|
ErrorLevel Level = level{s: "ERROR", i: 10}
|
|
|
|
FatalLevel Level = level{s: "FATAL", i: 0}
|
|
|
|
)
|
|
|
|
|
2019-02-03 00:27:10 +00:00
|
|
|
// LevelFromString takes a string describing one of the pre-defined Levels (e.g.
|
|
|
|
// "debug" or "INFO") and returns the corresponding Level instance, or nil if
|
|
|
|
// the string doesn't describe any of the predefined Levels.
|
|
|
|
func LevelFromString(s string) Level {
|
|
|
|
switch strings.TrimSpace(strings.ToUpper(s)) {
|
|
|
|
case "DEBUG":
|
|
|
|
return DebugLevel
|
|
|
|
case "INFO":
|
|
|
|
return InfoLevel
|
|
|
|
case "WARN":
|
|
|
|
return WarnLevel
|
|
|
|
case "ERROR":
|
|
|
|
return ErrorLevel
|
|
|
|
case "FATAL":
|
|
|
|
return FatalLevel
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
// KVer is used to provide context to a log entry in the form of a dynamic set
|
|
|
|
// of key/value pairs which can be different for every entry.
|
|
|
|
//
|
2019-01-15 03:47:16 +00:00
|
|
|
// The returned map is read-only, and may be nil.
|
2018-01-16 13:59:52 +00:00
|
|
|
type KVer interface {
|
2019-01-15 03:32:26 +00:00
|
|
|
KV() map[string]interface{}
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 23:01:28 +00:00
|
|
|
// KVerFunc is a function which implements the KVer interface by calling itself.
|
2019-01-15 03:32:26 +00:00
|
|
|
type KVerFunc func() map[string]interface{}
|
2018-07-18 23:01:28 +00:00
|
|
|
|
|
|
|
// KV implements the KVer interface by calling the KVerFunc itself.
|
2019-01-15 03:32:26 +00:00
|
|
|
func (kvf KVerFunc) KV() map[string]interface{} {
|
2018-07-18 23:01:28 +00:00
|
|
|
return kvf()
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:32:26 +00:00
|
|
|
// KV is a KVer which returns a copy of itself when KV is called.
|
2018-01-16 13:59:52 +00:00
|
|
|
type KV map[string]interface{}
|
|
|
|
|
2019-01-15 03:32:26 +00:00
|
|
|
// KV implements the KVer method by returning a copy of itself.
|
|
|
|
func (kv KV) KV() map[string]interface{} {
|
2019-01-15 03:47:16 +00:00
|
|
|
return map[string]interface{}(kv)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set returns a copy of the KV being called on with the given key/val set on
|
|
|
|
// it. The original KV is unaffected
|
|
|
|
func (kv KV) Set(k string, v interface{}) KV {
|
2019-01-15 03:47:16 +00:00
|
|
|
kvm := make(map[string]interface{}, len(kv)+1)
|
|
|
|
copyM(kvm, kv.KV())
|
|
|
|
kvm[k] = v
|
|
|
|
return KV(kvm)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 03:32:26 +00:00
|
|
|
// returns a key/value map which should not be written to. saves a map-cloning
|
|
|
|
// if KVer is a KV
|
|
|
|
func readOnlyKVM(kver KVer) map[string]interface{} {
|
|
|
|
if kver == nil {
|
|
|
|
return map[string]interface{}(nil)
|
|
|
|
} else if kv, ok := kver.(KV); ok {
|
|
|
|
return map[string]interface{}(kv)
|
|
|
|
}
|
|
|
|
return kver.KV()
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:47:16 +00:00
|
|
|
func copyM(dst, src map[string]interface{}) {
|
|
|
|
for k, v := range src {
|
|
|
|
dst[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 05:47:23 +00:00
|
|
|
// this may take in any amount of nil values, but should never return nil
|
2019-01-15 03:32:26 +00:00
|
|
|
func mergeInto(kv KVer, kvs ...KVer) map[string]interface{} {
|
2019-01-15 03:47:16 +00:00
|
|
|
kvm := map[string]interface{}{}
|
|
|
|
if kv != nil {
|
|
|
|
copyM(kvm, kv.KV())
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
2019-01-15 03:32:26 +00:00
|
|
|
for _, innerKV := range kvs {
|
2019-01-15 03:47:16 +00:00
|
|
|
if innerKV == nil {
|
|
|
|
continue
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
2019-01-15 03:47:16 +00:00
|
|
|
copyM(kvm, innerKV.KV())
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
|
|
|
return kvm
|
|
|
|
}
|
|
|
|
|
2018-07-19 18:43:17 +00:00
|
|
|
type merger struct {
|
|
|
|
base KVer
|
|
|
|
rest []KVer
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge takes in multiple KVers and returns a single KVer which is the union of
|
2018-07-18 23:51:24 +00:00
|
|
|
// all the passed in ones. Key/Vals on the rightmost of the set take precedence
|
2018-01-16 13:59:52 +00:00
|
|
|
// over conflicting ones to the left.
|
2018-07-18 23:51:24 +00:00
|
|
|
//
|
2018-07-19 18:43:17 +00:00
|
|
|
// The KVer returned will call KV() on each of the passed in KVers every time
|
|
|
|
// its KV method is called.
|
2018-01-16 13:59:52 +00:00
|
|
|
func Merge(kvs ...KVer) KVer {
|
2018-07-19 18:43:17 +00:00
|
|
|
if len(kvs) == 0 {
|
|
|
|
return merger{}
|
|
|
|
}
|
|
|
|
return merger{base: kvs[0], rest: kvs[1:]}
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 05:47:23 +00:00
|
|
|
// MergeInto is a convenience function which acts similarly to Merge.
|
|
|
|
func MergeInto(kv KVer, kvs ...KVer) KVer {
|
2018-07-19 18:43:17 +00:00
|
|
|
return merger{base: kv, rest: kvs}
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:32:26 +00:00
|
|
|
func (m merger) KV() map[string]interface{} {
|
2018-07-19 18:43:17 +00:00
|
|
|
return mergeInto(m.base, m.rest...)
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 21:39:57 +00:00
|
|
|
// Prefix prefixes all keys returned from the given KVer with the given prefix
|
|
|
|
// string.
|
2018-07-18 23:01:28 +00:00
|
|
|
func Prefix(kv KVer, prefix string) KVer {
|
2019-01-15 03:32:26 +00:00
|
|
|
return KVerFunc(func() map[string]interface{} {
|
|
|
|
kvm := readOnlyKVM(kv)
|
|
|
|
newKVM := make(map[string]interface{}, len(kvm))
|
|
|
|
for k, v := range kvm {
|
|
|
|
newKVM[prefix+k] = v
|
2018-07-18 23:01:28 +00:00
|
|
|
}
|
2019-01-15 03:32:26 +00:00
|
|
|
return newKVM
|
2018-07-18 23:01:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-11-30 23:50:08 +00:00
|
|
|
// Stringer generates and returns a string.
|
|
|
|
type Stringer interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// String is simply a string which implements Stringer.
|
|
|
|
type String string
|
|
|
|
|
|
|
|
func (str String) String() string {
|
|
|
|
return string(str)
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:59:52 +00:00
|
|
|
// Message describes a message to be logged, after having already resolved the
|
|
|
|
// KVer
|
|
|
|
type Message struct {
|
|
|
|
Level
|
2018-11-30 23:50:08 +00:00
|
|
|
Description Stringer
|
2019-01-13 02:01:00 +00:00
|
|
|
KVer
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringSlice(kv KV) [][2]string {
|
|
|
|
slice := make([][2]string, 0, len(kv))
|
|
|
|
for k, v := range kv {
|
|
|
|
slice = append(slice, [2]string{
|
|
|
|
k,
|
|
|
|
strconv.QuoteToGraphic(fmt.Sprint(v)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sort.Slice(slice, func(i, j int) bool {
|
|
|
|
return slice[i][0] < slice[j][0]
|
|
|
|
})
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
// Handler is a function which can process Messages in some way.
|
|
|
|
//
|
|
|
|
// NOTE that Logger does not handle thread-safety, that must be done inside the
|
|
|
|
// Handler if necessary.
|
|
|
|
type Handler func(msg Message) error
|
|
|
|
|
2019-01-25 21:39:57 +00:00
|
|
|
// DefaultFormat formats and writes the Message to the given Writer using mlog's
|
2018-11-30 21:27:18 +00:00
|
|
|
// default format.
|
|
|
|
func DefaultFormat(w io.Writer, msg Message) error {
|
2018-01-16 13:59:52 +00:00
|
|
|
var err error
|
|
|
|
write := func(s string, args ...interface{}) {
|
|
|
|
if err == nil {
|
|
|
|
_, err = fmt.Fprintf(w, s, args...)
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 23:50:08 +00:00
|
|
|
write("~ %s -- %s", msg.Level.String(), msg.Description.String())
|
2019-01-13 02:01:00 +00:00
|
|
|
if msg.KVer != nil {
|
|
|
|
if kv := msg.KV(); len(kv) > 0 {
|
|
|
|
write(" --")
|
|
|
|
for _, kve := range stringSlice(kv) {
|
|
|
|
write(" %s=%s", kve[0], kve[1])
|
|
|
|
}
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
write("\n")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
// DefaultHandler initializes and returns a Handler which will write all
|
|
|
|
// messages to os.Stderr in a thread-safe way. This is the Handler which
|
|
|
|
// NewLogger will use automatically.
|
|
|
|
func DefaultHandler() Handler {
|
|
|
|
l := new(sync.Mutex)
|
|
|
|
bw := bufio.NewWriter(os.Stderr)
|
|
|
|
return func(msg Message) error {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
err := DefaultFormat(bw, msg)
|
2019-01-13 01:10:45 +00:00
|
|
|
if err == nil {
|
2018-11-30 21:27:18 +00:00
|
|
|
err = bw.Flush()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
// Logger directs Messages to an internal Handler and provides convenient
|
2019-01-30 21:06:24 +00:00
|
|
|
// methods for creating and modifying its own behavior. All methods are
|
|
|
|
// thread-safe.
|
2018-01-16 13:59:52 +00:00
|
|
|
type Logger struct {
|
2019-01-30 21:06:24 +00:00
|
|
|
l *sync.RWMutex
|
2018-11-30 21:27:18 +00:00
|
|
|
h Handler
|
2018-05-28 05:47:23 +00:00
|
|
|
maxLevel uint
|
2019-01-13 02:01:00 +00:00
|
|
|
kv KVer
|
2018-01-16 13:59:52 +00:00
|
|
|
|
|
|
|
testMsgWrittenCh chan struct{} // only initialized/used in tests
|
|
|
|
}
|
|
|
|
|
2018-05-28 05:47:23 +00:00
|
|
|
// NewLogger initializes and returns a new instance of Logger which will write
|
2018-11-30 21:27:18 +00:00
|
|
|
// to the DefaultHandler.
|
|
|
|
func NewLogger() *Logger {
|
|
|
|
return &Logger{
|
2019-01-30 21:06:24 +00:00
|
|
|
l: new(sync.RWMutex),
|
2018-11-30 21:27:18 +00:00
|
|
|
h: DefaultHandler(),
|
2018-10-28 19:09:42 +00:00
|
|
|
maxLevel: InfoLevel.Uint(),
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
// Clone returns an identical instance of the Logger which can be modified
|
|
|
|
// independently of the original.
|
|
|
|
func (l *Logger) Clone() *Logger {
|
2018-11-30 21:27:18 +00:00
|
|
|
l2 := *l
|
2019-01-30 21:06:24 +00:00
|
|
|
l2.l = new(sync.RWMutex)
|
2018-11-30 21:27:18 +00:00
|
|
|
return &l2
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
// SetMaxLevel sets the Logger to not log any messages with a higher Level.Uint
|
|
|
|
// value than of the one given.
|
|
|
|
func (l *Logger) SetMaxLevel(lvl Level) {
|
|
|
|
l.l.Lock()
|
|
|
|
defer l.l.Unlock()
|
|
|
|
l.maxLevel = lvl.Uint()
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
// SetHandler sets the Logger to use the given Handler in order to process
|
|
|
|
// Messages.
|
|
|
|
func (l *Logger) SetHandler(h Handler) {
|
|
|
|
l.l.Lock()
|
|
|
|
defer l.l.Unlock()
|
|
|
|
l.h = h
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
// Handler returns the Handler currently in use by the Logger.
|
|
|
|
func (l *Logger) Handler() Handler {
|
|
|
|
l.l.RLock()
|
|
|
|
defer l.l.RUnlock()
|
|
|
|
return l.h
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
// SetKV sets the Logger to use the merging of the given KVers as a base KVer
|
|
|
|
// for all Messages. If the Logger already had a base KVer (via a previous SetKV
|
|
|
|
// call) then this set will be merged onto that one.
|
|
|
|
func (l *Logger) SetKV(kvs ...KVer) {
|
|
|
|
l.l.Lock()
|
|
|
|
defer l.l.Unlock()
|
2019-01-13 02:01:00 +00:00
|
|
|
l.kv = MergeInto(l.kv, kvs...)
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:47:16 +00:00
|
|
|
// Log can be used to manually log a message of some custom defined Level.
|
|
|
|
//
|
|
|
|
// If the Level is a fatal (Uint() == 0) then calling this will never return,
|
|
|
|
// and the process will have os.Exit(1) called.
|
2018-11-30 23:50:08 +00:00
|
|
|
func (l *Logger) Log(msg Message) {
|
2019-01-30 21:06:24 +00:00
|
|
|
l.l.RLock()
|
|
|
|
defer l.l.RUnlock()
|
|
|
|
|
2018-11-30 23:50:08 +00:00
|
|
|
if l.maxLevel < msg.Level.Uint() {
|
2018-01-16 13:59:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-13 02:01:00 +00:00
|
|
|
if l.kv != nil {
|
|
|
|
msg.KVer = MergeInto(l.kv, msg.KVer)
|
|
|
|
}
|
|
|
|
|
2018-11-30 23:50:08 +00:00
|
|
|
if err := l.h(msg); err != nil {
|
2019-01-15 04:55:22 +00:00
|
|
|
go l.Error("Logger.Handler returned error", merr.KV(err))
|
2018-05-28 05:47:23 +00:00
|
|
|
return
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
if l.testMsgWrittenCh != nil {
|
|
|
|
l.testMsgWrittenCh <- struct{}{}
|
2018-10-28 18:10:11 +00:00
|
|
|
}
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2018-11-30 23:50:08 +00:00
|
|
|
if msg.Level.Uint() == 0 {
|
2018-11-30 21:27:18 +00:00
|
|
|
os.Exit(1)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 23:50:08 +00:00
|
|
|
func mkMsg(lvl Level, descr string, kvs ...KVer) Message {
|
|
|
|
return Message{
|
|
|
|
Level: lvl,
|
|
|
|
Description: String(descr),
|
2019-01-13 02:01:00 +00:00
|
|
|
KVer: Merge(kvs...),
|
2018-11-30 23:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:59:52 +00:00
|
|
|
// Debug logs a DebugLevel message, merging the KVers together first
|
2018-11-30 23:50:08 +00:00
|
|
|
func (l *Logger) Debug(descr string, kvs ...KVer) {
|
|
|
|
l.Log(mkMsg(DebugLevel, descr, kvs...))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info logs a InfoLevel message, merging the KVers together first
|
2018-11-30 23:50:08 +00:00
|
|
|
func (l *Logger) Info(descr string, kvs ...KVer) {
|
|
|
|
l.Log(mkMsg(InfoLevel, descr, kvs...))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warn logs a WarnLevel message, merging the KVers together first
|
2018-11-30 23:50:08 +00:00
|
|
|
func (l *Logger) Warn(descr string, kvs ...KVer) {
|
|
|
|
l.Log(mkMsg(WarnLevel, descr, kvs...))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error logs a ErrorLevel message, merging the KVers together first
|
2018-11-30 23:50:08 +00:00
|
|
|
func (l *Logger) Error(descr string, kvs ...KVer) {
|
|
|
|
l.Log(mkMsg(ErrorLevel, descr, kvs...))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal logs a FatalLevel message, merging the KVers together first. A Fatal
|
|
|
|
// message automatically stops the process with an os.Exit(1)
|
2018-11-30 23:50:08 +00:00
|
|
|
func (l *Logger) Fatal(descr string, kvs ...KVer) {
|
|
|
|
l.Log(mkMsg(FatalLevel, descr, kvs...))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|