/
mongoLogger.go
78 lines (67 loc) · 1.93 KB
/
mongoLogger.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
package Oris_Log
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"log"
"os"
)
// MongoWriter
type MongoWriter struct {
config *config
db *mongo.Collection
context map[string]interface{}
ID string
}
//SetLogID set the current logger instance's ID
func (m *MongoWriter) SetLogID(id string) {
m.ID = id
}
// NewContext This function is used to add context to a log record.
func (m *MongoWriter) NewContext() Logger {
ctx:=make(map[string]interface{})
return &MongoWriter{config: m.config, context: ctx, ID: m.ID}
}
// AddContext Add a new context value to log writer
func (l *MongoWriter) AddContext(key string, value interface{}) {
l.context[key] = value
}
// GetContext returns context value based on its key
func (l *MongoWriter) GetContext(key string) interface{} {
return l.context[key]
}
// Info log info
func (m *MongoWriter) Info(arg0 interface{}, arg1 ...interface{}) {
if _,ok := m.config.Disabled["info"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), m.config.Name, m.context, INFO, m.ID)
m.writer(&lf)
}
}
// Error log error
func (m *MongoWriter) Error(arg0 interface{}, arg1 ...interface{}) {
if _,ok := m.config.Disabled["error"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), m.config.Name, m.context, ERROR, m.ID)
writer(&lf)
}
}
// Debug log debug
func (m *MongoWriter) Debug(arg0 interface{}, arg1 ...interface{}) {
if _,ok := m.config.Disabled["debug"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), m.config.Name, m.context, DEBUG, m.ID)
writer(&lf)
}
}
// Fatal log fatal
func (m *MongoWriter) Fatal(arg0 interface{}, arg1 ...interface{}) {
if _,ok := m.config.Disabled["fatal"]; !ok {
lf := prepareLog(fmt.Sprintf(arg0.(string), arg1...), m.config.Name, m.context, FATAL, m.ID)
writer(&lf)
os.Exit(0)
}
}
func (m *MongoWriter) writer(l *logFormat) {
_, err := m.db.InsertOne(context.Background(), &l)
if err != nil {
log.Println(err)
}
}