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:
+17
-2
@@ -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...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user