...

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

Documentation: github.com/rs/zerolog/log

     1  // +build !binary_log
     2  
     3  package log_test
     4  
     5  import (
     6  	"errors"
     7  	"flag"
     8  	"os"
     9  	"time"
    10  
    11  	"github.com/rs/zerolog"
    12  	"github.com/rs/zerolog/log"
    13  )
    14  
    15  // setup would normally be an init() function, however, there seems
    16  // to be something awry with the testing framework when we set the
    17  // global Logger from an init()
    18  func setup() {
    19  	// UNIX Time is faster and smaller than most timestamps
    20  	// If you set zerolog.TimeFieldFormat to an empty string,
    21  	// logs will write with UNIX time
    22  	zerolog.TimeFieldFormat = ""
    23  	// In order to always output a static time to stdout for these
    24  	// examples to pass, we need to override zerolog.TimestampFunc
    25  	// and log.Logger globals -- you would not normally need to do this
    26  	zerolog.TimestampFunc = func() time.Time {
    27  		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
    28  	}
    29  	log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
    30  }
    31  
    32  // Simple logging example using the Print function in the log package
    33  // Note that both Print and Printf are at the debug log level by default
    34  func ExamplePrint() {
    35  	setup()
    36  
    37  	log.Print("hello world")
    38  	// Output: {"level":"debug","time":1199811905,"message":"hello world"}
    39  }
    40  
    41  // Simple logging example using the Printf function in the log package
    42  func ExamplePrintf() {
    43  	setup()
    44  
    45  	log.Printf("hello %s", "world")
    46  	// Output: {"level":"debug","time":1199811905,"message":"hello world"}
    47  }
    48  
    49  // Example of a log with no particular "level"
    50  func ExampleLog() {
    51  	setup()
    52  	log.Log().Msg("hello world")
    53  
    54  	// Output: {"time":1199811905,"message":"hello world"}
    55  }
    56  
    57  // Example of a conditional level based on the presence of an error.
    58  func ExampleErr() {
    59  	setup()
    60  	err := errors.New("some error")
    61  	log.Err(err).Msg("hello world")
    62  	log.Err(nil).Msg("hello world")
    63  
    64  	// Output: {"level":"error","error":"some error","time":1199811905,"message":"hello world"}
    65  	// {"level":"info","time":1199811905,"message":"hello world"}
    66  }
    67  
    68  // Example of a log at a particular "level" (in this case, "trace")
    69  func ExampleTrace() {
    70  	setup()
    71  	log.Trace().Msg("hello world")
    72  
    73  	// Output: {"level":"trace","time":1199811905,"message":"hello world"}
    74  }
    75  
    76  // Example of a log at a particular "level" (in this case, "debug")
    77  func ExampleDebug() {
    78  	setup()
    79  	log.Debug().Msg("hello world")
    80  
    81  	// Output: {"level":"debug","time":1199811905,"message":"hello world"}
    82  }
    83  
    84  // Example of a log at a particular "level" (in this case, "info")
    85  func ExampleInfo() {
    86  	setup()
    87  	log.Info().Msg("hello world")
    88  
    89  	// Output: {"level":"info","time":1199811905,"message":"hello world"}
    90  }
    91  
    92  // Example of a log at a particular "level" (in this case, "warn")
    93  func ExampleWarn() {
    94  	setup()
    95  	log.Warn().Msg("hello world")
    96  
    97  	// Output: {"level":"warn","time":1199811905,"message":"hello world"}
    98  }
    99  
   100  // Example of a log at a particular "level" (in this case, "error")
   101  func ExampleError() {
   102  	setup()
   103  	log.Error().Msg("hello world")
   104  
   105  	// Output: {"level":"error","time":1199811905,"message":"hello world"}
   106  }
   107  
   108  // Example of a log at a particular "level" (in this case, "fatal")
   109  func ExampleFatal() {
   110  	setup()
   111  	err := errors.New("A repo man spends his life getting into tense situations")
   112  	service := "myservice"
   113  
   114  	log.Fatal().
   115  		Err(err).
   116  		Str("service", service).
   117  		Msgf("Cannot start %s", service)
   118  
   119  	// Outputs: {"level":"fatal","time":1199811905,"error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
   120  }
   121  
   122  // TODO: Panic
   123  
   124  // This example uses command-line flags to demonstrate various outputs
   125  // depending on the chosen log level.
   126  func Example() {
   127  	setup()
   128  	debug := flag.Bool("debug", false, "sets log level to debug")
   129  
   130  	flag.Parse()
   131  
   132  	// Default level for this example is info, unless debug flag is present
   133  	zerolog.SetGlobalLevel(zerolog.InfoLevel)
   134  	if *debug {
   135  		zerolog.SetGlobalLevel(zerolog.DebugLevel)
   136  	}
   137  
   138  	log.Debug().Msg("This message appears only when log level set to Debug")
   139  	log.Info().Msg("This message appears when log level set to Debug or Info")
   140  
   141  	if e := log.Debug(); e.Enabled() {
   142  		// Compute log output only if enabled.
   143  		value := "bar"
   144  		e.Str("foo", value).Msg("some debug message")
   145  	}
   146  
   147  	// Output: {"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}
   148  }
   149  
   150  // TODO: Output
   151  
   152  // TODO: With
   153  
   154  // TODO: Level
   155  
   156  // TODO: Sample
   157  
   158  // TODO: Hook
   159  
   160  // TODO: WithLevel
   161  
   162  // TODO: Ctx
   163  

View as plain text