Created
January 4, 2023 16:41
-
-
Save CeoFred/d597de551614581d4506f12c411d3f99 to your computer and use it in GitHub Desktop.
Log errors to a file in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "os" | |
import "log" | |
func main() { | |
logfile,err := os.Create("log.txt") | |
if err != nil { | |
fmt.Println("failed to create log file: ", err) | |
} | |
log.SetOutput(logfile) | |
_, err = os.Open("no-file.txt") | |
if err != nil { | |
fmt.Println("err happened ", err) | |
log.Println(err) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment