git » gofer » commit 1b9fa16

util: Log time only when writing to a file

author Alberto Bertogli
2017-08-11 01:58:07 UTC
committer Alberto Bertogli
2017-08-11 01:58:07 UTC
parent 8221b0ae3d859a7de57aaa8ab96a6424f2b4e634

util: Log time only when writing to a file

If we're logging to stdout or to syslog, logging the time is not
relevant and just makes the log harder to read.

util/util.go +7 -5

diff --git a/util/util.go b/util/util.go
index 43c721e..55420b8 100644
--- a/util/util.go
+++ b/util/util.go
@@ -32,26 +32,28 @@ var (
 func InitLog() {
 	var err error
 	var logfd io.Writer
+	var flags int
 
 	if *logfile == "-" {
 		logfd = os.Stdout
+		flags |= log.Lshortfile
 	} else if *logfile != "" {
 		logfd, err = os.OpenFile(*logfile,
-			os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
+			os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0664)
 		if err != nil {
-			log.Fatalf("error opening log file %s: %v",
-				*logfile, err)
+			log.Fatalf("error opening log file %s: %v", *logfile, err)
 		}
+		flags |= log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile
 	} else {
 		logfd, err = syslog.New(
 			syslog.LOG_INFO|syslog.LOG_DAEMON, "gofer")
 		if err != nil {
 			log.Fatalf("error opening syslog: %v", err)
 		}
+		flags |= log.Lshortfile
 	}
 
-	Log = log.New(logfd, "",
-		log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
+	Log = log.New(logfd, "", flags)
 }
 
 // LoadCerts loads certificates from the given directory, and returns a TLS