/
console.go
73 lines (62 loc) · 1.88 KB
/
console.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
package Oris_Log
import (
"encoding/json"
"fmt"
"os"
)
// ConsoleWriter writes log to console
type ConsoleWriter struct {
ID string
config *config
context map[string]interface{}
}
//SetLogID set the current logger instance's ID
func (l *ConsoleWriter) SetLogID(id string) {
l.ID = id
}
func writer(log *logFormat) {
js,_:=json.Marshal(log)
fmt.Fprintf(os.Stdout, "%s\n", string(js))
}
// NewContext This function is used to add context to the logger instance
func (l *ConsoleWriter) NewContext() Logger {
ctx:=make(map[string]interface{})
return &ConsoleWriter{config: l.config, context: ctx, ID: l.ID}
}
// AddContext Add a new context value to log writer
func (l *ConsoleWriter) AddContext(key string, value interface{}) {
l.context[key] = value
}
// GetContext returns context value based on its key
func (l *ConsoleWriter) GetContext(key string) interface{} {
return l.context[key]
}
// Info log info
func (l *ConsoleWriter) Info(arg0 interface{}, arg1 ...interface{}) {
if _,ok := l.config.Disabled["info"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), l.config.Name, l.context, INFO, l.ID)
writer(&lf)
}
}
// Error log error
func (l *ConsoleWriter) Error(arg0 interface{}, arg1 ...interface{}) {
if _,ok := l.config.Disabled["error"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), l.config.Name, l.context, ERROR, l.ID)
writer(&lf)
}
}
// Debug log debug
func (l *ConsoleWriter) Debug(arg0 interface{}, arg1 ...interface{}) {
if _,ok := l.config.Disabled["debug"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), l.config.Name, l.context, DEBUG, l.ID)
writer(&lf)
}
}
// Fatal log fatal
func (l *ConsoleWriter) Fatal(arg0 interface{}, arg1 ...interface{}) {
if _,ok := l.config.Disabled["fatal"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), l.config.Name, l.context, FATAL, l.ID)
writer(&lf)
os.Exit(0)
}
}