-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
64 lines (50 loc) · 1.17 KB
/
logger.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
package chromelogger
import (
"fmt"
"strings"
"runtime"
"encoding/base64"
"encoding/json"
)
const LOG_TYPE_LOG = "log"
const LOG_TYPE_WARN = "warn"
const LOG_TYPE_ERROR = "error"
const LOG_TYPE_INFO = "info"
type Logger struct {
data *Data
}
func (logger *Logger) add(item interface{}, logType string) {
_, file, line, ok := runtime.Caller(2)
var backtrace string
if ok {
backtrace = fmt.Sprintf("%s:%d", file, line)
}
logger.data.AddRow(item, backtrace, logType)
}
func (logger *Logger) Log(item interface{}) {
logger.add(item, LOG_TYPE_LOG)
}
func (logger *Logger) Warn(item interface{}) {
logger.add(item, LOG_TYPE_WARN)
}
func (logger *Logger) Error(item interface{}) {
logger.add(item, LOG_TYPE_ERROR)
}
func (logger *Logger) Info(item interface{}) {
logger.add(item, LOG_TYPE_INFO)
}
func (logger Logger) Export() string {
jsonBytes, err := json.Marshal(logger.data)
if err != nil {
return ""
}
encodedData := base64.StdEncoding.EncodeToString(jsonBytes)
finalData := strings.Replace(encodedData, "\n", "", -1)
return finalData
}
func newLogger() *Logger {
logger := &Logger{
data: NewData(),
}
return logger
}