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
+31.7k Golang : How to convert(cast) string to IP address?
+17.8k Golang : Iterate linked list example
+10.5k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+7.3k Golang : alternative to os.Exit() function
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame
+13.8k Golang : unknown escape sequence error
+5.2k Golang : Issue HTTP commands to server and port example
+28.6k Golang : Read, Write(Create) and Delete Cookie example
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+19.2k Golang : Check whether a network interface is up on your machine
+16.6k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error