From 9519a709dda7d697dd6376a23e357c2a3e11b96e Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 3 Mar 2023 15:29:48 -0500 Subject: [PATCH] docs: update readme examples --- README.md | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 61ac81c..93ac3bd 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ log.Print("Baking 101") Use `New()` to create new loggers. ```go -logger := log.New() +logger := log.New(os.Stderr) if butter { logger.Warn("chewy!", "butter", true) } @@ -115,8 +115,8 @@ log.ErrorLevel log.FatalLevel ``` -Use `log.SetLevel` or create a new logger with the `log.WithLevel` option to -set the level. +Use `log.SetLevel()` to set the log level. You can also create a new logger with +a specific log level using `log.Options{Level: }`. Use the corresponding function to log a message: @@ -143,13 +143,16 @@ log.Debug("Available ingredients", "ingredients", ingredients) ### Options -You can customize the logger with options. Use `log.WithCaller()` to enable -printing source location. `log.WithTimestamp()` prints the timestamp of each -log. +You can customize the logger with options. Use `log.NewWithOptions()` and +`log.Options{}` to customize your new logger. ```go -logger := log.New(log.WithTimestamp(), log.WithTimeFormat(time.Kitchen), - log.WithCaller(), log.WithPrefix("Baking 🍪 ")) +logger := log.NewWithOptions(os.Stderr, log.Options{ + ReportCaller: true, + ReportTimestamp: true, + TimeFormat: time.Kitchen, + Prefix: "Baking 🍪 " +}) logger.Info("Starting oven!", "degree", 375) time.Sleep(10 * time.Minute) logger.Info("Finished baking") @@ -162,8 +165,17 @@ logger.Info("Finished baking") -Use `log.SetFormatter()` or `log.WithFormatter()` to change the output format. -Available options are: +You can also use logger setters to customize the logger. + +```go +logger := log.New(os.Stderr) +logger.SetReportTimestamp(false) +logger.SetReportCaller(false) +logger.SetLevel(log.DebugLevel) +``` + +Use `log.SetFormatter()` or `log.Options{Formatter: }` to change the output +format. Available options are: - `log.TextFormatter` (_default_) - `log.JSONFormatter` @@ -174,14 +186,6 @@ Available options are: For a list of available options, refer to [options.go](./options.go). -Set the logger level and options. - -```go -logger.SetReportTimestamp(false) -logger.SetReportCaller(false) -logger.SetLevel(log.DebugLevel) -``` - ### Styles You can customize the logger styles using [Lipgloss][lipgloss]. The styles are @@ -293,7 +297,8 @@ For this, you can use the standard log adapter, which simply wraps the logger in a `*log.Logger` interface. ```go -stdlog := log.New(log.WithPrefix("http")).StandardLog(log.StandardLogOption{ +logger := log.NewWithOptions(os.Stderr, log.Options{Prefix: "http"}) +stdlog := logger.StandardLog(log.StandardLogOptions{ ForceLevel: log.ErrorLevel, }) s := &http.Server{