Go Package log: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 28: Line 28:


By default, the logger is set to write to <tt>stderr</tt>. To change that:
By default, the logger is set to write to <tt>stderr</tt>. To change that:
===Log to <tt>stdout</tt>===


<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
Line 41: Line 43:
}
}
</syntaxhighlight >
</syntaxhighlight >
===Log to a File===

Revision as of 23:52, 18 December 2023

External

Internal

Overview

The "log" package provides support for logging messages to stdout, stderr or custom devices.

import (
    "log"
)
...

log.Printf("something %s\n", "else")

Elements

  • log.Fatal: Fatal is equivalent to Print() followed by a call to os.Exit(1).

Recipes

Changing the Logging Device

By default, the logger is set to write to stderr. To change that:

Log to stdout

import (
    "log"
    "os"
)
...
func init() {

    // change the device for logging to stdout
    log.SetOutput(os.Stdout)
}

Log to a File