Remove github.com/whyrusleeping/go-logging Godeps
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Jun 12, 2015 at 14:03 UTC
2e9ec3deb84c01428b4b513fd66a395399e1411d
22 files changed
-2041
Godeps/Godeps.json
-4
@@ -227,10 +227,6 @@
227
"ImportPath": "github.com/syndtr/gosnappy/snappy",
228
"Rev": "156a073208e131d7d2e212cb749feae7c339e846"
229
},
230
- {
231
- "ImportPath": "github.com/whyrusleeping/go-logging",
232
- "Rev": "128b9855511a4ea3ccbcf712695baf2bab72e134"
233
- },
230
{
231
"ImportPath": "github.com/whyrusleeping/go-metrics",
232
"Rev": "1cd8009604ec2238b5a71305a0ecd974066e0e16"
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/.travis.yml
deleted
-6
@@ -1,6 +0,0 @@
1
-language: go
2
-
3
-go:
4
- - 1.0
5
- - 1.1
6
- - tip
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/CONTRIBUTORS
deleted
-5
@@ -1,5 +0,0 @@
1
-Alec Thomas <alec@swapoff.org>
2
-Guilhem Lettron <guilhem.lettron@optiflows.com>
3
-Ivan Daniluk <ivan.daniluk@gmail.com>
4
-Nimi Wariboko Jr <nimi@channelmeter.com>
5
-Róbert Selvek <robert.selvek@gmail.com>
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/LICENSE
deleted
-27
@@ -1,27 +0,0 @@
1
-Copyright (c) 2013 Örjan Persson. All rights reserved.
2
-
3
-Redistribution and use in source and binary forms, with or without
4
-modification, are permitted provided that the following conditions are
5
-met:
6
-
7
- * Redistributions of source code must retain the above copyright
8
-notice, this list of conditions and the following disclaimer.
9
- * Redistributions in binary form must reproduce the above
10
-copyright notice, this list of conditions and the following disclaimer
11
-in the documentation and/or other materials provided with the
12
-distribution.
13
- * Neither the name of Google Inc. nor the names of its
14
-contributors may be used to endorse or promote products derived from
15
-this software without specific prior written permission.
16
-
17
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/README.md
deleted
-89
@@ -1,89 +0,0 @@
1
-## Golang logging library
2
-
3
-[](https://godoc.org/github.com/op/go-logging) [](https://travis-ci.org/op/go-logging)
4
-
5
-Package logging implements a logging infrastructure for Go. Its output format
6
-is customizable and supports different logging backends like syslog, file and
7
-memory. Multiple backends can be utilized with different log levels per backend
8
-and logger.
9
-
10
-## Example
11
-
12
-Let's have a look at an [example](examples/example.go) which demonstrates most
13
-of the features found in this library.
14
-
15
-[](examples/example.go)
16
-
17
-```go
18
-package main
19
-
20
-import (
21
- "os"
22
-
23
- "github.com/op/go-logging"
24
-)
25
-
26
-var log = logging.MustGetLogger("example")
27
-
28
-// Example format string. Everything except the message has a custom color
29
-// which is dependent on the log level. Many fields have a custom output
30
-// formatting too, eg. the time returns the hour down to the milli second.
31
-var format = logging.MustStringFormatter(
32
- "%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
33
-)
34
-
35
-// Password is just an example type implementing the Redactor interface. Any
36
-// time this is logged, the Redacted() function will be called.
37
-type Password string
38
-
39
-func (p Password) Redacted() interface{} {
40
- return logging.Redact(string(p))
41
-}
42
-
43
-func main() {
44
- // For demo purposes, create two backend for os.Stderr.
45
- backend1 := logging.NewLogBackend(os.Stderr, "", 0)
46
- backend2 := logging.NewLogBackend(os.Stderr, "", 0)
47
-
48
- // For messages written to backend2 we want to add some additional
49
- // information to the output, including the used log level and the name of
50
- // the function.
51
- backend2Formatter := logging.NewBackendFormatter(backend2, format)
52
-
53
- // Only errors and more severe messages should be sent to backend1
54
- backend1Leveled := logging.AddModuleLevel(backend1)
55
- backend1Leveled.SetLevel(logging.ERROR, "")
56
-
57
- // Set the backends to be used.
58
- logging.SetBackend(backend1Leveled, backend2Formatter)
59
-
60
- log.Debug("debug %s", Password("secret"))
61
- log.Info("info")
62
- log.Notice("notice")
63
- log.Warning("warning")
64
- log.Error("err")
65
- log.Critical("crit")
66
-}
67
-```
68
-
69
-## Installing
70
-
71
-### Using *go get*
72
-
73
- $ go get github.com/op/go-logging
74
-
75
-After this command *go-logging* is ready to use. Its source will be in:
76
-
77
- $GOROOT/src/pkg/github.com/op/go-logging
78
-
79
-You can use `go get -u` to update the package.
80
-
81
-## Documentation
82
-
83
-For docs, see http://godoc.org/github.com/op/go-logging or run:
84
-
85
- $ godoc github.com/op/go-logging
86
-
87
-## Additional resources
88
-
89
-* [wslog](https://godoc.org/github.com/cryptix/go/logging/wslog) -- exposes log messages through a WebSocket.
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/backend.go
deleted
-39
@@ -1,39 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-// defaultBackend is the backend used for all logging calls.
8
-var defaultBackend LeveledBackend
9
-
10
-// Backend is the interface which a log backend need to implement to be able to
11
-// be used as a logging backend.
12
-type Backend interface {
13
- Log(Level, int, *Record) error
14
-}
15
-
16
-// Set backend replaces the backend currently set with the given new logging
17
-// backend.
18
-func SetBackend(backends ...Backend) LeveledBackend {
19
- var backend Backend
20
- if len(backends) == 1 {
21
- backend = backends[0]
22
- } else {
23
- backend = MultiLogger(backends...)
24
- }
25
-
26
- defaultBackend = AddModuleLevel(backend)
27
- return defaultBackend
28
-}
29
-
30
-// SetLevel sets the logging level for the specified module. The module
31
-// corresponds to the string specified in GetLogger.
32
-func SetLevel(level Level, module string) {
33
- defaultBackend.SetLevel(level, module)
34
-}
35
-
36
-// GetLevel returns the logging level for the specified module.
37
-func GetLevel(module string) Level {
38
- return defaultBackend.GetLevel(module)
39
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/example_test.go
deleted
-40
@@ -1,40 +0,0 @@
1
-package logging
2
-
3
-import "os"
4
-
5
-func Example() {
6
- // This call is for testing purposes and will set the time to unix epoch.
7
- InitForTesting(DEBUG)
8
-
9
- var log = MustGetLogger("example")
10
-
11
- // For demo purposes, create two backend for os.Stdout.
12
- //
13
- // os.Stderr should most likely be used in the real world but then the
14
- // "Output:" check in this example would not work.
15
- backend1 := NewLogBackend(os.Stdout, "", 0)
16
- backend2 := NewLogBackend(os.Stdout, "", 0)
17
-
18
- // For messages written to backend2 we want to add some additional
19
- // information to the output, including the used log level and the name of
20
- // the function.
21
- var format = MustStringFormatter(
22
- "%{time:15:04:05.000} %{shortfunc} %{level:.1s} %{message}",
23
- )
24
- backend2Formatter := NewBackendFormatter(backend2, format)
25
-
26
- // Only errors and more severe messages should be sent to backend2
27
- backend2Leveled := AddModuleLevel(backend2Formatter)
28
- backend2Leveled.SetLevel(ERROR, "")
29
-
30
- // Set the backends to be used and the default level.
31
- SetBackend(backend1, backend2Leveled)
32
-
33
- log.Debug("debug %s", "arg")
34
- log.Error("error")
35
-
36
- // Output:
37
- // debug arg
38
- // error
39
- // 00:00:00.000 Example E error
40
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/examples/example.go
deleted
-49
@@ -1,49 +0,0 @@
1
-package main
2
-
3
-import (
4
- "os"
5
-
6
- "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/go-logging"
7
-)
8
-
9
-var log = logging.MustGetLogger("example")
10
-
11
-// Example format string. Everything except the message has a custom color
12
-// which is dependent on the log level. Many fields have a custom output
13
-// formatting too, eg. the time returns the hour down to the milli second.
14
-var format = logging.MustStringFormatter(
15
- "%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}",
16
-)
17
-
18
-// Password is just an example type implementing the Redactor interface. Any
19
-// time this is logged, the Redacted() function will be called.
20
-type Password string
21
-
22
-func (p Password) Redacted() interface{} {
23
- return logging.Redact(string(p))
24
-}
25
-
26
-func main() {
27
- // For demo purposes, create two backend for os.Stderr.
28
- backend1 := logging.NewLogBackend(os.Stderr, "", 0)
29
- backend2 := logging.NewLogBackend(os.Stderr, "", 0)
30
-
31
- // For messages written to backend2 we want to add some additional
32
- // information to the output, including the used log level and the name of
33
- // the function.
34
- backend2Formatter := logging.NewBackendFormatter(backend2, format)
35
-
36
- // Only errors and more severe messages should be sent to backend1
37
- backend1Leveled := logging.AddModuleLevel(backend1)
38
- backend1Leveled.SetLevel(logging.ERROR, "")
39
-
40
- // Set the backends to be used.
41
- logging.SetBackend(backend1Leveled, backend2Formatter)
42
-
43
- log.Debug("debug %s", Password("secret"))
44
- log.Info("info")
45
- log.Notice("notice")
46
- log.Warning("warning")
47
- log.Error("err")
48
- log.Critical("crit")
49
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/examples/example.png
Binary files a/Godeps/_workspace/src/github.com/whyrusleeping/go-logging/examples/example.png and /dev/null differ
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/format.go
deleted
-368
@@ -1,368 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "bytes"
9
- "errors"
10
- "fmt"
11
- "io"
12
- "os"
13
- "path"
14
- "path/filepath"
15
- "regexp"
16
- "runtime"
17
- "strings"
18
- "sync"
19
- "time"
20
-)
21
-
22
-// TODO see Formatter interface in fmt/print.go
23
-// TODO try text/template, maybe it have enough performance
24
-// TODO other template systems?
25
-// TODO make it possible to specify formats per backend?
26
-type fmtVerb int
27
-
28
-const (
29
- fmtVerbTime fmtVerb = iota
30
- fmtVerbLevel
31
- fmtVerbId
32
- fmtVerbPid
33
- fmtVerbProgram
34
- fmtVerbModule
35
- fmtVerbMessage
36
- fmtVerbLongfile
37
- fmtVerbShortfile
38
- fmtVerbLongpkg
39
- fmtVerbShortpkg
40
- fmtVerbLongfunc
41
- fmtVerbShortfunc
42
- fmtVerbLevelColor
43
-
44
- // Keep last, there are no match for these below.
45
- fmtVerbUnknown
46
- fmtVerbStatic
47
-)
48
-
49
-var fmtVerbs = []string{
50
- "time",
51
- "level",
52
- "id",
53
- "pid",
54
- "program",
55
- "module",
56
- "message",
57
- "longfile",
58
- "shortfile",
59
- "longpkg",
60
- "shortpkg",
61
- "longfunc",
62
- "shortfunc",
63
- "color",
64
-}
65
-
66
-const rfc3339Milli = "2006-01-02T15:04:05.999Z07:00"
67
-
68
-var defaultVerbsLayout = []string{
69
- rfc3339Milli,
70
- "s",
71
- "d",
72
- "d",
73
- "s",
74
- "s",
75
- "s",
76
- "s",
77
- "s",
78
- "s",
79
- "s",
80
- "s",
81
- "s",
82
- "",
83
-}
84
-
85
-var (
86
- pid = os.Getpid()
87
- program = filepath.Base(os.Args[0])
88
-)
89
-
90
-func getFmtVerbByName(name string) fmtVerb {
91
- for i, verb := range fmtVerbs {
92
- if name == verb {
93
- return fmtVerb(i)
94
- }
95
- }
96
- return fmtVerbUnknown
97
-}
98
-
99
-// Formatter is the required interface for a custom log record formatter.
100
-type Formatter interface {
101
- Format(calldepth int, r *Record, w io.Writer) error
102
-}
103
-
104
-// formatter is used by all backends unless otherwise overriden.
105
-var formatter struct {
106
- sync.RWMutex
107
- def Formatter
108
-}
109
-
110
-func getFormatter() Formatter {
111
- formatter.RLock()
112
- defer formatter.RUnlock()
113
- return formatter.def
114
-}
115
-
116
-var (
117
- // DefaultFormatter is the default formatter used and is only the message.
118
- DefaultFormatter Formatter = MustStringFormatter("%{message}")
119
-
120
- // Glog format
121
- GlogFormatter Formatter = MustStringFormatter("%{level:.1s}%{time:0102 15:04:05.999999} %{pid} %{shortfile}] %{message}")
122
-)
123
-
124
-// SetFormatter sets the default formatter for all new backends. A backend will
125
-// fetch this value once it is needed to format a record. Note that backends
126
-// will cache the formatter after the first point. For now, make sure to set
127
-// the formatter before logging.
128
-func SetFormatter(f Formatter) {
129
- formatter.Lock()
130
- defer formatter.Unlock()
131
- formatter.def = f
132
-}
133
-
134
-var formatRe *regexp.Regexp = regexp.MustCompile(`%{([a-z]+)(?::(.*?[^\\]))?}`)
135
-
136
-type part struct {
137
- verb fmtVerb
138
- layout string
139
-}
140
-
141
-// stringFormatter contains a list of parts which explains how to build the
142
-// formatted string passed on to the logging backend.
143
-type stringFormatter struct {
144
- parts []part
145
-}
146
-
147
-// NewStringFormatter returns a new Formatter which outputs the log record as a
148
-// string based on the 'verbs' specified in the format string.
149
-//
150
-// The verbs:
151
-//
152
-// General:
153
-// %{id} Sequence number for log message (uint64).
154
-// %{pid} Process id (int)
155
-// %{time} Time when log occurred (time.Time)
156
-// %{level} Log level (Level)
157
-// %{module} Module (string)
158
-// %{program} Basename of os.Args[0] (string)
159
-// %{message} Message (string)
160
-// %{longfile} Full file name and line number: /a/b/c/d.go:23
161
-// %{shortfile} Final file name element and line number: d.go:23
162
-// %{color} ANSI color based on log level
163
-//
164
-// For normal types, the output can be customized by using the 'verbs' defined
165
-// in the fmt package, eg. '%{id:04d}' to make the id output be '%04d' as the
166
-// format string.
167
-//
168
-// For time.Time, use the same layout as time.Format to change the time format
169
-// when output, eg "2006-01-02T15:04:05.999Z-07:00".
170
-//
171
-// For the 'color' verb, the output can be adjusted to either use bold colors,
172
-// i.e., '%{color:bold}' or to reset the ANSI attributes, i.e.,
173
-// '%{color:reset}' Note that if you use the color verb explicitly, be sure to
174
-// reset it or else the color state will persist past your log message. e.g.,
175
-// "%{color:bold}%{time:15:04:05} %{level:-8s}%{color:reset} %{message}" will
176
-// just colorize the time and level, leaving the message uncolored.
177
-//
178
-// There's also a couple of experimental 'verbs'. These are exposed to get
179
-// feedback and needs a bit of tinkering. Hence, they might change in the
180
-// future.
181
-//
182
-// Experimental:
183
-// %{longpkg} Full package path, eg. github.com/go-logging
184
-// %{shortpkg} Base package path, eg. go-logging
185
-// %{longfunc} Full function name, eg. littleEndian.PutUint32
186
-// %{shortfunc} Base function name, eg. PutUint32
187
-func NewStringFormatter(format string) (*stringFormatter, error) {
188
- var fmter = &stringFormatter{}
189
-
190
- // Find the boundaries of all %{vars}
191
- matches := formatRe.FindAllStringSubmatchIndex(format, -1)
192
- if matches == nil {
193
- return nil, errors.New("logger: invalid log format: " + format)
194
- }
195
-
196
- // Collect all variables and static text for the format
197
- prev := 0
198
- for _, m := range matches {
199
- start, end := m[0], m[1]
200
- if start > prev {
201
- fmter.add(fmtVerbStatic, format[prev:start])
202
- }
203
-
204
- name := format[m[2]:m[3]]
205
- verb := getFmtVerbByName(name)
206
- if verb == fmtVerbUnknown {
207
- return nil, errors.New("logger: unknown variable: " + name)
208
- }
209
-
210
- // Handle layout customizations or use the default. If this is not for the
211
- // time or color formatting, we need to prefix with %.
212
- layout := defaultVerbsLayout[verb]
213
- if m[4] != -1 {
214
- layout = format[m[4]:m[5]]
215
- }
216
- if verb != fmtVerbTime && verb != fmtVerbLevelColor {
217
- layout = "%" + layout
218
- }
219
-
220
- fmter.add(verb, layout)
221
- prev = end
222
- }
223
- end := format[prev:]
224
- if end != "" {
225
- fmter.add(fmtVerbStatic, end)
226
- }
227
-
228
- // Make a test run to make sure we can format it correctly.
229
- t, err := time.Parse(time.RFC3339, "2010-02-04T21:00:57-08:00")
230
- if err != nil {
231
- panic(err)
232
- }
233
- r := &Record{
234
- Id: 12345,
235
- Time: t,
236
- Module: "logger",
237
- fmt: "hello %s",
238
- args: []interface{}{"go"},
239
- }
240
- if err := fmter.Format(0, r, &bytes.Buffer{}); err != nil {
241
- return nil, err
242
- }
243
-
244
- return fmter, nil
245
-}
246
-
247
-// MustStringFormatter is equivalent to NewStringFormatter with a call to panic
248
-// on error.
249
-func MustStringFormatter(format string) *stringFormatter {
250
- f, err := NewStringFormatter(format)
251
- if err != nil {
252
- panic("Failed to initialized string formatter: " + err.Error())
253
- }
254
- return f
255
-}
256
-
257
-func (f *stringFormatter) add(verb fmtVerb, layout string) {
258
- f.parts = append(f.parts, part{verb, layout})
259
-}
260
-
261
-func (f *stringFormatter) Format(calldepth int, r *Record, output io.Writer) error {
262
- for _, part := range f.parts {
263
- if part.verb == fmtVerbStatic {
264
- output.Write([]byte(part.layout))
265
- } else if part.verb == fmtVerbTime {
266
- output.Write([]byte(r.Time.Format(part.layout)))
267
- } else if part.verb == fmtVerbLevelColor {
268
- if part.layout == "bold" {
269
- output.Write([]byte(boldcolors[r.Level]))
270
- } else if part.layout == "reset" {
271
- output.Write([]byte("\033[0m"))
272
- } else {
273
- output.Write([]byte(colors[r.Level]))
274
- }
275
- } else {
276
- var v interface{}
277
- switch part.verb {
278
- case fmtVerbLevel:
279
- v = r.Level
280
- break
281
- case fmtVerbId:
282
- v = r.Id
283
- break
284
- case fmtVerbPid:
285
- v = pid
286
- break
287
- case fmtVerbProgram:
288
- v = program
289
- break
290
- case fmtVerbModule:
291
- v = r.Module
292
- break
293
- case fmtVerbMessage:
294
- v = r.Message()
295
- break
296
- case fmtVerbLongfile, fmtVerbShortfile:
297
- _, file, line, ok := runtime.Caller(calldepth + 1)
298
- if !ok {
299
- file = "???"
300
- line = 0
301
- } else if part.verb == fmtVerbShortfile {
302
- file = filepath.Base(file)
303
- }
304
- v = fmt.Sprintf("%s:%d", file, line)
305
- case fmtVerbLongfunc, fmtVerbShortfunc,
306
- fmtVerbLongpkg, fmtVerbShortpkg:
307
- // TODO cache pc
308
- v = "???"
309
- if pc, _, _, ok := runtime.Caller(calldepth + 1); ok {
310
- if f := runtime.FuncForPC(pc); f != nil {
311
- v = formatFuncName(part.verb, f.Name())
312
- }
313
- }
314
- default:
315
- panic("unhandled format part")
316
- }
317
- fmt.Fprintf(output, part.layout, v)
318
- }
319
- }
320
- return nil
321
-}
322
-
323
-// formatFuncName tries to extract certain part of the runtime formatted
324
-// function name to some pre-defined variation.
325
-//
326
-// This function is known to not work properly if the package path or name
327
-// contains a dot.
328
-func formatFuncName(v fmtVerb, f string) string {
329
- i := strings.LastIndex(f, "/")
330
- j := strings.Index(f[i+1:], ".")
331
- if j < 1 {
332
- return "???"
333
- }
334
- pkg, fun := f[:i+j+1], f[i+j+2:]
335
- switch v {
336
- case fmtVerbLongpkg:
337
- return pkg
338
- case fmtVerbShortpkg:
339
- return path.Base(pkg)
340
- case fmtVerbLongfunc:
341
- return fun
342
- case fmtVerbShortfunc:
343
- i = strings.LastIndex(fun, ".")
344
- return fun[i+1:]
345
- }
346
- panic("unexpected func formatter")
347
-}
348
-
349
-// backendFormatter combines a backend with a specific formatter making it
350
-// possible to have different log formats for different backends.
351
-type backendFormatter struct {
352
- b Backend
353
- f Formatter
354
-}
355
-
356
-// NewBackendFormatter creates a new backend which makes all records that
357
-// passes through it beeing formatted by the specific formatter.
358
-func NewBackendFormatter(b Backend, f Formatter) *backendFormatter {
359
- return &backendFormatter{b, f}
360
-}
361
-
362
-// Log implements the Log function required by the Backend interface.
363
-func (bf *backendFormatter) Log(level Level, calldepth int, r *Record) error {
364
- // Make a shallow copy of the record and replace any formatter
365
- r2 := *r
366
- r2.formatter = bf.f
367
- return bf.b.Log(level, calldepth+1, &r2)
368
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/format_test.go
deleted
-184
@@ -1,184 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "bytes"
9
- "testing"
10
-)
11
-
12
-func TestFormat(t *testing.T) {
13
- backend := InitForTesting(DEBUG)
14
-
15
- f, err := NewStringFormatter("%{shortfile} %{time:2006-01-02T15:04:05} %{level:.1s} %{id:04d} %{module} %{message}")
16
- if err != nil {
17
- t.Fatalf("failed to set format: %s", err)
18
- }
19
- SetFormatter(f)
20
-
21
- log := MustGetLogger("module")
22
- log.Debug("hello")
23
-
24
- line := MemoryRecordN(backend, 0).Formatted(0)
25
- if "format_test.go:24 1970-01-01T00:00:00 D 0001 module hello" != line {
26
- t.Errorf("Unexpected format: %s", line)
27
- }
28
-}
29
-
30
-func logAndGetLine(backend *MemoryBackend) string {
31
- MustGetLogger("foo").Debug("hello")
32
- return MemoryRecordN(backend, 0).Formatted(1)
33
-}
34
-
35
-func getLastLine(backend *MemoryBackend) string {
36
- return MemoryRecordN(backend, 0).Formatted(1)
37
-}
38
-
39
-func realFunc(backend *MemoryBackend) string {
40
- return logAndGetLine(backend)
41
-}
42
-
43
-type structFunc struct{}
44
-
45
-func (structFunc) Log(backend *MemoryBackend) string {
46
- return logAndGetLine(backend)
47
-}
48
-
49
-func TestRealFuncFormat(t *testing.T) {
50
- backend := InitForTesting(DEBUG)
51
- SetFormatter(MustStringFormatter("%{shortfunc}"))
52
-
53
- line := realFunc(backend)
54
- if "realFunc" != line {
55
- t.Errorf("Unexpected format: %s", line)
56
- }
57
-}
58
-
59
-func TestStructFuncFormat(t *testing.T) {
60
- backend := InitForTesting(DEBUG)
61
- SetFormatter(MustStringFormatter("%{longfunc}"))
62
-
63
- var x structFunc
64
- line := x.Log(backend)
65
- if "structFunc.Log" != line {
66
- t.Errorf("Unexpected format: %s", line)
67
- }
68
-}
69
-
70
-func TestVarFuncFormat(t *testing.T) {
71
- backend := InitForTesting(DEBUG)
72
- SetFormatter(MustStringFormatter("%{shortfunc}"))
73
-
74
- var varFunc = func() string {
75
- return logAndGetLine(backend)
76
- }
77
-
78
- line := varFunc()
79
- if "???" == line || "TestVarFuncFormat" == line || "varFunc" == line {
80
- t.Errorf("Unexpected format: %s", line)
81
- }
82
-}
83
-
84
-func TestFormatFuncName(t *testing.T) {
85
- var tests = []struct {
86
- filename string
87
- longpkg string
88
- shortpkg string
89
- longfunc string
90
- shortfunc string
91
- }{
92
- {"",
93
- "???",
94
- "???",
95
- "???",
96
- "???"},
97
- {"main",
98
- "???",
99
- "???",
100
- "???",
101
- "???"},
102
- {"main.",
103
- "main",
104
- "main",
105
- "",
106
- ""},
107
- {"main.main",
108
- "main",
109
- "main",
110
- "main",
111
- "main"},
112
- {"github.com/op/go-logging.func·001",
113
- "github.com/op/go-logging",
114
- "go-logging",
115
- "func·001",
116
- "func·001"},
117
- {"github.com/op/go-logging.stringFormatter.Format",
118
- "github.com/op/go-logging",
119
- "go-logging",
120
- "stringFormatter.Format",
121
- "Format"},
122
- }
123
-
124
- var v string
125
- for _, test := range tests {
126
- v = formatFuncName(fmtVerbLongpkg, test.filename)
127
- if test.longpkg != v {
128
- t.Errorf("%s != %s", test.longpkg, v)
129
- }
130
- v = formatFuncName(fmtVerbShortpkg, test.filename)
131
- if test.shortpkg != v {
132
- t.Errorf("%s != %s", test.shortpkg, v)
133
- }
134
- v = formatFuncName(fmtVerbLongfunc, test.filename)
135
- if test.longfunc != v {
136
- t.Errorf("%s != %s", test.longfunc, v)
137
- }
138
- v = formatFuncName(fmtVerbShortfunc, test.filename)
139
- if test.shortfunc != v {
140
- t.Errorf("%s != %s", test.shortfunc, v)
141
- }
142
- }
143
-}
144
-
145
-func TestBackendFormatter(t *testing.T) {
146
- InitForTesting(DEBUG)
147
-
148
- // Create two backends and wrap one of the with a backend formatter
149
- b1 := NewMemoryBackend(1)
150
- b2 := NewMemoryBackend(1)
151
-
152
- f := MustStringFormatter("%{level} %{message}")
153
- bf := NewBackendFormatter(b2, f)
154
-
155
- SetBackend(b1, bf)
156
-
157
- log := MustGetLogger("module")
158
- log.Info("foo")
159
- if "foo" != getLastLine(b1) {
160
- t.Errorf("Unexpected line: %s", getLastLine(b1))
161
- }
162
- if "INFO foo" != getLastLine(b2) {
163
- t.Errorf("Unexpected line: %s", getLastLine(b2))
164
- }
165
-}
166
-
167
-func BenchmarkStringFormatter(b *testing.B) {
168
- fmt := "%{time:2006-01-02T15:04:05} %{level:.1s} %{id:04d} %{module} %{message}"
169
- f := MustStringFormatter(fmt)
170
-
171
- backend := InitForTesting(DEBUG)
172
- buf := &bytes.Buffer{}
173
- log := MustGetLogger("module")
174
- log.Debug("")
175
- record := MemoryRecordN(backend, 0)
176
-
177
- b.ResetTimer()
178
- for i := 0; i < b.N; i++ {
179
- if err := f.Format(1, record, buf); err != nil {
180
- b.Fatal(err)
181
- buf.Truncate(0)
182
- }
183
- }
184
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/level.go
deleted
-124
@@ -1,124 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "errors"
9
- "strings"
10
- "sync"
11
-)
12
-
13
-var ErrInvalidLogLevel = errors.New("logger: invalid log level")
14
-
15
-// Level defines all available log levels for log messages.
16
-type Level int
17
-
18
-const (
19
- CRITICAL Level = iota
20
- ERROR
21
- WARNING
22
- NOTICE
23
- INFO
24
- DEBUG
25
-)
26
-
27
-var levelNames = []string{
28
- "CRITICAL",
29
- "ERROR",
30
- "WARNING",
31
- "NOTICE",
32
- "INFO",
33
- "DEBUG",
34
-}
35
-
36
-// String returns the string representation of a logging level.
37
-func (p Level) String() string {
38
- return levelNames[p]
39
-}
40
-
41
-// LogLevel returns the log level from a string representation.
42
-func LogLevel(level string) (Level, error) {
43
- for i, name := range levelNames {
44
- if strings.EqualFold(name, level) {
45
- return Level(i), nil
46
- }
47
- }
48
- return ERROR, ErrInvalidLogLevel
49
-}
50
-
51
-type Leveled interface {
52
- GetLevel(string) Level
53
- SetLevel(Level, string)
54
- IsEnabledFor(Level, string) bool
55
-}
56
-
57
-// LeveledBackend is a log backend with additional knobs for setting levels on
58
-// individual modules to different levels.
59
-type LeveledBackend interface {
60
- Backend
61
- Leveled
62
-}
63
-
64
-type moduleLeveled struct {
65
- levels map[string]Level
66
- backend Backend
67
- formatter Formatter
68
- once sync.Once
69
-}
70
-
71
-// AddModuleLevel wraps a log backend with knobs to have different log levels
72
-// for different modules.
73
-func AddModuleLevel(backend Backend) LeveledBackend {
74
- var leveled LeveledBackend
75
- var ok bool
76
- if leveled, ok = backend.(LeveledBackend); !ok {
77
- leveled = &moduleLeveled{
78
- levels: make(map[string]Level),
79
- backend: backend,
80
- }
81
- }
82
- return leveled
83
-}
84
-
85
-// GetLevel returns the log level for the given module.
86
-func (l *moduleLeveled) GetLevel(module string) Level {
87
- level, exists := l.levels[module]
88
- if exists == false {
89
- level, exists = l.levels[""]
90
- // no configuration exists, default to debug
91
- if exists == false {
92
- level = DEBUG
93
- }
94
- }
95
- return level
96
-}
97
-
98
-// SetLevel sets the log level for the given module.
99
-func (l *moduleLeveled) SetLevel(level Level, module string) {
100
- l.levels[module] = level
101
-}
102
-
103
-// IsEnabledFor will return true if logging is enabled for the given module.
104
-func (l *moduleLeveled) IsEnabledFor(level Level, module string) bool {
105
- return level <= l.GetLevel(module)
106
-}
107
-
108
-func (l *moduleLeveled) Log(level Level, calldepth int, rec *Record) (err error) {
109
- if l.IsEnabledFor(level, rec.Module) {
110
- // TODO get rid of traces of formatter here. BackendFormatter should be used.
111
- rec.formatter = l.getFormatterAndCacheCurrent()
112
- err = l.backend.Log(level, calldepth+1, rec)
113
- }
114
- return
115
-}
116
-
117
-func (l *moduleLeveled) getFormatterAndCacheCurrent() Formatter {
118
- l.once.Do(func() {
119
- if l.formatter == nil {
120
- l.formatter = getFormatter()
121
- }
122
- })
123
- return l.formatter
124
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/level_test.go
deleted
-76
@@ -1,76 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import "testing"
8
-
9
-func TestLevelString(t *testing.T) {
10
- // Make sure all levels can be converted from string -> constant -> string
11
- for _, name := range levelNames {
12
- level, err := LogLevel(name)
13
- if err != nil {
14
- t.Errorf("failed to get level: %v", err)
15
- continue
16
- }
17
-
18
- if level.String() != name {
19
- t.Errorf("invalid level conversion: %v != %v", level, name)
20
- }
21
- }
22
-}
23
-
24
-func TestLevelLogLevel(t *testing.T) {
25
- tests := []struct {
26
- expected Level
27
- level string
28
- }{
29
- {-1, "bla"},
30
- {INFO, "iNfO"},
31
- {ERROR, "error"},
32
- {WARNING, "warninG"},
33
- }
34
-
35
- for _, test := range tests {
36
- level, err := LogLevel(test.level)
37
- if err != nil {
38
- if test.expected == -1 {
39
- continue
40
- } else {
41
- t.Errorf("failed to convert %s: %s", test.level, err)
42
- }
43
- }
44
- if test.expected != level {
45
- t.Errorf("failed to convert %s to level: %s != %s", test.level, test.expected, level)
46
- }
47
- }
48
-}
49
-
50
-func TestLevelModuleLevel(t *testing.T) {
51
- backend := NewMemoryBackend(128)
52
-
53
- leveled := AddModuleLevel(backend)
54
- leveled.SetLevel(NOTICE, "")
55
- leveled.SetLevel(ERROR, "foo")
56
- leveled.SetLevel(INFO, "foo.bar")
57
- leveled.SetLevel(WARNING, "bar")
58
-
59
- expected := []struct {
60
- level Level
61
- module string
62
- }{
63
- {NOTICE, ""},
64
- {NOTICE, "something"},
65
- {ERROR, "foo"},
66
- {INFO, "foo.bar"},
67
- {WARNING, "bar"},
68
- }
69
-
70
- for _, e := range expected {
71
- actual := leveled.GetLevel(e.module)
72
- if e.level != actual {
73
- t.Errorf("unexpected level in %s: %s != %s", e.module, e.level, actual)
74
- }
75
- }
76
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/log.go
deleted
-80
@@ -1,80 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "bytes"
9
- "fmt"
10
- "io"
11
- "log"
12
-)
13
-
14
-// TODO initialize here
15
-var colors []string
16
-var boldcolors []string
17
-
18
-type color int
19
-
20
-const (
21
- colorBlack = (iota + 30)
22
- colorRed
23
- colorGreen
24
- colorYellow
25
- colorBlue
26
- colorMagenta
27
- colorCyan
28
- colorWhite
29
-)
30
-
31
-// LogBackend utilizes the standard log module.
32
-type LogBackend struct {
33
- Logger *log.Logger
34
- Color bool
35
-}
36
-
37
-// NewLogBackend creates a new LogBackend.
38
-func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend {
39
- return &LogBackend{Logger: log.New(out, prefix, flag)}
40
-}
41
-
42
-func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
43
- if b.Color {
44
- buf := &bytes.Buffer{}
45
- buf.Write([]byte(colors[level]))
46
- buf.Write([]byte(rec.Formatted(calldepth + 1)))
47
- buf.Write([]byte("\033[0m"))
48
- // For some reason, the Go logger arbitrarily decided "2" was the correct
49
- // call depth...
50
- return b.Logger.Output(calldepth+2, buf.String())
51
- } else {
52
- return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1))
53
- }
54
- panic("should not be reached")
55
-}
56
-
57
-func colorSeq(color color) string {
58
- return fmt.Sprintf("\033[%dm", int(color))
59
-}
60
-
61
-func colorSeqBold(color color) string {
62
- return fmt.Sprintf("\033[%d;1m", int(color))
63
-}
64
-
65
-func init() {
66
- colors = []string{
67
- CRITICAL: colorSeq(colorMagenta),
68
- ERROR: colorSeq(colorRed),
69
- WARNING: colorSeq(colorYellow),
70
- NOTICE: colorSeq(colorGreen),
71
- DEBUG: colorSeq(colorCyan),
72
- }
73
- boldcolors = []string{
74
- CRITICAL: colorSeqBold(colorMagenta),
75
- ERROR: colorSeqBold(colorRed),
76
- WARNING: colorSeqBold(colorYellow),
77
- NOTICE: colorSeqBold(colorGreen),
78
- DEBUG: colorSeqBold(colorCyan),
79
- }
80
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/log_test.go
deleted
-118
@@ -1,118 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "bytes"
9
- "io/ioutil"
10
- "log"
11
- "strings"
12
- "testing"
13
-)
14
-
15
-func TestLogCalldepth(t *testing.T) {
16
- buf := &bytes.Buffer{}
17
- SetBackend(NewLogBackend(buf, "", log.Lshortfile))
18
- SetFormatter(MustStringFormatter("%{shortfile} %{level} %{message}"))
19
-
20
- log := MustGetLogger("test")
21
- log.Info("test filename")
22
-
23
- parts := strings.SplitN(buf.String(), " ", 2)
24
-
25
- // Verify that the correct filename is registered by the stdlib logger
26
- if !strings.HasPrefix(parts[0], "log_test.go:") {
27
- t.Errorf("incorrect filename: %s", parts[0])
28
- }
29
- // Verify that the correct filename is registered by go-logging
30
- if !strings.HasPrefix(parts[1], "log_test.go:") {
31
- t.Errorf("incorrect filename: %s", parts[1])
32
- }
33
-}
34
-
35
-func BenchmarkLogMemoryBackendIgnored(b *testing.B) {
36
- backend := SetBackend(NewMemoryBackend(1024))
37
- backend.SetLevel(INFO, "")
38
- RunLogBenchmark(b)
39
-}
40
-
41
-func BenchmarkLogMemoryBackend(b *testing.B) {
42
- backend := SetBackend(NewMemoryBackend(1024))
43
- backend.SetLevel(DEBUG, "")
44
- RunLogBenchmark(b)
45
-}
46
-
47
-func BenchmarkLogChannelMemoryBackend(b *testing.B) {
48
- channelBackend := NewChannelMemoryBackend(1024)
49
- backend := SetBackend(channelBackend)
50
- backend.SetLevel(DEBUG, "")
51
- RunLogBenchmark(b)
52
- channelBackend.Flush()
53
-}
54
-
55
-func BenchmarkLogLeveled(b *testing.B) {
56
- backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
57
- backend.SetLevel(INFO, "")
58
-
59
- RunLogBenchmark(b)
60
-}
61
-
62
-func BenchmarkLogLogBackend(b *testing.B) {
63
- backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
64
- backend.SetLevel(DEBUG, "")
65
- RunLogBenchmark(b)
66
-}
67
-
68
-func BenchmarkLogLogBackendColor(b *testing.B) {
69
- colorizer := NewLogBackend(ioutil.Discard, "", 0)
70
- colorizer.Color = true
71
- backend := SetBackend(colorizer)
72
- backend.SetLevel(DEBUG, "")
73
- RunLogBenchmark(b)
74
-}
75
-
76
-func BenchmarkLogLogBackendStdFlags(b *testing.B) {
77
- backend := SetBackend(NewLogBackend(ioutil.Discard, "", log.LstdFlags))
78
- backend.SetLevel(DEBUG, "")
79
- RunLogBenchmark(b)
80
-}
81
-
82
-func BenchmarkLogLogBackendLongFileFlag(b *testing.B) {
83
- backend := SetBackend(NewLogBackend(ioutil.Discard, "", log.Llongfile))
84
- backend.SetLevel(DEBUG, "")
85
- RunLogBenchmark(b)
86
-}
87
-
88
-func RunLogBenchmark(b *testing.B) {
89
- password := Password("foo")
90
- log := MustGetLogger("test")
91
-
92
- b.ResetTimer()
93
- for i := 0; i < b.N; i++ {
94
- log.Debug("log line for %d and this is rectified: %s", i, password)
95
- }
96
-}
97
-
98
-func BenchmarkLogFixed(b *testing.B) {
99
- backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
100
- backend.SetLevel(DEBUG, "")
101
-
102
- RunLogBenchmarkFixedString(b)
103
-}
104
-
105
-func BenchmarkLogFixedIgnored(b *testing.B) {
106
- backend := SetBackend(NewLogBackend(ioutil.Discard, "", 0))
107
- backend.SetLevel(INFO, "")
108
- RunLogBenchmarkFixedString(b)
109
-}
110
-
111
-func RunLogBenchmarkFixedString(b *testing.B) {
112
- log := MustGetLogger("test")
113
-
114
- b.ResetTimer()
115
- for i := 0; i < b.N; i++ {
116
- log.Debug("some random fixed text")
117
- }
118
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/logger.go
deleted
-277
@@ -1,277 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-// Package logging implements a logging infrastructure for Go. It supports
6
-// different logging backends like syslog, file and memory. Multiple backends
7
-// can be utilized with different log levels per backend and logger.
8
-package logging
9
-
10
-import (
11
- "bytes"
12
- "fmt"
13
- "log"
14
- "os"
15
- "strings"
16
- "sync/atomic"
17
- "time"
18
-)
19
-
20
-// Redactor is an interface for types that may contain sensitive information
21
-// (like passwords), which shouldn't be printed to the log. The idea was found
22
-// in relog as part of the vitness project.
23
-type Redactor interface {
24
- Redacted() interface{}
25
-}
26
-
27
-// Redact returns a string of * having the same length as s.
28
-func Redact(s string) string {
29
- return strings.Repeat("*", len(s))
30
-}
31
-
32
-var (
33
- // Sequence number is incremented and utilized for all log records created.
34
- sequenceNo uint64
35
-
36
- // timeNow is a customizable for testing purposes.
37
- timeNow = time.Now
38
-)
39
-
40
-// Record represents a log record and contains the timestamp when the record
41
-// was created, an increasing id, filename and line and finally the actual
42
-// formatted log line.
43
-type Record struct {
44
- Id uint64
45
- Time time.Time
46
- Module string
47
- Level Level
48
-
49
- // message is kept as a pointer to have shallow copies update this once
50
- // needed.
51
- message *string
52
- args []interface{}
53
- fmt string
54
- formatter Formatter
55
- formatted string
56
-}
57
-
58
-// Formatted returns the string-formatted version of a record.
59
-func (r *Record) Formatted(calldepth int) string {
60
- if r.formatted == "" {
61
- var buf bytes.Buffer
62
- r.formatter.Format(calldepth+1, r, &buf)
63
- r.formatted = buf.String()
64
- }
65
- return r.formatted
66
-}
67
-
68
-// Message returns a string message for outputting. Redacts any record args
69
-// that implement the Redactor interface
70
-func (r *Record) Message() string {
71
- if r.message == nil {
72
- // Redact the arguments that implements the Redactor interface
73
- for i, arg := range r.args {
74
- if redactor, ok := arg.(Redactor); ok == true {
75
- r.args[i] = redactor.Redacted()
76
- }
77
- }
78
- msg := fmt.Sprintf(r.fmt, r.args...)
79
- r.message = &msg
80
- }
81
- return *r.message
82
-}
83
-
84
-// Logger is a logging unit. It controls the flow of messages to a given
85
-// (swappable) backend.
86
-type Logger struct {
87
- Module string
88
- backend LeveledBackend
89
- haveBackend bool
90
-
91
- // ExtraCallDepth can be used to add additional call depth when getting the
92
- // calling function. This is normally used when wrapping a logger.
93
- ExtraCalldepth int
94
-}
95
-
96
-// SetBackend changes the backend of the logger.
97
-func (l *Logger) SetBackend(backend LeveledBackend) {
98
- l.backend = backend
99
- l.haveBackend = true
100
-}
101
-
102
-// GetLogger creates and returns a Logger object based on the module name.
103
-// TODO call NewLogger and remove MustGetLogger?
104
-func GetLogger(module string) (*Logger, error) {
105
- return &Logger{Module: module}, nil
106
-}
107
-
108
-// MustGetLogger is like GetLogger but panics if the logger can't be created.
109
-// It simplifies safe initialization of a global logger for eg. a package.
110
-func MustGetLogger(module string) *Logger {
111
- logger, err := GetLogger(module)
112
- if err != nil {
113
- panic("logger: " + module + ": " + err.Error())
114
- }
115
- return logger
116
-}
117
-
118
-// Reset restores the internal state of the logging library.
119
-func Reset() {
120
- // TODO make a global Init() method to be less magic? or make it such that
121
- // if there's no backends at all configured, we could use some tricks to
122
- // automatically setup backends based if we have a TTY or not.
123
- sequenceNo = 0
124
- b := SetBackend(NewLogBackend(os.Stderr, "", log.LstdFlags))
125
- b.SetLevel(DEBUG, "")
126
- SetFormatter(DefaultFormatter)
127
- timeNow = time.Now
128
-}
129
-
130
-// InitForTesting is a convenient method when using logging in a test. Once
131
-// called, the time will be frozen to January 1, 1970 UTC.
132
-func InitForTesting(level Level) *MemoryBackend {
133
- Reset()
134
-
135
- memoryBackend := NewMemoryBackend(10240)
136
-
137
- leveledBackend := AddModuleLevel(memoryBackend)
138
- leveledBackend.SetLevel(level, "")
139
- SetBackend(leveledBackend)
140
-
141
- timeNow = func() time.Time {
142
- return time.Unix(0, 0).UTC()
143
- }
144
- return memoryBackend
145
-}
146
-
147
-// IsEnabledFor returns true if the logger is enabled for the given level.
148
-func (l *Logger) IsEnabledFor(level Level) bool {
149
- return defaultBackend.IsEnabledFor(level, l.Module)
150
-}
151
-
152
-func (l *Logger) log(lvl Level, format string, args ...interface{}) {
153
- if !l.IsEnabledFor(lvl) {
154
- return
155
- }
156
-
157
- // Create the logging record and pass it in to the backend
158
- record := &Record{
159
- Id: atomic.AddUint64(&sequenceNo, 1),
160
- Time: timeNow(),
161
- Module: l.Module,
162
- Level: lvl,
163
- fmt: format,
164
- args: args,
165
- }
166
-
167
- // TODO use channels to fan out the records to all backends?
168
- // TODO in case of errors, do something (tricky)
169
-
170
- // calldepth=2 brings the stack up to the caller of the level
171
- // methods, Info(), Fatal(), etc.
172
- // ExtraCallDepth allows this to be extended further up the stack in case we
173
- // are wrapping these methods, eg. to expose them package level
174
- if l.haveBackend {
175
- l.backend.Log(lvl, 2+l.ExtraCalldepth, record)
176
- return
177
- }
178
-
179
- defaultBackend.Log(lvl, 2+l.ExtraCalldepth, record)
180
-}
181
-
182
-// Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1).
183
-func (l *Logger) Fatal(args ...interface{}) {
184
- s := fmt.Sprint(args...)
185
- l.log(CRITICAL, "%s", s)
186
- os.Exit(1)
187
-}
188
-
189
-// Fatalf is equivalent to l.Critical followed by a call to os.Exit(1).
190
-func (l *Logger) Fatalf(format string, args ...interface{}) {
191
- l.log(CRITICAL, format, args...)
192
- os.Exit(1)
193
-}
194
-
195
-// Panic is equivalent to l.Critical(fmt.Sprint()) followed by a call to panic().
196
-func (l *Logger) Panic(args ...interface{}) {
197
- s := fmt.Sprint(args...)
198
- l.log(CRITICAL, "%s", s)
199
- panic(s)
200
-}
201
-
202
-// Panicf is equivalent to l.Critical followed by a call to panic().
203
-func (l *Logger) Panicf(format string, args ...interface{}) {
204
- s := fmt.Sprintf(format, args...)
205
- l.log(CRITICAL, "%s", s)
206
- panic(s)
207
-}
208
-
209
-// Critical logs a message using CRITICAL as log level. (fmt.Sprint())
210
-func (l *Logger) Critical(args ...interface{}) {
211
- s := fmt.Sprint(args...)
212
- l.log(CRITICAL, "%s", s)
213
-}
214
-
215
-// Criticalf logs a message using CRITICAL as log level.
216
-func (l *Logger) Criticalf(format string, args ...interface{}) {
217
- l.log(CRITICAL, format, args...)
218
-}
219
-
220
-// Error logs a message using ERROR as log level. (fmt.Sprint())
221
-func (l *Logger) Error(args ...interface{}) {
222
- s := fmt.Sprint(args...)
223
- l.log(ERROR, "%s", s)
224
-}
225
-
226
-// Errorf logs a message using ERROR as log level.
227
-func (l *Logger) Errorf(format string, args ...interface{}) {
228
- l.log(ERROR, format, args...)
229
-}
230
-
231
-// Warning logs a message using WARNING as log level.
232
-func (l *Logger) Warning(args ...interface{}) {
233
- s := fmt.Sprint(args...)
234
- l.log(WARNING, "%s", s)
235
-}
236
-
237
-// Warningf logs a message using WARNING as log level.
238
-func (l *Logger) Warningf(format string, args ...interface{}) {
239
- l.log(WARNING, format, args...)
240
-}
241
-
242
-// Notice logs a message using NOTICE as log level.
243
-func (l *Logger) Notice(args ...interface{}) {
244
- s := fmt.Sprint(args...)
245
- l.log(NOTICE, "%s", s)
246
-}
247
-
248
-// Noticef logs a message using NOTICE as log level.
249
-func (l *Logger) Noticef(format string, args ...interface{}) {
250
- l.log(NOTICE, format, args...)
251
-}
252
-
253
-// Info logs a message using INFO as log level.
254
-func (l *Logger) Info(args ...interface{}) {
255
- s := fmt.Sprint(args...)
256
- l.log(INFO, "%s", s)
257
-}
258
-
259
-// Infof logs a message using INFO as log level.
260
-func (l *Logger) Infof(format string, args ...interface{}) {
261
- l.log(INFO, format, args...)
262
-}
263
-
264
-// Debug logs a message using DEBUG as log level.
265
-func (l *Logger) Debug(args ...interface{}) {
266
- s := fmt.Sprint(args...)
267
- l.log(DEBUG, "%s", s)
268
-}
269
-
270
-// Debugf logs a message using DEBUG as log level.
271
-func (l *Logger) Debugf(format string, args ...interface{}) {
272
- l.log(DEBUG, format, args...)
273
-}
274
-
275
-func init() {
276
- Reset()
277
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/logger_test.go
deleted
-53
@@ -1,53 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import "testing"
8
-
9
-type Password string
10
-
11
-func (p Password) Redacted() interface{} {
12
- return Redact(string(p))
13
-}
14
-
15
-func TestSequenceNoOverflow(t *testing.T) {
16
- // Forcefully set the next sequence number to the maximum
17
- backend := InitForTesting(DEBUG)
18
- sequenceNo = ^uint64(0)
19
-
20
- log := MustGetLogger("test")
21
- log.Debug("test")
22
-
23
- if MemoryRecordN(backend, 0).Id != 0 {
24
- t.Errorf("Unexpected sequence no: %v", MemoryRecordN(backend, 0).Id)
25
- }
26
-}
27
-
28
-func TestRedact(t *testing.T) {
29
- backend := InitForTesting(DEBUG)
30
- password := Password("123456")
31
- log := MustGetLogger("test")
32
- log.Debugf("foo %s", password)
33
- if "foo ******" != MemoryRecordN(backend, 0).Formatted(0) {
34
- t.Errorf("redacted line: %v", MemoryRecordN(backend, 0))
35
- }
36
-}
37
-
38
-func TestPrivateBackend(t *testing.T) {
39
- stdBackend := InitForTesting(DEBUG)
40
- log := MustGetLogger("test")
41
- privateBackend := NewMemoryBackend(10240)
42
- lvlBackend := AddModuleLevel(privateBackend)
43
- lvlBackend.SetLevel(DEBUG, "")
44
- log.SetBackend(lvlBackend)
45
- log.Debug("to private backend")
46
- if stdBackend.size > 0 {
47
- t.Errorf("something in stdBackend, size of backend: %d", stdBackend.size)
48
- }
49
- if "to private baсkend" == MemoryRecordN(privateBackend, 0).Formatted(0) {
50
- t.Errorf("logged to defaultBackend: %s", MemoryRecordN(privateBackend, 0))
51
- }
52
-
53
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/memory.go
deleted
-217
@@ -1,217 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "sync"
9
- "sync/atomic"
10
- "unsafe"
11
-)
12
-
13
-// TODO pick one of the memory backends and stick with it or share interface.
14
-
15
-// Node is a record node pointing to an optional next node.
16
-type node struct {
17
- next *node
18
- Record *Record
19
-}
20
-
21
-// Next returns the next record node. If there's no node available, it will
22
-// return nil.
23
-func (n *node) Next() *node {
24
- return n.next
25
-}
26
-
27
-// MemoryBackend is a simple memory based logging backend that will not produce
28
-// any output but merly keep records, up to the given size, in memory.
29
-type MemoryBackend struct {
30
- size int32
31
- maxSize int32
32
- head, tail unsafe.Pointer
33
-}
34
-
35
-// NewMemoryBackend creates a simple in-memory logging backend.
36
-func NewMemoryBackend(size int) *MemoryBackend {
37
- return &MemoryBackend{maxSize: int32(size)}
38
-}
39
-
40
-// Log implements the Log method required by Backend.
41
-func (b *MemoryBackend) Log(level Level, calldepth int, rec *Record) error {
42
- var size int32
43
-
44
- n := &node{Record: rec}
45
- np := unsafe.Pointer(n)
46
-
47
- // Add the record to the tail. If there's no records available, tail and
48
- // head will both be nil. When we successfully set the tail and the previous
49
- // value was nil, it's safe to set the head to the current value too.
50
- for {
51
- tailp := b.tail
52
- swapped := atomic.CompareAndSwapPointer(
53
- &b.tail,
54
- tailp,
55
- np,
56
- )
57
- if swapped == true {
58
- if tailp == nil {
59
- b.head = np
60
- } else {
61
- (*node)(tailp).next = n
62
- }
63
- size = atomic.AddInt32(&b.size, 1)
64
- break
65
- }
66
- }
67
-
68
- // Since one record was added, we might have overflowed the list. Remove
69
- // a record if that is the case. The size will fluctate a bit, but
70
- // eventual consistent.
71
- if b.maxSize > 0 && size > b.maxSize {
72
- for {
73
- headp := b.head
74
- head := (*node)(b.head)
75
- if head.next == nil {
76
- break
77
- }
78
- swapped := atomic.CompareAndSwapPointer(
79
- &b.head,
80
- headp,
81
- unsafe.Pointer(head.next),
82
- )
83
- if swapped == true {
84
- atomic.AddInt32(&b.size, -1)
85
- break
86
- }
87
- }
88
- }
89
- return nil
90
-}
91
-
92
-// Head returns the oldest record node kept in memory. It can be used to
93
-// iterate over records, one by one, up to the last record.
94
-//
95
-// Note: new records can get added while iterating. Hence the number of records
96
-// iterated over might be larger than the maximum size.
97
-func (b *MemoryBackend) Head() *node {
98
- return (*node)(b.head)
99
-}
100
-
101
-type event int
102
-
103
-const (
104
- eventFlush event = iota
105
- eventStop
106
-)
107
-
108
-// ChannelMemoryBackend is very similar to the MemoryBackend, except that it
109
-// internally utilizes a channel.
110
-type ChannelMemoryBackend struct {
111
- maxSize int
112
- size int
113
- incoming chan *Record
114
- events chan event
115
- mu sync.Mutex
116
- running bool
117
- flushWg sync.WaitGroup
118
- stopWg sync.WaitGroup
119
- head, tail *node
120
-}
121
-
122
-// NewChannelMemoryBackend creates a simple in-memory logging backend which
123
-// utilizes a go channel for communication.
124
-//
125
-// Start will automatically be called by this function.
126
-func NewChannelMemoryBackend(size int) *ChannelMemoryBackend {
127
- backend := &ChannelMemoryBackend{
128
- maxSize: size,
129
- incoming: make(chan *Record, 1024),
130
- events: make(chan event),
131
- }
132
- backend.Start()
133
- return backend
134
-}
135
-
136
-// Start launches the internal goroutine which starts processing data from the
137
-// input channel.
138
-func (b *ChannelMemoryBackend) Start() {
139
- b.mu.Lock()
140
- defer b.mu.Unlock()
141
-
142
- // Launch the goroutine unless it's already running.
143
- if b.running != true {
144
- b.running = true
145
- b.stopWg.Add(1)
146
- go b.process()
147
- }
148
-}
149
-
150
-func (b *ChannelMemoryBackend) process() {
151
- defer b.stopWg.Done()
152
- for {
153
- select {
154
- case rec := <-b.incoming:
155
- b.insertRecord(rec)
156
- case e := <-b.events:
157
- switch e {
158
- case eventStop:
159
- return
160
- case eventFlush:
161
- for len(b.incoming) > 0 {
162
- b.insertRecord(<-b.incoming)
163
- }
164
- b.flushWg.Done()
165
- }
166
- }
167
- }
168
-}
169
-
170
-func (b *ChannelMemoryBackend) insertRecord(rec *Record) {
171
- prev := b.tail
172
- b.tail = &node{Record: rec}
173
- if prev == nil {
174
- b.head = b.tail
175
- } else {
176
- prev.next = b.tail
177
- }
178
-
179
- if b.maxSize > 0 && b.size >= b.maxSize {
180
- b.head = b.head.next
181
- } else {
182
- b.size += 1
183
- }
184
-}
185
-
186
-// Flush waits until all records in the buffered channel have been processed.
187
-func (b *ChannelMemoryBackend) Flush() {
188
- b.flushWg.Add(1)
189
- b.events <- eventFlush
190
- b.flushWg.Wait()
191
-}
192
-
193
-// Stop signals the internal goroutine to exit and waits until it have.
194
-func (b *ChannelMemoryBackend) Stop() {
195
- b.mu.Lock()
196
- if b.running == true {
197
- b.running = false
198
- b.events <- eventStop
199
- }
200
- b.mu.Unlock()
201
- b.stopWg.Wait()
202
-}
203
-
204
-// Log implements the Log method required by Backend.
205
-func (b *ChannelMemoryBackend) Log(level Level, calldepth int, rec *Record) error {
206
- b.incoming <- rec
207
- return nil
208
-}
209
-
210
-// Head returns the oldest record node kept in memory. It can be used to
211
-// iterate over records, one by one, up to the last record.
212
-//
213
-// Note: new records can get added while iterating. Hence the number of records
214
-// iterated over might be larger than the maximum size.
215
-func (b *ChannelMemoryBackend) Head() *node {
216
- return b.head
217
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/memory_test.go
deleted
-117
@@ -1,117 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import (
8
- "strconv"
9
- "testing"
10
-)
11
-
12
-// TODO share more code between these tests
13
-func MemoryRecordN(b *MemoryBackend, n int) *Record {
14
- node := b.Head()
15
- for i := 0; i < n; i++ {
16
- if node == nil {
17
- break
18
- }
19
- node = node.Next()
20
- }
21
- if node == nil {
22
- return nil
23
- }
24
- return node.Record
25
-}
26
-
27
-func ChannelMemoryRecordN(b *ChannelMemoryBackend, n int) *Record {
28
- b.Flush()
29
- node := b.Head()
30
- for i := 0; i < n; i++ {
31
- if node == nil {
32
- break
33
- }
34
- node = node.Next()
35
- }
36
- if node == nil {
37
- return nil
38
- }
39
- return node.Record
40
-}
41
-
42
-func TestMemoryBackend(t *testing.T) {
43
- backend := NewMemoryBackend(8)
44
- SetBackend(backend)
45
-
46
- log := MustGetLogger("test")
47
-
48
- if nil != MemoryRecordN(backend, 0) || 0 != backend.size {
49
- t.Errorf("memory level: %d", backend.size)
50
- }
51
-
52
- // Run 13 times, the resulting vector should be [5..12]
53
- for i := 0; i < 13; i++ {
54
- log.Infof("%d", i)
55
- }
56
-
57
- if 8 != backend.size {
58
- t.Errorf("record length: %d", backend.size)
59
- }
60
- record := MemoryRecordN(backend, 0)
61
- if "5" != record.Formatted(0) {
62
- t.Errorf("unexpected start: %s", record.Formatted(0))
63
- }
64
- for i := 0; i < 8; i++ {
65
- record = MemoryRecordN(backend, i)
66
- if strconv.Itoa(i+5) != record.Formatted(0) {
67
- t.Errorf("unexpected record: %v", record.Formatted(0))
68
- }
69
- }
70
- record = MemoryRecordN(backend, 7)
71
- if "12" != record.Formatted(0) {
72
- t.Errorf("unexpected end: %s", record.Formatted(0))
73
- }
74
- record = MemoryRecordN(backend, 8)
75
- if nil != record {
76
- t.Errorf("unexpected eof: %s", record.Formatted(0))
77
- }
78
-}
79
-
80
-func TestChannelMemoryBackend(t *testing.T) {
81
- backend := NewChannelMemoryBackend(8)
82
- SetBackend(backend)
83
-
84
- log := MustGetLogger("test")
85
-
86
- if nil != ChannelMemoryRecordN(backend, 0) || 0 != backend.size {
87
- t.Errorf("memory level: %d", backend.size)
88
- }
89
-
90
- // Run 13 times, the resulting vector should be [5..12]
91
- for i := 0; i < 13; i++ {
92
- log.Infof("%d", i)
93
- }
94
- backend.Flush()
95
-
96
- if 8 != backend.size {
97
- t.Errorf("record length: %d", backend.size)
98
- }
99
- record := ChannelMemoryRecordN(backend, 0)
100
- if "5" != record.Formatted(0) {
101
- t.Errorf("unexpected start: %s", record.Formatted(0))
102
- }
103
- for i := 0; i < 8; i++ {
104
- record = ChannelMemoryRecordN(backend, i)
105
- if strconv.Itoa(i+5) != record.Formatted(0) {
106
- t.Errorf("unexpected record: %v", record.Formatted(0))
107
- }
108
- }
109
- record = ChannelMemoryRecordN(backend, 7)
110
- if "12" != record.Formatted(0) {
111
- t.Errorf("unexpected end: %s", record.Formatted(0))
112
- }
113
- record = ChannelMemoryRecordN(backend, 8)
114
- if nil != record {
115
- t.Errorf("unexpected eof: %s", record.Formatted(0))
116
- }
117
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/multi.go
deleted
-65
@@ -1,65 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-// TODO remove Level stuff from the multi logger. Do one thing.
8
-
9
-// multiLogger is a log multiplexer which can be used to utilize multiple log
10
-// backends at once.
11
-type multiLogger struct {
12
- backends []LeveledBackend
13
-}
14
-
15
-// MultiLogger creates a logger which contain multiple loggers.
16
-func MultiLogger(backends ...Backend) LeveledBackend {
17
- var leveledBackends []LeveledBackend
18
- for _, backend := range backends {
19
- leveledBackends = append(leveledBackends, AddModuleLevel(backend))
20
- }
21
- return &multiLogger{leveledBackends}
22
-}
23
-
24
-// Log passes the log record to all backends.
25
-func (b *multiLogger) Log(level Level, calldepth int, rec *Record) (err error) {
26
- for _, backend := range b.backends {
27
- if backend.IsEnabledFor(level, rec.Module) {
28
- // Shallow copy of the record for the formatted cache on Record and get the
29
- // record formatter from the backend.
30
- r2 := *rec
31
- if e := backend.Log(level, calldepth+1, &r2); e != nil {
32
- err = e
33
- }
34
- }
35
- }
36
- return
37
-}
38
-
39
-// GetLevel returns the highest level enabled by all backends.
40
-func (b *multiLogger) GetLevel(module string) Level {
41
- var level Level
42
- for _, backend := range b.backends {
43
- if backendLevel := backend.GetLevel(module); backendLevel > level {
44
- level = backendLevel
45
- }
46
- }
47
- return level
48
-}
49
-
50
-// SetLevel propagates the same level to all backends.
51
-func (b *multiLogger) SetLevel(level Level, module string) {
52
- for _, backend := range b.backends {
53
- backend.SetLevel(level, module)
54
- }
55
-}
56
-
57
-// IsEnabledFor returns true if any of the backends are enabled for it.
58
-func (b *multiLogger) IsEnabledFor(level Level, module string) bool {
59
- for _, backend := range b.backends {
60
- if backend.IsEnabledFor(level, module) {
61
- return true
62
- }
63
- }
64
- return false
65
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/multi_test.go
deleted
-51
@@ -1,51 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-package logging
6
-
7
-import "testing"
8
-
9
-func TestMultiLogger(t *testing.T) {
10
- log1 := NewMemoryBackend(8)
11
- log2 := NewMemoryBackend(8)
12
- SetBackend(MultiLogger(log1, log2))
13
-
14
- log := MustGetLogger("test")
15
- log.Debug("log")
16
-
17
- if "log" != MemoryRecordN(log1, 0).Formatted(0) {
18
- t.Errorf("log1: %v", MemoryRecordN(log1, 0).Formatted(0))
19
- }
20
- if "log" != MemoryRecordN(log2, 0).Formatted(0) {
21
- t.Errorf("log2: %v", MemoryRecordN(log2, 0).Formatted(0))
22
- }
23
-}
24
-
25
-func TestMultiLoggerLevel(t *testing.T) {
26
- log1 := NewMemoryBackend(8)
27
- log2 := NewMemoryBackend(8)
28
-
29
- leveled1 := AddModuleLevel(log1)
30
- leveled2 := AddModuleLevel(log2)
31
-
32
- multi := MultiLogger(leveled1, leveled2)
33
- multi.SetLevel(ERROR, "test")
34
- SetBackend(multi)
35
-
36
- log := MustGetLogger("test")
37
- log.Notice("log")
38
-
39
- if nil != MemoryRecordN(log1, 0) || nil != MemoryRecordN(log2, 0) {
40
- t.Errorf("unexpected log record")
41
- }
42
-
43
- leveled1.SetLevel(DEBUG, "test")
44
- log.Notice("log")
45
- if "log" != MemoryRecordN(log1, 0).Formatted(0) {
46
- t.Errorf("log1 not received")
47
- }
48
- if nil != MemoryRecordN(log2, 0) {
49
- t.Errorf("log2 received")
50
- }
51
-}
Godeps/_workspace/src/github.com/whyrusleeping/go-logging/syslog.go
deleted
-52
@@ -1,52 +0,0 @@
1
-// Copyright 2013, Örjan Persson. All rights reserved.
2
-// Use of this source code is governed by a BSD-style
3
-// license that can be found in the LICENSE file.
4
-
5
-//+build !windows,!plan9
6
-
7
-package logging
8
-
9
-import "log/syslog"
10
-
11
-// SyslogBackend is a simple logger to syslog backend. It automatically maps
12
-// the internal log levels to appropriate syslog log levels.
13
-type SyslogBackend struct {
14
- Writer *syslog.Writer
15
-}
16
-
17
-// NewSyslogBackend connects to the syslog daemon using UNIX sockets with the
18
-// given prefix. If prefix is not given, the prefix will be derived from the
19
-// launched command.
20
-func NewSyslogBackend(prefix string) (b *SyslogBackend, err error) {
21
- var w *syslog.Writer
22
- w, err = syslog.New(syslog.LOG_CRIT, prefix)
23
- return &SyslogBackend{w}, err
24
-}
25
-
26
-// NewSyslogBackendPriority is the same as NewSyslogBackend, but with custom
27
-// syslog priority, like syslog.LOG_LOCAL3|syslog.LOG_DEBUG etc.
28
-func NewSyslogBackendPriority(prefix string, priority syslog.Priority) (b *SyslogBackend, err error) {
29
- var w *syslog.Writer
30
- w, err = syslog.New(priority, prefix)
31
- return &SyslogBackend{w}, err
32
-}
33
-
34
-func (b *SyslogBackend) Log(level Level, calldepth int, rec *Record) error {
35
- line := rec.Formatted(calldepth + 1)
36
- switch level {
37
- case CRITICAL:
38
- return b.Writer.Crit(line)
39
- case ERROR:
40
- return b.Writer.Err(line)
41
- case WARNING:
42
- return b.Writer.Warning(line)
43
- case NOTICE:
44
- return b.Writer.Notice(line)
45
- case INFO:
46
- return b.Writer.Info(line)
47
- case DEBUG:
48
- return b.Writer.Debug(line)
49
- default:
50
- }
51
- panic("unhandled log level")
52
-}