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.
|
|
|
|
//
|
2019-02-09 19:08:30 +00:00
|
|
|
// The log methods take in a message string and a Context. The Context can be
|
|
|
|
// loaded with additional annotations which will be included in the log entry as
|
|
|
|
// well (see mctx package).
|
2018-01-16 13:59:52 +00:00
|
|
|
package mlog
|
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2021-04-10 00:05:07 +00:00
|
|
|
"errors"
|
2023-12-26 15:44:53 +00:00
|
|
|
"io"
|
2018-01-16 13:59:52 +00:00
|
|
|
"os"
|
2019-02-03 00:27:10 +00:00
|
|
|
"strings"
|
2018-01-16 13:59:52 +00:00
|
|
|
"sync"
|
2021-02-07 23:04:41 +00:00
|
|
|
"time"
|
2019-01-15 04:55:22 +00:00
|
|
|
|
2023-12-26 15:58:08 +00:00
|
|
|
"code.betamike.com/mediocregopher/mediocre-go-lib/mctx"
|
|
|
|
"code.betamike.com/mediocregopher/mediocre-go-lib/merr"
|
2018-01-16 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
2021-04-10 00:05:07 +00:00
|
|
|
type mlogAnnotation string
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Null is an instance of Logger which will write all Messages to /dev/null.
|
|
|
|
var Null = NewLogger(&LoggerOpts{
|
2023-12-26 15:44:53 +00:00
|
|
|
MessageHandler: NewMessageHandler(io.Discard),
|
2021-02-07 23:04:41 +00:00
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Int gives an integer indicator of the severity of the level, with zero
|
|
|
|
// being most severe. If a Level with a negative Int is logged then the
|
|
|
|
// Logger implementation provided by this package will exit the process.
|
|
|
|
Int() int
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type level struct {
|
|
|
|
s string
|
2021-02-07 23:04:41 +00:00
|
|
|
i int
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l level) String() string {
|
|
|
|
return l.s
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
func (l level) Int() int {
|
2018-01-16 13:59:52 +00:00
|
|
|
return l.i
|
|
|
|
}
|
|
|
|
|
|
|
|
// All pre-defined log levels
|
|
|
|
var (
|
2021-02-07 23:04:41 +00:00
|
|
|
LevelDebug Level = level{s: "DEBUG", i: 40}
|
|
|
|
LevelInfo Level = level{s: "INFO", i: 30}
|
|
|
|
LevelWarn Level = level{s: "WARN", i: 20}
|
|
|
|
LevelError Level = level{s: "ERROR", i: 10}
|
|
|
|
LevelFatal Level = level{s: "FATAL", i: -1}
|
2018-01-16 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
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":
|
2021-02-07 23:04:41 +00:00
|
|
|
return LevelDebug
|
2019-02-03 00:27:10 +00:00
|
|
|
case "INFO":
|
2021-02-07 23:04:41 +00:00
|
|
|
return LevelInfo
|
2019-02-03 00:27:10 +00:00
|
|
|
case "WARN":
|
2021-02-07 23:04:41 +00:00
|
|
|
return LevelWarn
|
2019-02-03 00:27:10 +00:00
|
|
|
case "ERROR":
|
2021-02-07 23:04:41 +00:00
|
|
|
return LevelError
|
2019-02-03 00:27:10 +00:00
|
|
|
case "FATAL":
|
2021-02-07 23:04:41 +00:00
|
|
|
return LevelFatal
|
2019-02-03 00:27:10 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Message describes a message to be logged.
|
2018-01-16 13:59:52 +00:00
|
|
|
type Message struct {
|
2021-02-07 23:04:41 +00:00
|
|
|
Context context.Context
|
2018-01-16 13:59:52 +00:00
|
|
|
Level
|
2019-02-05 20:18:17 +00:00
|
|
|
Description string
|
2021-02-07 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FullMessage extends Message to contain loggable properties not provided
|
|
|
|
// directly by the user.
|
|
|
|
type FullMessage struct {
|
|
|
|
Message
|
|
|
|
Time time.Time
|
|
|
|
Namespace []string
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// LoggerOpts are optional parameters to NewLogger. All fields are optional. A
|
|
|
|
// nil value of LoggerOpts is equivalent to an empty one.
|
|
|
|
type LoggerOpts struct {
|
|
|
|
// MessageHandler is the MessageHandler which will be used to process
|
|
|
|
// Messages.
|
|
|
|
//
|
|
|
|
// Defaults to NewMessageHandler(os.Stderr).
|
|
|
|
MessageHandler MessageHandler
|
|
|
|
|
|
|
|
// MaxLevel indicates the maximum log level which should be handled. See the
|
|
|
|
// Level interface for more.
|
|
|
|
//
|
|
|
|
// Defaults to LevelInfo.Int().
|
|
|
|
MaxLevel int
|
|
|
|
|
|
|
|
// Now returns the current time.Time whenever it is called.
|
|
|
|
//
|
|
|
|
// Defaults to time.Now.
|
|
|
|
Now func() time.Time
|
|
|
|
}
|
2019-02-09 19:08:30 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
func (o *LoggerOpts) withDefaults() *LoggerOpts {
|
|
|
|
out := new(LoggerOpts)
|
|
|
|
if o != nil {
|
|
|
|
*out = *o
|
2018-11-30 21:27:18 +00:00
|
|
|
}
|
2021-02-07 23:04:41 +00:00
|
|
|
|
|
|
|
if out.MessageHandler == nil {
|
|
|
|
out.MessageHandler = NewMessageHandler(os.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.MaxLevel == 0 {
|
|
|
|
out.MaxLevel = LevelInfo.Int()
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.Now == nil {
|
|
|
|
out.Now = time.Now
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Logger creates and directs Messages to an internal MessageHandler. All
|
|
|
|
// methods are thread-safe.
|
2018-01-16 13:59:52 +00:00
|
|
|
type Logger struct {
|
2021-02-07 23:04:41 +00:00
|
|
|
opts *LoggerOpts
|
|
|
|
l *sync.RWMutex
|
|
|
|
ns []string
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// NewLogger initializes and returns a new instance of Logger.
|
|
|
|
func NewLogger(opts *LoggerOpts) *Logger {
|
2018-11-30 21:27:18 +00:00
|
|
|
return &Logger{
|
2021-02-07 23:04:41 +00:00
|
|
|
opts: opts.withDefaults(),
|
|
|
|
l: new(sync.RWMutex),
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Close cleans up all resources held by the Logger.
|
|
|
|
func (l *Logger) Close() error {
|
|
|
|
if err := l.opts.MessageHandler.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) clone() *Logger {
|
2022-11-13 15:05:23 +00:00
|
|
|
|
|
|
|
l2 := &Logger{
|
|
|
|
opts: &LoggerOpts{
|
|
|
|
MessageHandler: l.opts.MessageHandler,
|
|
|
|
MaxLevel: l.opts.MaxLevel,
|
|
|
|
Now: l.opts.Now,
|
|
|
|
},
|
|
|
|
l: new(sync.RWMutex),
|
|
|
|
ns: make([]string, len(l.ns), len(l.ns)+1),
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
copy(l2.ns, l.ns)
|
2022-11-13 15:05:23 +00:00
|
|
|
return l2
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// WithNamespace returns a clone of the Logger with the given value appended to
|
|
|
|
// its namespace array. The namespace array is included in every FullMessage
|
|
|
|
// which is handled by Logger's MessageHandler.
|
|
|
|
func (l *Logger) WithNamespace(name string) *Logger {
|
|
|
|
l = l.clone()
|
|
|
|
l.ns = append(l.ns, name)
|
|
|
|
return l
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:05:23 +00:00
|
|
|
// WithMaxLevel returns a clone of the Logger with the given MaxLevel set as the
|
|
|
|
// value for the maximum log level which will be output (see MaxLevel in
|
|
|
|
// LoggerOpts).
|
|
|
|
func (l *Logger) WithMaxLevel(level int) *Logger {
|
|
|
|
l = l.clone()
|
|
|
|
l.opts.MaxLevel = level
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:11:54 +00:00
|
|
|
// MaxLevel returns the Logger's maximum level which it will log (see MaxLevel
|
|
|
|
// in LoggerOpts, and the WithMaxLevel method).
|
|
|
|
func (l *Logger) MaxLevel() int {
|
|
|
|
return l.opts.MaxLevel
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:47:16 +00:00
|
|
|
// Log can be used to manually log a message of some custom defined Level.
|
|
|
|
//
|
2022-11-13 15:05:23 +00:00
|
|
|
// If the Level is a fatal (Int() < 0) then calling this will never return,
|
2019-01-15 03:47:16 +00:00
|
|
|
// 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()
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
if l.opts.MaxLevel < msg.Level.Int() {
|
2018-01-16 13:59:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
fullMsg := FullMessage{
|
|
|
|
Message: msg,
|
|
|
|
Time: l.opts.Now(),
|
|
|
|
Namespace: l.ns,
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
if err := l.opts.MessageHandler.Handle(fullMsg); err != nil {
|
2021-04-09 23:30:59 +00:00
|
|
|
go l.Error(context.Background(), "MessageHandler.Handle returned error", err)
|
2021-02-07 23:04:41 +00:00
|
|
|
return
|
2018-10-28 18:10:11 +00:00
|
|
|
}
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
if msg.Level.Int() < 0 {
|
2023-12-26 15:44:53 +00:00
|
|
|
if err := l.opts.MessageHandler.Sync(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-11-30 21:27:18 +00:00
|
|
|
os.Exit(1)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 00:05:07 +00:00
|
|
|
func mkMsg(ctx context.Context, lvl Level, descr string) Message {
|
2018-11-30 23:50:08 +00:00
|
|
|
return Message{
|
2021-02-07 23:04:41 +00:00
|
|
|
Context: ctx,
|
2018-11-30 23:50:08 +00:00
|
|
|
Level: lvl,
|
2019-02-05 20:18:17 +00:00
|
|
|
Description: descr,
|
2018-11-30 23:50:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 00:05:07 +00:00
|
|
|
func mkErrMsg(ctx context.Context, lvl Level, descr string, err error) Message {
|
|
|
|
var e merr.Error
|
|
|
|
if !errors.As(err, &e) {
|
|
|
|
ctx = mctx.Annotate(ctx, mlogAnnotation("errMsg"), err.Error())
|
|
|
|
return mkMsg(ctx, lvl, descr)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = mctx.Annotate(ctx,
|
|
|
|
mlogAnnotation("errMsg"), err.Error(),
|
|
|
|
mlogAnnotation("errCtx"), mctx.ContextAsAnnotator(e.Ctx),
|
|
|
|
mlogAnnotation("errLine"), e.Stacktrace.String(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return mkMsg(ctx, lvl, descr)
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Debug logs a LevelDebug message.
|
2021-04-09 23:30:59 +00:00
|
|
|
func (l *Logger) Debug(ctx context.Context, descr string) {
|
|
|
|
l.Log(mkMsg(ctx, LevelDebug, descr))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Info logs a LevelInfo message.
|
2021-04-09 23:30:59 +00:00
|
|
|
func (l *Logger) Info(ctx context.Context, descr string) {
|
|
|
|
l.Log(mkMsg(ctx, LevelInfo, descr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WarnString logs a LevelWarn message which is only a string.
|
|
|
|
func (l *Logger) WarnString(ctx context.Context, descr string) {
|
|
|
|
l.Log(mkMsg(ctx, LevelWarn, descr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn logs a LevelWarn message, including information from the given error.
|
|
|
|
func (l *Logger) Warn(ctx context.Context, descr string, err error) {
|
2021-04-10 00:05:07 +00:00
|
|
|
l.Log(mkErrMsg(ctx, LevelWarn, descr, err))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
// ErrorString logs a LevelError message which is only a string.
|
|
|
|
func (l *Logger) ErrorString(ctx context.Context, descr string) {
|
|
|
|
l.Log(mkMsg(ctx, LevelError, descr))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
// Error logs a LevelError message, including information from the given error.
|
|
|
|
func (l *Logger) Error(ctx context.Context, descr string, err error) {
|
2021-04-10 00:05:07 +00:00
|
|
|
l.Log(mkErrMsg(ctx, LevelError, descr, err))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 01:17:45 +00:00
|
|
|
// FatalString logs a LevelFatal message which is only a string. A Fatal message
|
|
|
|
// automatically stops the process with an os.Exit(1) if the default
|
|
|
|
// MessageHandler is used.
|
|
|
|
func (l *Logger) FatalString(ctx context.Context, descr string) {
|
|
|
|
l.Log(mkMsg(ctx, LevelFatal, descr))
|
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// Fatal logs a LevelFatal message. A Fatal message automatically stops the
|
|
|
|
// process with an os.Exit(1) if the default MessageHandler is used.
|
2022-05-06 01:17:45 +00:00
|
|
|
func (l *Logger) Fatal(ctx context.Context, descr string, err error) {
|
|
|
|
l.Log(mkErrMsg(ctx, LevelFatal, descr, err))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|