summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/vendor/src/github.com/spacemonkeygo/spacelog/collection.go
blob: fd612db6ebd1d1b14a761547877bd2202aeb696d (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2014 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package spacelog

import (
	"regexp"
	"runtime"
	"strings"
	"sync"
	"text/template"
)

var (
	// If set, these prefixes will be stripped out of automatic logger names.
	IgnoredPrefixes []string

	badChars = regexp.MustCompile("[^a-zA-Z0-9_.-]")
	slashes  = regexp.MustCompile("[/]")
)

func callerName() string {
	pc, _, _, ok := runtime.Caller(2)
	if !ok {
		return "unknown.unknown"
	}
	f := runtime.FuncForPC(pc)
	if f == nil {
		return "unknown.unknown"
	}
	name := f.Name()
	for _, prefix := range IgnoredPrefixes {
		name = strings.TrimPrefix(name, prefix)
	}
	return badChars.ReplaceAllLiteralString(
		slashes.ReplaceAllLiteralString(name, "."), "_")
}

// LoggerCollections contain all of the loggers a program might use. Typically
// a codebase will just use the default logger collection.
type LoggerCollection struct {
	mtx     sync.Mutex
	loggers map[string]*Logger
	level   LogLevel
	handler Handler
}

// NewLoggerCollection creates a new logger collection. It's unlikely you will
// ever practically need this method. Use the DefaultLoggerCollection instead.
func NewLoggerCollection() *LoggerCollection {
	return &LoggerCollection{
		loggers: make(map[string]*Logger),
		level:   DefaultLevel,
		handler: defaultHandler}
}

// GetLogger returns a new Logger with a name automatically generated using
// the callstack. If you want to avoid automatic name generation check out
// GetLoggerNamed
func (c *LoggerCollection) GetLogger() *Logger {
	return GetLoggerNamed(callerName())
}

func (c *LoggerCollection) getLogger(name string, level LogLevel,
	handler Handler) *Logger {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	logger, exists := c.loggers[name]
	if !exists {
		logger = &Logger{level: level,
			collection: c,
			name:       name,
			handler:    handler}
		c.loggers[name] = logger
	}
	return logger
}

// GetLoggerNamed returns a new Logger with the provided name. GetLogger is
// more frequently used.
func (c *LoggerCollection) GetLoggerNamed(name string) *Logger {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	logger, exists := c.loggers[name]
	if !exists {
		logger = &Logger{level: c.level,
			collection: c,
			name:       name,
			handler:    c.handler}
		c.loggers[name] = logger
	}
	return logger
}

// SetLevel will set the current log level for all loggers with names that
// match a provided regular expression. If the regular expression is nil, then
// all loggers match.
func (c *LoggerCollection) SetLevel(re *regexp.Regexp, level LogLevel) {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	if re == nil {
		c.level = level
	}
	for name, logger := range c.loggers {
		if re == nil || re.MatchString(name) {
			logger.setLevel(level)
		}
	}
}

// SetHandler will set the current log handler for all loggers with names that
// match a provided regular expression. If the regular expression is nil, then
// all loggers match.
func (c *LoggerCollection) SetHandler(re *regexp.Regexp, handler Handler) {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	if re == nil {
		c.handler = handler
	}
	for name, logger := range c.loggers {
		if re == nil || re.MatchString(name) {
			logger.setHandler(handler)
		}
	}
}

// SetTextTemplate will set the current text template for all loggers with
// names that match a provided regular expression. If the regular expression
// is nil, then all loggers match. Note that not every handler is guaranteed
// to support text templates and a text template will only apply to
// text-oriented and unstructured handlers.
func (c *LoggerCollection) SetTextTemplate(re *regexp.Regexp,
	t *template.Template) {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	if re == nil {
		c.handler.SetTextTemplate(t)
	}
	for name, logger := range c.loggers {
		if re == nil || re.MatchString(name) {
			logger.getHandler().SetTextTemplate(t)
		}
	}
}

// SetTextOutput will set the current output interface for all loggers with
// names that match a provided regular expression. If the regular expression
// is nil, then all loggers match. Note that not every handler is guaranteed
// to support text output and a text output interface will only apply to
// text-oriented and unstructured handlers.
func (c *LoggerCollection) SetTextOutput(re *regexp.Regexp,
	output TextOutput) {
	c.mtx.Lock()
	defer c.mtx.Unlock()

	if re == nil {
		c.handler.SetTextOutput(output)
	}
	for name, logger := range c.loggers {
		if re == nil || re.MatchString(name) {
			logger.getHandler().SetTextOutput(output)
		}
	}
}

var (
	// It's unlikely you'll need to use this directly
	DefaultLoggerCollection = NewLoggerCollection()
)

// GetLogger returns an automatically-named logger on the default logger
// collection.
func GetLogger() *Logger {
	return DefaultLoggerCollection.GetLoggerNamed(callerName())
}

// GetLoggerNamed returns a new Logger with the provided name on the default
// logger collection. GetLogger is more frequently used.
func GetLoggerNamed(name string) *Logger {
	return DefaultLoggerCollection.GetLoggerNamed(name)
}

// SetLevel will set the current log level for all loggers on the default
// collection with names that match a provided regular expression. If the
// regular expression is nil, then all loggers match.
func SetLevel(re *regexp.Regexp, level LogLevel) {
	DefaultLoggerCollection.SetLevel(re, level)
}

// SetHandler will set the current log handler for all loggers on the default
// collection with names that match a provided regular expression. If the
// regular expression is nil, then all loggers match.
func SetHandler(re *regexp.Regexp, handler Handler) {
	DefaultLoggerCollection.SetHandler(re, handler)
}

// SetTextTemplate will set the current text template for all loggers on the
// default collection with names that match a provided regular expression. If
// the regular expression is nil, then all loggers match. Note that not every
// handler is guaranteed to support text templates and a text template will
// only apply to text-oriented and unstructured handlers.
func SetTextTemplate(re *regexp.Regexp, t *template.Template) {
	DefaultLoggerCollection.SetTextTemplate(re, t)
}

// SetTextOutput will set the current output interface for all loggers on the
// default collection with names that match a provided regular expression. If
// the regular expression is nil, then all loggers match. Note that not every
// handler is guaranteed to support text output and a text output interface
// will only apply to text-oriented and unstructured handlers.
func SetTextOutput(re *regexp.Regexp, output TextOutput) {
	DefaultLoggerCollection.SetTextOutput(re, output)
}