In my main function, I open a logging text file for writing with a defer close method to close it once the application quits. However, at the beginning of each new day I wish to start writing to the next day's log file, and I don't know how to close the previous day's file and start writing to the current one.
In my main function:
func main() {
f, err := os.OpenFile("2019-07-24.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Error opening log file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
Now when I receive a message on a new day in another package:
func gateway() {
f, err := os.OpenFile("2019-07-25.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Error opening log file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
How do I get a pointer to the previous day's log file from another package, and then close it (when a defer call will not be invoked unless the application completely closes?