Skip to content

Question: how to use custom logger with the guide 'Exit-Once' #189

Answered by abhinav
tzq0301 asked this question in Q&A
Discussion options

You must be logged in to vote

@tzq0301 The example uses log.Fatal because that's what most folks already use when they exit multiple times.
You have a couple options for main, since the purpose of that specific exit it just to print to stderr and exit:

  • Use log.Fatal. If you want that to not print timestamps, etc. you can do:

    func main() {
        log.SetFlags(0)
        if err := run(); err != nil {
            log.Fatal(err)
        }
    }
  • Print directly to stderr:

    func main() {
        if err := run(); err != nil {
            fmt.Fprintln(os.Stderr, err)
            os.Exit(1)
        }
    }
  • Send the exit code back from run(),
    which uses a custom logger:

    func main() {
        os.Exit(run())
    }
    
    func run() (exitCode int) {
        logger := customLogger()
    …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by tzq0301
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #187 on July 26, 2023 11:26.