OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package logging | 5 package logging |
6 | 6 |
7 import ( | 7 import ( |
8 "flag" | 8 "flag" |
9 | 9 |
10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
11 ) | 11 ) |
12 | 12 |
13 // Config is a logging configuration structure. | 13 // Config is a logging configuration structure. |
14 type Config struct { | 14 type Config struct { |
15 » Level Level | 15 » Level Level |
16 » Filter Filter | |
17 } | 16 } |
18 | 17 |
19 // AddFlags adds common flags to a supplied FlagSet. | 18 // AddFlags adds common flags to a supplied FlagSet. |
20 func (c *Config) AddFlags(fs *flag.FlagSet) { | 19 func (c *Config) AddFlags(fs *flag.FlagSet) { |
21 » fs.Var(&c.Level, "log_level", | 20 » fs.Var(&c.Level, "log-level", |
22 "The logging level. Valid options are: debug, info, warning, err
or.") | 21 "The logging level. Valid options are: debug, info, warning, err
or.") |
23 fs.Var(&c.Filter, "log_filter", | |
24 "Log filter keywords. Can be specified multiple times.") | |
25 } | 22 } |
26 | 23 |
27 // Set installs a filter-aware logger that wraps the currently-installed Logger | 24 // Set installs a logger that wraps the currently-installed Logger and sets |
28 // and selectively discards messages based on the logging configuration. | 25 // the level via the command-line level flag. |
29 func (c *Config) Set(ctx context.Context) context.Context { | 26 func (c *Config) Set(ctx context.Context) context.Context { |
30 » filterFunc := c.Filter.Get() | 27 » return SetLevel(ctx, c.Level) |
31 » baseFactory := GetFactory(ctx) | |
32 | |
33 » ctx = SetLevel(ctx, c.Level) | |
34 » return SetFactory(ctx, func(ctx context.Context) Logger { | |
35 » » if value, ok := GetFields(ctx)[FilterOnKey]; ok { | |
36 » » » if !filterFunc(value) { | |
37 » » » » return Null() | |
38 » » » } | |
39 » » } | |
40 » » return baseFactory(ctx) | |
41 » }) | |
42 } | 28 } |
OLD | NEW |