...

Source file src/github.com/rs/zerolog/log/log.go

Documentation: github.com/rs/zerolog/log

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

View as plain text