-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
194 lines (176 loc) · 6.24 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package kiwi
import "fmt"
// This file consists of Logger related structures and functions.
/* Copyright (c) 2016-2019, Alexander I.Grafov <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of kvlog nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ॐ तारे तुत्तारे तुरे स्व */
var (
MessageKey = "message"
ErrorKey = "kiwi-error"
InfoKey = "kiwi-info"
)
type (
// Logger keeps context and log record. There are many loggers initialized
// in different places of application. Loggers are not safe for
// concurrent usage so then you need logger for another goroutine you will need clone existing instance.
// See Logger.New() method below for details.
Logger struct {
context []*Pair
pairs []*Pair
}
// Stringer is the same as fmt.Stringer
Stringer interface {
String() string
}
// Valuer allows log data from any custom types if they conform this interface.
// Also types that conform fmt.Stringer can be used. But as they not have IsQuoted() check
// they always treated as strings and displayed in quotes.
Valuer interface {
Stringer
IsQuoted() bool
}
// Pair is key and value together. They can be used by custom
// helpers for example for logging timestamps or something.
Pair struct {
Key string
Val string
Eval interface{}
Type int
}
)
// Fork creates a new logger instance that inherited the context from
// the global logger. Thi fuction is concurrent safe.
func Fork() *Logger {
var newContext = make([]*Pair, len(context))
global.RLock()
copy(newContext, context)
global.RUnlock()
return &Logger{context: newContext}
}
// New creates a new logger instance but not copy context from the
// global logger. The method is empty now, I keep it for compatibility
// with older versions of API.
func New() *Logger {
return new(Logger)
}
// Fork creates a new instance of the logger. It copies the context
// from the logger from the parent logger. But the values of the
// current record of the parent logger discarded.
func (l *Logger) Fork() *Logger {
var fork = Logger{context: make([]*Pair, len(l.context))}
copy(fork.context, l.context)
return &fork
}
// New creates a new instance of the logger. It not inherited the
// context of the parent logger. The method is empty now, I keep it
// for compatibility with older versions of API.
func (l *Logger) New() *Logger {
return new(Logger)
}
// Log is the most common method for flushing previously added key-val pairs to an output.
// After current record is flushed all pairs removed from a record except contextSrc pairs.
func (l *Logger) Log(keyVals ...interface{}) {
// 1. Log the context.
var record = make([]*Pair, 0, len(l.context)+len(l.pairs)+len(keyVals))
for _, p := range l.context {
if p.Eval != nil {
// Evaluate delayed context value here before output.
record = append(record, &Pair{p.Key, p.Eval.(func() string)(), p.Eval, p.Type})
} else {
record = append(record, &Pair{p.Key, p.Val, nil, p.Type})
}
}
// 2. Log the regular key-value pairs that added before by Add() calls.
for _, p := range l.pairs {
if p.Eval != nil {
record = append(record, &Pair{p.Key, p.Eval.(func() string)(), p.Eval, p.Type})
} else {
record = append(record, &Pair{p.Key, p.Val, nil, p.Type})
}
}
// 3. Log the regular key-value pairs that come in the args.
var (
key string
shouldBeAKey = true
)
for _, val := range keyVals {
if shouldBeAKey {
switch v := val.(type) {
case string:
key = v
case *Pair:
record = append(record, v)
continue
default:
record = append(record, toPair(ErrorKey, fmt.Sprintf("non a string type (%T) for the key (%v)", val, val)))
key = MessageKey
}
} else {
record = append(record, toPair(key, val))
}
shouldBeAKey = !shouldBeAKey
}
if !shouldBeAKey && key != MessageKey {
record = append(record, toPair(MessageKey, key))
}
// 4. Pass the record to the collector.
sinkRecord(record)
l.pairs = nil
}
// Add a new key-value pairs to the log record. If a key already added then value will be
// updated. If a key already exists in a contextSrc then it will be overridden by a new
// value for a current record only. After flushing a record with Log() old context value
// will be restored.
func (l *Logger) Add(keyVals ...interface{}) *Logger {
var (
key string
shouldBeAKey = true
)
// key=val pairs
for _, val := range keyVals {
if shouldBeAKey {
switch v := val.(type) {
case string:
key = v
case *Pair:
l.pairs = append(l.pairs, v)
continue
default:
l.pairs = append(l.pairs, toPair(ErrorKey, fmt.Sprintf("non a string type (%T) for the key (%v)", val, val)))
continue
}
} else {
l.pairs = append(l.pairs, toPair(key, val))
}
shouldBeAKey = !shouldBeAKey
}
if !shouldBeAKey {
l.pairs = append(l.pairs, toPair(MessageKey, key))
}
return l
}
// Reset logger values added after last Log() call. It keeps context untouched.
func (l *Logger) Reset() *Logger {
l.pairs = nil
return l
}