Skip to content

Commit 1f29da5

Browse files
committed
Include calling filename and line # in error logs
...instead of the filename and line number of the Printf call in log.Error()
1 parent 8dbb4eb commit 1f29da5

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

log/log.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
package log
33

44
import (
5+
"fmt"
56
"log"
67
"os"
8+
"runtime"
79
)
810

911
var (
@@ -13,7 +15,7 @@ var (
1315

1416
func init() {
1517
InfoLog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
16-
ErrorLog = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
18+
ErrorLog = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime)
1719
}
1820

1921
// Info logs an informational message to Stdout.
@@ -23,5 +25,18 @@ func Info(s string, v ...interface{}) {
2325

2426
// Error logs an error to Stderr.
2527
func Error(s string, v ...interface{}) {
26-
ErrorLog.Printf(s, v...)
28+
// Include original caller information
29+
_, file, line, _ := runtime.Caller(1)
30+
31+
// Determine short filename (from standard log package)
32+
short := file
33+
for i := len(file) - 1; i > 0; i-- {
34+
if file[i] == '/' {
35+
short = file[i+1:]
36+
break
37+
}
38+
}
39+
file = short
40+
41+
ErrorLog.Printf(fmt.Sprintf("%s:%d: ", short, line)+s, v...)
2742
}

0 commit comments

Comments
 (0)