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
|
// Copyright 2016 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package loggo
import (
"fmt"
"strings"
"sync"
)
// Context produces loggers for a hierarchy of modules. The context holds
// a collection of hierarchical loggers and their writers.
type Context struct {
root *module
// Perhaps have one mutex?
modulesMutex sync.Mutex
modules map[string]*module
writersMutex sync.Mutex
writers map[string]Writer
// writeMuxtex is used to serialise write operations.
writeMutex sync.Mutex
}
// NewLoggers returns a new Context with no writers set.
// If the root level is UNSPECIFIED, WARNING is used.
func NewContext(rootLevel Level) *Context {
if rootLevel < TRACE || rootLevel > CRITICAL {
rootLevel = WARNING
}
context := &Context{
modules: make(map[string]*module),
writers: make(map[string]Writer),
}
context.root = &module{
level: rootLevel,
context: context,
}
context.modules[""] = context.root
return context
}
// GetLogger returns a Logger for the given module name, creating it and
// its parents if necessary.
func (c *Context) GetLogger(name string) Logger {
name = strings.TrimSpace(strings.ToLower(name))
c.modulesMutex.Lock()
defer c.modulesMutex.Unlock()
return Logger{c.getLoggerModule(name)}
}
func (c *Context) getLoggerModule(name string) *module {
if name == rootString {
name = ""
}
impl, found := c.modules[name]
if found {
return impl
}
parentName := ""
if i := strings.LastIndex(name, "."); i >= 0 {
parentName = name[0:i]
}
parent := c.getLoggerModule(parentName)
impl = &module{name, UNSPECIFIED, parent, c}
c.modules[name] = impl
return impl
}
// Config returns the current configuration of the Loggers. Loggers
// with UNSPECIFIED level will not be included.
func (c *Context) Config() Config {
result := make(Config)
c.modulesMutex.Lock()
defer c.modulesMutex.Unlock()
for name, module := range c.modules {
if module.level != UNSPECIFIED {
result[name] = module.level
}
}
return result
}
// CompleteConfig returns all the loggers and their defined levels,
// even if that level is UNSPECIFIED.
func (c *Context) CompleteConfig() Config {
result := make(Config)
c.modulesMutex.Lock()
defer c.modulesMutex.Unlock()
for name, module := range c.modules {
result[name] = module.level
}
return result
}
// ApplyConfig configures the logging modules according to the provided config.
func (c *Context) ApplyConfig(config Config) {
c.modulesMutex.Lock()
defer c.modulesMutex.Unlock()
for name, level := range config {
module := c.getLoggerModule(name)
module.setLevel(level)
}
}
// ResetLoggerLevels iterates through the known logging modules and sets the
// levels of all to UNSPECIFIED, except for <root> which is set to WARNING.
func (c *Context) ResetLoggerLevels() {
c.modulesMutex.Lock()
defer c.modulesMutex.Unlock()
// Setting the root module to UNSPECIFIED will set it to WARNING.
for _, module := range c.modules {
module.setLevel(UNSPECIFIED)
}
}
func (c *Context) write(entry Entry) {
c.writeMutex.Lock()
defer c.writeMutex.Unlock()
for _, writer := range c.getWriters() {
writer.Write(entry)
}
}
func (c *Context) getWriters() []Writer {
c.writersMutex.Lock()
defer c.writersMutex.Unlock()
var result []Writer
for _, writer := range c.writers {
result = append(result, writer)
}
return result
}
// AddWriter adds a writer to the list to be called for each logging call.
// The name cannot be empty, and the writer cannot be nil. If an existing
// writer exists with the specified name, an error is returned.
func (c *Context) AddWriter(name string, writer Writer) error {
if name == "" {
return fmt.Errorf("name cannot be empty")
}
if writer == nil {
return fmt.Errorf("writer cannot be nil")
}
c.writersMutex.Lock()
defer c.writersMutex.Unlock()
if _, found := c.writers[name]; found {
return fmt.Errorf("context already has a writer named %q", name)
}
c.writers[name] = writer
return nil
}
// RemoveWriter remotes the specified writer. If a writer is not found with
// the specified name an error is returned. The writer that was removed is also
// returned.
func (c *Context) RemoveWriter(name string) (Writer, error) {
c.writersMutex.Lock()
defer c.writersMutex.Unlock()
reg, found := c.writers[name]
if !found {
return nil, fmt.Errorf("context has no writer named %q", name)
}
delete(c.writers, name)
return reg, nil
}
// ReplaceWriter is a convenience method that does the equivalent of RemoveWriter
// followed by AddWriter with the same name. The replaced writer is returned.
func (c *Context) ReplaceWriter(name string, writer Writer) (Writer, error) {
if name == "" {
return nil, fmt.Errorf("name cannot be empty")
}
if writer == nil {
return nil, fmt.Errorf("writer cannot be nil")
}
c.writersMutex.Lock()
defer c.writersMutex.Unlock()
reg, found := c.writers[name]
if !found {
return nil, fmt.Errorf("context has no writer named %q", name)
}
oldWriter := reg
c.writers[name] = writer
return oldWriter, nil
}
// ResetWriters is generally only used in testing and removes all the writers.
func (c *Context) ResetWriters() {
c.writersMutex.Lock()
defer c.writersMutex.Unlock()
c.writers = make(map[string]Writer)
}
|