From e07a4c980b60c5c4f94fcfb8b86948cbfbc2d7d6 Mon Sep 17 00:00:00 2001 From: phuslu Date: Sun, 2 Jun 2024 22:32:27 +0800 Subject: [PATCH] add writev example to readme --- README.md | 58 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index f300913a..5c8cdcda 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ - `IOWriter`, *io.Writer wrapper* - `ConsoleWriter`, *colorful & formatting* - `FileWriter`, *rotating & effective* + - `AsyncWriter`, *asynchronously & performant* - `MultiLevelWriter`, *multiple level dispatch* - `SyslogWriter`, *memory efficient syslog* - `JournalWriter`, *linux systemd logging* - `EventlogWriter`, *windows system event* - - `AsyncWriter`, *asynchronously writing* * Stdlib Log Adapter - `Logger.Std`, *transform to std log instances* - `Logger.Slog`, *transform to log/slog instances* @@ -187,7 +187,7 @@ func main() { ``` > Note: By default log writes to `os.Stderr` -### Customize the configuration and formatting: +### Customize the logger fields: To customize logger filed name and format. [![playground][play-customize-img]][play-customize] ```go @@ -477,6 +477,34 @@ func main() { } ``` +### AsyncWriter + +To logging asynchronously for performance stability, use `AsyncWriter`. + +```go +logger := log.Logger{ + Level: log.InfoLevel, + Writer: &log.AsyncWriter{ + ChannelSize: 4096, + WritevEnabled: true, + Writer: &log.FileWriter{ + Filename: "main.log", + FileMode: 0600, + MaxSize: 50 * 1024 * 1024, + MaxBackups: 7, + LocalTime: false, + }, + }, +} + +logger.Info().Int("number", 42).Str("foo", "bar").Msg("a async info log") +logger.Warn().Int("number", 42).Str("foo", "bar").Msg("a async warn log") +logger.Writer.(io.Closer).Close() +``` +*Highlights*: +- To flush data and quit safely, call `AsyncWriter.Close()` explicitly. +- To boost write performance by up to 10x under high load, enable the `WritevEnabled` option. + ### Random Sample Logger: To logging only 5% logs, use below idiom. @@ -655,32 +683,6 @@ log.DefaultLogger.Writer = &log.EventlogWriter{ log.Info().Int("number", 42).Str("foo", "bar").Msg("hello world") ``` -### AsyncWriter - -To logging asynchronously for performance stability, use `AsyncWriter`. - -```go -logger := log.Logger{ - Level: log.InfoLevel, - Writer: &log.AsyncWriter{ - ChannelSize: 100, - Writer: &log.FileWriter{ - Filename: "main.log", - FileMode: 0600, - MaxSize: 50*1024*1024, - MaxBackups: 7, - LocalTime: false, - }, - }, -} - -logger.Info().Int("number", 42).Str("foo", "bar").Msg("a async info log") -logger.Warn().Int("number", 42).Str("foo", "bar").Msg("a async warn log") -logger.Writer.(io.Closer).Close() -``` - -> Note: To flush data and quit safely, call `AsyncWriter.Close()` explicitly. - ### Stdlib Log Adapter Using wrapped loggers for stdlog. [![playground][play-stdlog-img]][play-stdlog]