Golang log.SetOutput() function example
package log
Golang log.SetOutput() function usage example
package main
import (
"log"
"os"
)
func main() {
// make sure log.txt
// use touch command to create if log.txt does not exist
logFile, err := os.OpenFile("log.txt", os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
defer logFile.Close()
// direct all log messages to log.txt
log.SetOutput(logFile)
log.Println("First log message!")
}
Reference :
Advertisement
Something interesting
Tutorials
+21.2k Golang : How to force compile or remove object files first before rebuild?
+16.3k Golang : Loop each day of the current month example
+6.7k Golang : Skip or discard items of non-interest when iterating example
+16.7k Golang : Gzip file example
+7.1k Golang : Squaring elements in array
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+14.2k Elastic Search : Mapping date format and sort by date
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+9.2k Golang : does not implement flag.Value (missing Set method)
+13.9k Golang : How to check if a file is hidden?
+8.1k Golang : Get all countries phone codes