Pioz
Pioz

Reputation: 6321

How to suppress logs

If I have a code in Go like this:

package main

import "fmt"
import "log"

func main() {
    fmt.Println("hello world")
    log.Println("log hello world")
}

is it possible to run this program suppressing the log output without change the source code, passing a flag or env variable from command line like for example QUIET=1 go run hello?

Upvotes: 1

Views: 1295

Answers (1)

Jory Geerts
Jory Geerts

Reputation: 1977

You can either have the shell discard the output using go run hello 2>/dev/null (as @TimCooper mentioned in the comments), or you can overwrite the output of the log package by calling log.SetOutput() and passing an implementation of io.Writer that discards the input (or writes it to whatever you want).

Upvotes: 1

Related Questions