Include calling filename and line # in error logs

...instead of the filename and line number of the Printf call in
log.Error()
This commit is contained in:
Matt Baer
2019-05-23 06:59:22 -04:00
parent 8dbb4ebdc5
commit 1f29da565f
+17 -2
View File
@@ -2,8 +2,10 @@
package log
import (
"fmt"
"log"
"os"
"runtime"
)
var (
@@ -13,7 +15,7 @@ var (
func init() {
InfoLog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
ErrorLog = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
ErrorLog = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime)
}
// Info logs an informational message to Stdout.
@@ -23,5 +25,18 @@ func Info(s string, v ...interface{}) {
// Error logs an error to Stderr.
func Error(s string, v ...interface{}) {
ErrorLog.Printf(s, v...)
// Include original caller information
_, file, line, _ := runtime.Caller(1)
// Determine short filename (from standard log package)
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
ErrorLog.Printf(fmt.Sprintf("%s:%d: ", short, line)+s, v...)
}