Go Package log: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 4: Line 4:


=Internal=
=Internal=
 
* [[Go_Logging#Standard_Library_log_Package|Go Logging]]
* [[Go Concepts - Standard Library#Packages|Standard Library]]
* [[Go_Language_Modularization#log|Standard Library]]


=Overview=
=Overview=


The "log" package provides support for logging messages to <tt>stdout</tt>, <tt>stderr</tt> or custom devices.
The "log" package provides support for logging messages to <tt>stdout</tt>, <tt>stderr</tt> or custom devices.
<syntaxhighlight lang='go'>
import (
    "log"
)
...
log.Printf("something %s\n", "else")
</syntaxhighlight >


=Elements=
=Elements=
 
==<tt>log.Fatal()</tt>, <tt>log.Fatalf()</tt>==
* <tt>[https://golang.org/pkg/log/#Fatal log.Fatal]</tt>
{{External|https://golang.org/pkg/log/#Fatal}}
<code>Fatal</code> is equivalent to <code>Print()</code> followed by a call to <code>os.Exit(1)</code>.


=Recipes=
=Recipes=
Line 19: Line 28:
==Changing the Logging Device==
==Changing the Logging Device==


By default, the logger is set to write to <tt>stderr</tt>. To change that:
By default, the logger is set to write to <code>stderr</code>. To change that, execute <code>log.SetOutput()</code> from the <code>[[Go_Functions#init.28.29|init()]]</code> function, or at least before using the logging system.
 
===Log to <tt>stdout</tt>===


<pre>
<syntaxhighlight lang='go'>
import (
import (
     "log"
     "log"
Line 32: Line 43:
     log.SetOutput(os.Stdout)
     log.SetOutput(os.Stdout)
}
}
</pre>
</syntaxhighlight >
 
===Log to a File===
<syntaxhighlight lang='go'>
import (
    "log"
    "os"
)
...
func init() {
  // change the device for logging to a custom file
  fileName := "/tmp/somefile.log"
  f, err := os.Create(fileName)
  if err != nil {
      fmt.Printf("failed to open log file %s for writing\n", fileName)
      ...
  } else {
      log.SetOutput(f)
  }
}
</syntaxhighlight >

Latest revision as of 21:11, 3 January 2024

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(), log.Fatalf()

https://golang.org/pkg/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, execute log.SetOutput() from the init() function, or at least before using the logging system.

Log to stdout

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

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

Log to a File

import (
    "log"
    "os"
)
...
func init() {
   // change the device for logging to a custom file 
   fileName := "/tmp/somefile.log"
   f, err := os.Create(fileName)
   if err != nil {
      fmt.Printf("failed to open log file %s for writing\n", fileName)
      ...
   } else {
      log.SetOutput(f)
   }
}