OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /* | |
6 Package logging defines Logger interface and context.Context helpers to put\get | |
7 logger from context.Context. | |
8 | |
9 Unfortunately standard library doesn't define any Logger interface (only | |
10 struct). And even worse: GAE logger is exposing different set of methods. Some | |
11 additional layer is needed to unify the logging. Package logging is intended to | |
12 be used from packages that support both local and GAE environments. Such | |
13 packages should not use global logger but must accept instances of Logger | |
14 interface (or even more generally context.Context) as parameters. Then callers | |
15 can pass appropriate Logger implementation (or inject appropriate logger into | |
16 context.Context) depending on where the code is running. | |
17 | |
18 Libraries under infra/libs/* MUST use infra/libs/logger instead of directly | |
19 instantiating concrete implementations. | |
20 */ | |
21 package logging | |
22 | |
23 import ( | |
24 "golang.org/x/net/context" | |
25 ) | |
26 | |
27 // Logger interface is ultimately implemented by underlying logging libraries | |
28 // (like go-logging or GAE logging). It is the least common denominator among | |
29 // logger implementations. | |
30 type Logger interface { | |
31 // Infof formats its arguments according to the format, analogous to | |
32 // fmt.Printf and records the text as a log message at Info level. | |
33 Infof(format string, args ...interface{}) | |
34 | |
35 // Warningf is like Infof, but logs at Warning level. | |
36 Warningf(format string, args ...interface{}) | |
37 | |
38 // Errorf is like Infof, but logs at Error level. | |
39 Errorf(format string, args ...interface{}) | |
40 } | |
41 | |
42 type key int | |
43 | |
44 var loggerKey key | |
45 | |
46 // Set sets the Logger factory for this context. | |
47 // | |
48 // The current Logger can be retrieved with Get(context) | |
49 func Set(c context.Context, f func(context.Context) Logger) context.Context { | |
50 return context.WithValue(c, loggerKey, f) | |
51 } | |
52 | |
53 // Get the current Logger, or a logger that ignores all messages if none | |
54 // is defined. | |
55 func Get(c context.Context) (ret Logger) { | |
56 if f, ok := c.Value(loggerKey).(func(context.Context) Logger); ok { | |
57 ret = f(c) | |
58 } | |
59 if ret == nil { | |
60 ret = Null() | |
61 } | |
62 return | |
63 } | |
64 | |
65 // Null returns logger that silently ignores all messages. | |
66 func Null() Logger { | |
67 return nullLogger{} | |
68 } | |
69 | |
70 // nullLogger silently ignores all messages. | |
71 type nullLogger struct{} | |
72 | |
73 func (nullLogger) Infof(string, ...interface{}) {} | |
74 func (nullLogger) Warningf(string, ...interface{}) {} | |
75 func (nullLogger) Errorf(string, ...interface{}) {} | |
OLD | NEW |