/
types.go
126 lines (104 loc) · 2.92 KB
/
types.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
115
116
117
118
119
120
121
122
123
124
125
126
//
// Copyright (c) 2018 Cavium
//
// SPDX-License-Identifier: Apache-2.0
//
package logging
import (
"os"
"github.com/Circutor/edgex/internal"
"github.com/Circutor/edgex/internal/pkg/db"
"github.com/Circutor/edgex/pkg/clients/logger"
"github.com/Circutor/edgex/pkg/clients/types"
"github.com/Circutor/edgex/pkg/models"
"github.com/go-kit/kit/log"
)
const (
PersistenceDB = "database"
PersistenceFile = "file"
)
type persistence interface {
add(logEntry models.LogEntry) error
closeSession()
remove(criteria matchCriteria) (int, error)
find(criteria matchCriteria) ([]models.LogEntry, error)
// Needed for the tests. Reset the instance (closing files, sessions...)
// and clear the logs.
reset()
}
type privLogger struct {
logLevel *string
rootLogger log.Logger
levelLoggers map[string]log.Logger
}
func newPrivateLogger() privLogger {
pl := privLogger{}
logLevel := logger.InfoLog
pl.logLevel = &logLevel
// Set up the loggers
pl.levelLoggers = map[string]log.Logger{}
pl.rootLogger = log.NewLogfmtLogger(os.Stdout)
pl.rootLogger = log.WithPrefix(pl.rootLogger, "ts", log.DefaultTimestampUTC,
"app", internal.SupportLoggingServiceKey, "source", log.Caller(5))
return pl
}
func (l privLogger) log(logLevel string, msg string, args ...interface{}) {
// Check minimum log level
for _, name := range logger.LogLevels {
if name == *l.logLevel {
break
}
if name == logLevel {
return
}
}
if dbClient != nil {
logEntry := models.LogEntry{
Level: logLevel,
Args: args,
OriginService: internal.SupportLoggingServiceKey,
Message: msg,
Created: db.MakeTimestamp(),
}
dbClient.add(logEntry)
}
if args == nil {
args = []interface{}{"msg", msg}
} else {
if len(args)%2 == 1 {
// add an empty string to keep k/v pairs correct
args = append(args, "")
}
// Practical usage thus far has been to call this type like so Logger.Info("message")
// I'm attempting to preserve that behavior below without requiring the client types
// to provide the "msg" key.
args = append(args, "msg", msg)
}
if l.levelLoggers[logLevel] == nil {
l.levelLoggers[logLevel] = log.WithPrefix(l.rootLogger, "level", logLevel)
}
l.levelLoggers[logLevel].Log(args...)
}
// SetLogLevel sets logger log level
func (l privLogger) SetLogLevel(logLevel string) error {
if logger.IsValidLogLevel(logLevel) == true {
*l.logLevel = logLevel
return nil
}
return types.ErrNotFound{}
}
func (l privLogger) Debug(msg string, args ...interface{}) {
l.log(logger.DebugLog, msg, args...)
}
func (l privLogger) Error(msg string, args ...interface{}) {
l.log(logger.ErrorLog, msg, args...)
}
func (l privLogger) Info(msg string, args ...interface{}) {
l.log(logger.InfoLog, msg, args...)
}
func (l privLogger) Trace(msg string, args ...interface{}) {
l.log(logger.TraceLog, msg, args...)
}
func (l privLogger) Warn(msg string, args ...interface{}) {
l.log(logger.WarnLog, msg, args...)
}