Home > Backend Development > Golang > How to Disable the Standard Logger in Go?

How to Disable the Standard Logger in Go?

Susan Sarandon
Release: 2024-11-11 18:20:03
Original
487 people have browsed it

How to Disable the Standard Logger in Go?

How to Disable the Standard Logger in Go

If you're looking to disable logging in your Go code that utilizes the log package, you may wonder about the best approach. Contrary to what you might think, setting a flag or commenting out log calls isn't necessary.

Solution Using io/ioutil

Prior to Go 1.16, you can disable logging by setting the output to ioutil.Discard, a pre-defined io.Writer that ignores all writes:

import (
    "log"
    "io/ioutil"
)

func init() {
    log.SetOutput(ioutil.Discard)
}
Copy after login

Solution Using io.Discard in Go 1.16

In Go 1.16 and later, you can simply use io.Discard directly, as it has been exported from the io/ioutil package:

log.SetOutput(io.Discard)
Copy after login

This will effectively disable all logging output from the standard logger.

The above is the detailed content of How to Disable the Standard Logger in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template