forked from duythinht/gelf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
69 lines (60 loc) · 1.27 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
60
61
62
63
64
65
66
67
68
69
package gelf
import (
"encoding/json"
"os"
"time"
)
// Log struct
/*
{
"version": "1.1",
"host": "example.org",
"short_message": "A short message that helps you identify what is going on",
"full_message": "Backtrace here\n\nmore stuff",
"timestamp": 1385053862.3072,
"level": 1,
"_user_id": 9001,
"_some_info": "foo",
"_some_env_var": "bar"
}
*/
type Log struct {
Version string `json:"version"`
Host string `json:"host"`
ShortMessage string `json:"short_message"`
FullMessage string `json:"full_message"`
Timestamp int64 `json:"timestamp"`
Level int `json:"level"`
}
func Create(message string) *Log {
return &Log{
Version: "1.1",
Host: "default",
ShortMessage: message,
Timestamp: time.Now().Unix(),
Level: 1,
}
}
func (self *Log) SetTimestamp(timestamp int64) *Log {
self.Timestamp = timestamp
return self
}
func (self *Log) SetHost(host string) *Log {
self.Host = host
return self
}
func (self *Log) SetFullMessage(fullMessage string) *Log {
self.FullMessage = fullMessage
return self
}
func (self *Log) SetLevel(level int) *Log {
self.Level = level
return self
}
func (self *Log) ToJSON() string {
message, err := json.Marshal(self)
if err != nil {
os.Exit(1)
}
return string(message)
}