/
syslog.go
51 lines (43 loc) · 1004 Bytes
/
syslog.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
package zlog
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
)
type CustomLogger struct {
*log.Logger
}
func NewCustomLogger() *CustomLogger {
return &CustomLogger{log.New(os.Stderr, "", log.LstdFlags)}
}
func (l *CustomLogger) Info(format string, v ...interface{}) {
l.Output(3, fmt.Sprintf("[INFO] %s:%d %s(): %s", getCallerInfo(), getLineNumber(), getFunctionName(), fmt.Sprintf(format, v...)))
}
func (l *CustomLogger) Error(format string, v ...interface{}) {
l.Output(3, fmt.Sprintf("[ERROR] %s:%d %s(): %s", getCallerInfo(), getLineNumber(), getFunctionName(), fmt.Sprintf(format, v...)))
}
func getCallerInfo() string {
_, file, _, ok := runtime.Caller(3)
if !ok {
file = "???"
} else {
file = filepath.Base(file)
}
return file
}
func getFunctionName() string {
pc, _, _, ok := runtime.Caller(3)
if !ok {
return "???"
}
return runtime.FuncForPC(pc).Name()
}
func getLineNumber() int {
_, _, line, ok := runtime.Caller(3)
if !ok {
return 0
}
return line
}