David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2016 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package loggo |
| 5 | |
| 6 | var ( |
| 7 | defaultContext = newDefaultContxt() |
| 8 | ) |
| 9 | |
| 10 | func newDefaultContxt() *Context { |
| 11 | ctx := NewContext(WARNING) |
| 12 | ctx.AddWriter(DefaultWriterName, defaultWriter()) |
| 13 | return ctx |
| 14 | } |
| 15 | |
| 16 | // DefaultContext returns the global default logging context. |
| 17 | func DefaultContext() *Context { |
| 18 | return defaultContext |
| 19 | } |
| 20 | |
| 21 | // LoggerInfo returns information about the configured loggers and their |
| 22 | // logging levels. The information is returned in the format expected by |
| 23 | // ConfigureLoggers. Loggers with UNSPECIFIED level will not |
| 24 | // be included. |
| 25 | func LoggerInfo() string { |
| 26 | return defaultContext.Config().String() |
| 27 | } |
| 28 | |
| 29 | // GetLogger returns a Logger for the given module name, |
| 30 | // creating it and its parents if necessary. |
| 31 | func GetLogger(name string) Logger { |
| 32 | return defaultContext.GetLogger(name) |
| 33 | } |
| 34 | |
| 35 | // ResetLogging iterates through the known modules and sets the levels of all |
| 36 | // to UNSPECIFIED, except for <root> which is set to WARNING. The call also |
| 37 | // removes all writers in the DefaultContext and puts the original default |
| 38 | // writer back as the only writer. |
| 39 | func ResetLogging() { |
| 40 | defaultContext.ResetLoggerLevels() |
| 41 | defaultContext.ResetWriters() |
| 42 | } |
| 43 | |
| 44 | // ResetWriters puts the list of writers back into the initial state. |
| 45 | func ResetWriters() { |
| 46 | defaultContext.ResetWriters() |
| 47 | } |
| 48 | |
| 49 | // ReplaceDefaultWriter is a convenience method that does the equivalent of |
| 50 | // RemoveWriter and then RegisterWriter with the name "default". The previous |
| 51 | // default writer, if any is returned. |
| 52 | func ReplaceDefaultWriter(writer Writer) (Writer, error) { |
| 53 | return defaultContext.ReplaceWriter(DefaultWriterName, writer) |
| 54 | } |
| 55 | |
| 56 | // RegisterWriter adds the writer to the list of writers in the DefaultContext |
| 57 | // that get notified when logging. If there is already a registered writer |
| 58 | // with that name, an error is returned. |
| 59 | func RegisterWriter(name string, writer Writer) error { |
| 60 | return defaultContext.AddWriter(name, writer) |
| 61 | } |
| 62 | |
| 63 | // RemoveWriter removes the Writer identified by 'name' and returns it. |
| 64 | // If the Writer is not found, an error is returned. |
| 65 | func RemoveWriter(name string) (Writer, error) { |
| 66 | return defaultContext.RemoveWriter(name) |
| 67 | } |
| 68 | |
| 69 | // ConfigureLoggers configures loggers according to the given string |
| 70 | // specification, which specifies a set of modules and their associated |
| 71 | // logging levels. Loggers are colon- or semicolon-separated; each |
| 72 | // module is specified as <modulename>=<level>. White space outside of |
| 73 | // module names and levels is ignored. The root module is specified |
| 74 | // with the name "<root>". |
| 75 | // |
| 76 | // An example specification: |
| 77 | // `<root>=ERROR; foo.bar=WARNING` |
| 78 | func ConfigureLoggers(specification string) error { |
| 79 | config, err := ParseConfigString(specification) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | defaultContext.ApplyConfig(config) |
| 84 | return nil |
| 85 | } |