blob: dd92ab90880df733631a532c71db8e31fbde17aa [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// Package log provides a global logger for zerolog.
2package log
3
4import (
5 "context"
6 "io"
7 "os"
8
9 "github.com/rs/zerolog"
10)
11
12// Logger is the global logger.
13var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
14
15// Output duplicates the global logger and sets w as its output.
16func Output(w io.Writer) zerolog.Logger {
17 return Logger.Output(w)
18}
19
20// With creates a child logger with the field added to its context.
21func With() zerolog.Context {
22 return Logger.With()
23}
24
25// Level creates a child logger with the minimum accepted level set to level.
26func Level(level zerolog.Level) zerolog.Logger {
27 return Logger.Level(level)
28}
29
30// Sample returns a logger with the s sampler.
31func Sample(s zerolog.Sampler) zerolog.Logger {
32 return Logger.Sample(s)
33}
34
35// Hook returns a logger with the h Hook.
36func Hook(h zerolog.Hook) zerolog.Logger {
37 return Logger.Hook(h)
38}
39
40// Debug starts a new message with debug level.
41//
42// You must call Msg on the returned event in order to send the event.
43func Debug() *zerolog.Event {
44 return Logger.Debug()
45}
46
47// Info starts a new message with info level.
48//
49// You must call Msg on the returned event in order to send the event.
50func Info() *zerolog.Event {
51 return Logger.Info()
52}
53
54// Warn starts a new message with warn level.
55//
56// You must call Msg on the returned event in order to send the event.
57func Warn() *zerolog.Event {
58 return Logger.Warn()
59}
60
61// Error starts a new message with error level.
62//
63// You must call Msg on the returned event in order to send the event.
64func Error() *zerolog.Event {
65 return Logger.Error()
66}
67
68// Fatal starts a new message with fatal level. The os.Exit(1) function
69// is called by the Msg method.
70//
71// You must call Msg on the returned event in order to send the event.
72func Fatal() *zerolog.Event {
73 return Logger.Fatal()
74}
75
76// Panic starts a new message with panic level. The message is also sent
77// to the panic function.
78//
79// You must call Msg on the returned event in order to send the event.
80func Panic() *zerolog.Event {
81 return Logger.Panic()
82}
83
84// WithLevel starts a new message with level.
85//
86// You must call Msg on the returned event in order to send the event.
87func WithLevel(level zerolog.Level) *zerolog.Event {
88 return Logger.WithLevel(level)
89}
90
91// Log starts a new message with no level. Setting zerolog.GlobalLevel to
92// zerolog.Disabled will still disable events produced by this method.
93//
94// You must call Msg on the returned event in order to send the event.
95func Log() *zerolog.Event {
96 return Logger.Log()
97}
98
99// Print sends a log event using debug level and no extra field.
100// Arguments are handled in the manner of fmt.Print.
101func Print(v ...interface{}) {
102 Logger.Print(v...)
103}
104
105// Printf sends a log event using debug level and no extra field.
106// Arguments are handled in the manner of fmt.Printf.
107func Printf(format string, v ...interface{}) {
108 Logger.Printf(format, v...)
109}
110
111// Ctx returns the Logger associated with the ctx. If no logger
112// is associated, a disabled logger is returned.
113func Ctx(ctx context.Context) *zerolog.Logger {
114 return zerolog.Ctx(ctx)
115}