2022-03-20 19:19:42 +00:00
|
|
|
// Package log provides a global logger for zlog.
|
2017-05-12 05:24:39 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2017-05-20 07:22:37 +00:00
|
|
|
"context"
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
"fmt"
|
2017-08-06 02:47:55 +00:00
|
|
|
"io"
|
2017-05-12 05:24:39 +00:00
|
|
|
"os"
|
2022-03-20 19:19:42 +00:00
|
|
|
"time"
|
2017-05-12 05:24:39 +00:00
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
"git.tuxpa.in/a/zlog"
|
2017-05-12 05:24:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is the global logger.
|
2022-03-20 19:19:42 +00:00
|
|
|
var Logger = zlog.New(os.Stderr).With().Timestamp().Logger()
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
zlog.TimeFieldFormat = time.RFC3339Nano
|
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
|
2017-08-06 02:47:55 +00:00
|
|
|
// Output duplicates the global logger and sets w as its output.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Output(w io.Writer) zlog.Logger {
|
2017-08-06 02:47:55 +00:00
|
|
|
return Logger.Output(w)
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
// With creates a child logger with the field added to its context.
|
2022-03-20 19:19:42 +00:00
|
|
|
func With() zlog.Context {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.With()
|
|
|
|
}
|
|
|
|
|
2018-02-11 02:57:53 +00:00
|
|
|
// Level creates a child logger with the minimum accepted level set to level.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Level(level zlog.Level) zlog.Logger {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Level(level)
|
|
|
|
}
|
|
|
|
|
2017-08-29 01:52:15 +00:00
|
|
|
// Sample returns a logger with the s sampler.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Sample(s zlog.Sampler) zlog.Logger {
|
2017-08-29 01:52:15 +00:00
|
|
|
return Logger.Sample(s)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:52:37 +00:00
|
|
|
// Hook returns a logger with the h Hook.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Hook(h zlog.Hook) zlog.Logger {
|
2017-12-01 17:52:37 +00:00
|
|
|
return Logger.Hook(h)
|
|
|
|
}
|
|
|
|
|
2019-11-19 00:22:50 +00:00
|
|
|
// Err starts a new message with error level with err as a field if not nil or
|
|
|
|
// with info level if err is nil.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Err(err error) *zlog.Event {
|
2019-11-19 00:22:50 +00:00
|
|
|
return Logger.Err(err)
|
|
|
|
}
|
|
|
|
|
2019-11-04 19:39:22 +00:00
|
|
|
// Trace starts a new message with trace level.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Trace() *zlog.Event {
|
2019-11-04 19:39:22 +00:00
|
|
|
return Logger.Trace()
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
// Debug starts a new message with debug level.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Debug() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Debug()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info starts a new message with info level.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Info() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Info()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn starts a new message with warn level.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Warn() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Warn()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error starts a new message with error level.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Error() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Error()
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Errorf sends a log event using debug level and no extra field.
|
|
|
|
// Arguments are handled in the manner of fmt.Errorf.
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
Logger.Error().CallerSkipFrame(1).Msgf(format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errorln(args ...interface{}) {
|
|
|
|
Logger.Error().Msg(fmt.Sprintln(args...))
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
|
|
|
// is called by the Msg method.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Fatal() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Fatal()
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
func Fatalf(format string, args ...interface{}) {
|
2022-03-21 23:17:16 +00:00
|
|
|
Logger.Fatal().Msgf(format, args...)
|
2022-03-20 19:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalln(args ...interface{}) {
|
|
|
|
Logger.Fatal().Msg(fmt.Sprintln(args...))
|
|
|
|
}
|
|
|
|
|
2017-05-12 05:24:39 +00:00
|
|
|
// Panic starts a new message with panic level. The message is also sent
|
|
|
|
// to the panic function.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Panic() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Panic()
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
func Panicf(format string, args ...interface{}) {
|
2022-03-21 23:17:16 +00:00
|
|
|
Logger.Panic().Msgf(format, args...)
|
2022-03-20 19:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Panicln(args ...interface{}) {
|
|
|
|
Logger.Panic().Msg(fmt.Sprintln(args...))
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:33:32 +00:00
|
|
|
// WithLevel starts a new message with level.
|
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func WithLevel(level zlog.Level) *zlog.Event {
|
2017-12-14 18:33:32 +00:00
|
|
|
return Logger.WithLevel(level)
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Log starts a new message with no level. Setting zlog.GlobalLevel to
|
|
|
|
// zlog.Disabled will still disable events produced by this method.
|
2017-05-12 05:24:39 +00:00
|
|
|
//
|
|
|
|
// You must call Msg on the returned event in order to send the event.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Log() *zlog.Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
return Logger.Log()
|
|
|
|
}
|
2017-05-20 07:22:37 +00:00
|
|
|
|
2017-09-02 02:56:35 +00:00
|
|
|
// Print sends a log event using debug level and no extra field.
|
|
|
|
// Arguments are handled in the manner of fmt.Print.
|
|
|
|
func Print(v ...interface{}) {
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
|
2017-09-02 02:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Printf sends a log event using debug level and no extra field.
|
|
|
|
// Arguments are handled in the manner of fmt.Printf.
|
|
|
|
func Printf(format string, v ...interface{}) {
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
Logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
|
2017-09-02 02:56:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
func Println(args ...interface{}) {
|
|
|
|
Logger.Debug().Msg(fmt.Sprintln(args...))
|
|
|
|
}
|
|
|
|
|
2017-05-20 07:22:37 +00:00
|
|
|
// Ctx returns the Logger associated with the ctx. If no logger
|
|
|
|
// is associated, a disabled logger is returned.
|
2022-03-20 19:19:42 +00:00
|
|
|
func Ctx(ctx context.Context) *zlog.Logger {
|
|
|
|
return zlog.Ctx(ctx)
|
2017-05-20 07:22:37 +00:00
|
|
|
}
|