rs / zerolog

Zero Allocation JSON Logger
MIT License
10.33k stars 564 forks source link

diode writer does not flush when program exits #666

Closed ttc0419 closed 4 months ago

ttc0419 commented 4 months ago

There will be no output for the following code:

package main

import (
    "fmt"
    "github.com/rs/zerolog"
    "github.com/rs/zerolog/diode"
    "github.com/rs/zerolog/log"
    "os"
)

func main() {
    log.Logger = zerolog.New(
        diode.NewWriter(os.Stderr, 1000, 0, func(missed int) {
            fmt.Printf("Dropped %d messages\n", missed)
        }))
    log.Info().Msg("Test")
}
ttc0419 commented 4 months ago

Okay, according to https://github.com/rs/zerolog/blob/eb081e1fa2325f7aab25a9386e463f67b37b421d/diode/diode_example_test.go#L20 You should close the writer before program exits.