-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
114 lines (88 loc) · 2.39 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
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
package writer
import (
"bytes"
"errors"
"fmt"
"regexp"
"time"
)
type LogLevel string
const (
LogLevelTrace LogLevel = "trace"
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
LogLevelWarn LogLevel = "warn"
LogLevelError LogLevel = "error"
LogLevelFatal LogLevel = "fatal"
)
type LogTimeFormat string
const (
LogTimeFormatLocalTime LogTimeFormat = "[Jan 2 2006 15:04:05]"
LogTimeFormatUTC LogTimeFormat = "[2006-01-02T15:04:05Z]"
)
const (
NewLine byte = '\n'
)
// Message is an abstraction of log message
type Log struct {
Timestamp time.Time
Level LogLevel
TimestampFormat LogTimeFormat
Message []byte
}
// NewLog wraps the message with log formatted message
func NewLog(msg []byte, logLevel LogLevel, timestampFormat LogTimeFormat) *Log {
return &Log{
Timestamp: time.Now(),
Level: logLevel,
TimestampFormat: timestampFormat,
Message: msg,
}
}
// Format returns in log format - "timestamp loglevel message"
func (l *Log) Format() []byte {
logInBytes := new(bytes.Buffer)
timestamp := []byte(l.Timestamp.Format(string(l.TimestampFormat)))
logInBytes.Write(timestamp)
logInBytes.Write([]byte(string(' ')))
logInBytes.Write([]byte(l.Level))
logInBytes.Write([]byte(string(' ')))
logInBytes.Write(l.Message)
return logInBytes.Bytes()
}
// Size returns the size of the log
func (l *Log) Size() int {
return len(l.Format())
}
// Unmarshal unmarshalls the byte slice into Log
func (l *Log) Unmarshal(b []byte) error {
re := regexp.MustCompile(`(\[.+\])\s(.+)\s(.+)`)
logLine := re.FindStringSubmatch(string(b))
if len(logLine) != 4 {
return errors.New("error matching log line")
}
l.TimestampFormat = LogTimeFormatLocalTime
timestamp, err := time.Parse(string(LogTimeFormatLocalTime), logLine[1])
if err != nil {
timestamp, err = time.Parse(string(LogTimeFormatUTC), logLine[1])
if err != nil {
return err
}
l.TimestampFormat = LogTimeFormatUTC
}
l.Timestamp = timestamp
l.Level = LogLevel(logLine[2])
l.Message = []byte(logLine[3])
return nil
}
func (l *Log) String() string {
var timestamp string
switch l.TimestampFormat {
case LogTimeFormatLocalTime:
timestamp = l.Timestamp.Format(string(LogTimeFormatLocalTime))
case LogTimeFormatUTC:
timestamp = l.Timestamp.Format(string(LogTimeFormatUTC))
}
log := fmt.Sprintf("%s %s %s", timestamp, l.Level, string(l.Message))
return log
}