forked from hanchuanchuan/bingo2sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
98 lines (84 loc) · 2.06 KB
/
log.go
File metadata and controls
98 lines (84 loc) · 2.06 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package bingo2sql
import (
"fmt"
"path"
"runtime"
"strings"
log "github.com/sirupsen/logrus"
)
var (
// BuildTS 打包时间
BuildTS = ""
// GitHash GIT提交ID
GitHash = ""
// GitBranch GIT分支
GitBranch = ""
// GoVersion GOLANG版本
GoVersion = ""
// GitVersion GIT版本
GitVersion = ""
)
func init() {
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
// log.SetFormatter(&log.TextFormatter{
// DisableColors: true,
// FullTimestamp: true,
// })
// log.SetReportCaller(true)
// 输出文件名和行号
log.AddHook(&contextHook{})
}
// modifyHook injects file name and line pos into log entry.
type contextHook struct{}
// Fire implements logrus.Hook interface
// https://github.com/sirupsen/logrus/issues/63
func (hook *contextHook) Fire(entry *log.Entry) error {
pc := make([]uintptr, 4)
cnt := runtime.Callers(6, pc)
for i := 0; i < cnt; i++ {
fu := runtime.FuncForPC(pc[i] - 1)
name := fu.Name()
if !isSkippedPackageName(name) {
file, line := fu.FileLine(pc[i] - 1)
entry.Data["file"] = path.Base(file)
entry.Data["line"] = line
break
}
}
return nil
}
// Levels implements logrus.Hook interface.
func (hook *contextHook) Levels() []log.Level {
return log.AllLevels
}
// isSKippedPackageName tests wether path name is on log library calling stack.
func isSkippedPackageName(name string) bool {
return strings.Contains(name, "github.com/sirupsen/logrus") ||
strings.Contains(name, "github.com/coreos/pkg/capnslog")
}
// PrintVersion prints the version information.
func PrintVersion() {
log.Infof("Version: %s", GitVersion)
log.Infof("Git Commit Hash: %s", GitHash)
log.Infof("Git Branch: %s", GitBranch)
log.Infof("UTC Build Time: %s", BuildTS)
log.Infof("Go Version: %s", GoVersion)
}
// GetInfo returns the git hash and build time of this -server binary.
func GetInfo() string {
return fmt.Sprintf(
"Version: %v\n"+
"Git Commit Hash: %s\n"+
"Git Branch: %s\n"+
"UTC Build Time: %s\n"+
"Go Version: %s",
GitVersion,
GitHash,
GitBranch,
BuildTS,
GoVersion,
)
}