summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-11 04:07:15 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-11 04:07:15 -0400
commita0be15e3a8dd1b9062cb5f9d3a19dffc2a310745 (patch)
treed7e4c88061cac08bacd553e68e32a9acb236ca48
parentb133debd9d6f7d80bfd0279dd1f7ef5259492a58 (diff)
downloadgetwtxt-a0be15e3a8dd1b9062cb5f9d3a19dffc2a310745.tar.gz
logging initialization in init()
-rw-r--r--init.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/init.go b/init.go
new file mode 100644
index 0000000..612bb6d
--- /dev/null
+++ b/init.go
@@ -0,0 +1,27 @@
+package main
+
+import (
+	"log"
+	"os"
+)
+
+// Sets up logging before the main function executes
+func init() {
+	logfile, err := os.OpenFile("getwtxt.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
+	if err != nil {
+		log.Printf("Could not open log file: %v\n", err)
+	}
+
+	// Listen for the signal to close the log file
+	go func() {
+		<-closelog
+		log.Printf("Closing log file ...\n")
+		err = logfile.Close()
+		if err != nil {
+			log.Printf("Couldn't close log file: %v\n", err)
+		}
+	}()
+
+	log.SetOutput(logfile)
+
+}