-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
59 lines (50 loc) · 1.31 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package log
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
const flags = log.Ldate | log.Ltime | log.LUTC | log.Lmsgprefix
var (
info = log.New(os.Stderr, "[INFO] ", flags)
err = log.New(os.Stderr, "[ERROR] ", flags)
fatal = log.New(os.Stderr, "[FATAL] ", flags)
packageDir string
)
func init() {
_, file, _, ok := runtime.Caller(0)
if !ok {
panic("failed to get package directory information")
}
packageDir = filepath.Join(filepath.Dir(file), "..", "..")
}
func getLocation() string {
_, file, line, ok := runtime.Caller(2)
if !ok {
return "unknown location"
}
file = strings.TrimPrefix(file, packageDir)[1:]
return fmt.Sprintf("%v:%v", file, line)
}
// Infof logs an informational message.
func Infof(str string, args ...interface{}) {
loc := getLocation()
msg := fmt.Sprintf(str, args...)
info.Printf("(%v) %v", loc, msg)
}
// Errorf logs an error. As a special case, it returns a new error
// constructed from its arguments via fmt.Errorf.
func Errorf(str string, args ...interface{}) error {
loc := getLocation()
nerr := fmt.Errorf(str, args...)
err.Printf("(%v) %v", loc, nerr)
return nerr
}
// Fatalf logs a fatal error and immediately exits.
func Fatalf(str string, args ...interface{}) {
loc := getLocation()
fatal.Fatalf("(%v) %v", loc, fmt.Sprintf(str, args...))
}