forked from jirs5/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsrampchanges.txt
149 lines (125 loc) · 4 KB
/
opsrampchanges.txt
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
Change 1:
Import “runtime”
Import “strconv”
Import “syscall”
Following lines to be added inside func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {} in text_formatter.go
f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat))
f.appendKeyValue(b, "process ID", strconv.Itoa(syscall.Getpid()))
f.appendKeyValue(b, "thread ID", strconv.Itoa(GetCurrentThreadId()))
f.appendKeyValue(b, "OS", detectOS())
if key == "source_file" {
n := strings.LastIndexByte(entry.Data[key].(string), '/')
f.appendKeyValue(b, key, entry.Data[key].(string)[n+1:])
} else {
}
--------------------------------------------
Note: It must be in the following order to get our customise logs inside func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {} in text_formatter.go
f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat))
f.appendKeyValue(b, "level", entry.Level.String())
f.appendKeyValue(b, "process ID", strconv.Itoa(syscall.Getpid()))
f.appendKeyValue(b, "thread ID", strconv.Itoa(GetCurrentThreadId()))
f.appendKeyValue(b, "OS", detectOS())
for _, key := range keys {
if key == "source_file" {
n := strings.LastIndexByte(entry.Data[key].(string), '/')
f.appendKeyValue(b, key, entry.Data[key].(string)[n+1:])
} else {
f.appendKeyValue(b, key, entry.Data[key])
}
}
if entry.Message != "" {
f.appendKeyValue(b, "msg", entry.Message)
}
which print logs in order like
22-06-2018 07:27:57 [debug] [pid 7202] [tid 7206] [L] [connectivity:676] Sending Heart Beat message...!
Change 2:
Replace existing function appendKeyValue() definition as shown below
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
switch value := value.(type) {
case string:
if key == "time" {
arrstr := strings.Split(value, "T")
arr := strings.Split(arrstr[0], "-")
for i := len(arr) - 1; i >= 0; i-- {
if i == 0 {
fmt.Fprintf(b, "%s ", arr[i])
break
}
fmt.Fprintf(b, "%s"+"-", arr[i])
}
time := arrstr[1]
fmt.Fprintf(b, "%s", time[:8])
break
} else if key == "level" {
fmt.Fprintf(b, "[%s]", value)
break
} else if key == "process ID" {
fmt.Fprintf(b, "[pid %s]", value)
break
} else if key == "thread ID" {
fmt.Fprintf(b, "[tid %s]", value)
break
} else if key == "OS" {
fmt.Fprintf(b, "[%s]", value)
break
} else if key == "msg" {
fmt.Fprintf(b, "%s", value)
break
} else if key == "source_file" {
fmt.Fprintf(b, "[%s]", strings.Replace(value, ".go", "", -1))
break
}
fmt.Fprintf(b, "%s", value)
case error:
errmsg := value.Error()
if !f.needsQuoting(errmsg) {
b.WriteString(errmsg)
} else {
fmt.Fprintf(b, "%q", errmsg)
}
default:
fmt.Fprint(b, value)
}
b.WriteByte(' ')
}
Change 3:
Add the below function definition in text_formatter.go
func detectOS() string {
switch osplatform := runtime.GOOS; osplatform {
case "windows":
return "W"
case "darwin":
return "M"
default:
return "L"
}
}
Change 4:
Place the termical_windows.go file in same directory for getting thread ID.
Change 5:
Create copy of terminal_bsd.go and rename to termical_darwin.go
Remove darwin in the +build comment section in terminal_bsd.go
Keep +build darwin comment section in termical_darwin.go
copy below function definition in termical_darwin.go
Import “runtime”
Import “strconv”
Import “string”
func GetCurrentThreadId() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
return 0
}
return id
}
Place the termical_darwin.go file in same directory
Change 6:
copy below function definition in terminal_linux.go, terminal_bsd.go
Import “syscall”
func GetCurrentThreadId() int {
return syscall.Gettid()
}
Change 7:
In latest logrus version sourcefile folder is removed and since agent importing github.com/Sirupsen/logrus/hooks/sourcefile for loglevel and filename we need to maintain this package.