Created
November 22, 2021 20:18
-
-
Save justinschuldt/67d38959c6b9f79061858e88c3f45d4a to your computer and use it in GitHub Desktop.
Golang log to file
This file contains 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 ( | |
"os" | |
"time" | |
) | |
func main() { | |
logFileName := fmt.Sprintf("logs_%v.log", time.Now().String()) | |
logFilePath := "./" + logFileName | |
f, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | |
if err != nil { | |
log.Fatalf("error opening file: %v", err) | |
} | |
defer f.Close() | |
wrt := io.MultiWriter(os.Stdout, f) | |
log.SetOutput(wrt) | |
log.Println("this goes to the conosle, and to the file.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment