launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/worker/logger/logger.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package logger 5 6 import ( 7 "github.com/loggo/loggo" 8 "launchpad.net/juju-core/agent" 9 "launchpad.net/juju-core/state/api/logger" 10 "launchpad.net/juju-core/state/api/watcher" 11 "launchpad.net/juju-core/worker" 12 ) 13 14 var log = loggo.GetLogger("juju.worker.logger") 15 16 // Logger is responsible for updating the loggo configuration when the 17 // environment watcher tells the agent that the value has changed. 18 type Logger struct { 19 api *logger.State 20 agentConfig agent.Config 21 lastConfig string 22 } 23 24 var _ worker.NotifyWatchHandler = (*Logger)(nil) 25 26 // NewLogger returns a worker.Worker that uses the notify watcher returned 27 // from the setup. 28 func NewLogger(api *logger.State, agentConfig agent.Config) worker.Worker { 29 logger := &Logger{ 30 api: api, 31 agentConfig: agentConfig, 32 lastConfig: loggo.LoggerInfo(), 33 } 34 log.Debugf("initial log config: %q", logger.lastConfig) 35 return worker.NewNotifyWorker(logger) 36 } 37 38 func (logger *Logger) setLogging() { 39 loggingConfig, err := logger.api.LoggingConfig(logger.agentConfig.Tag()) 40 if err != nil { 41 log.Errorf("%v", err) 42 } else { 43 if loggingConfig != logger.lastConfig { 44 log.Debugf("reconfiguring logging from %q to %q", logger.lastConfig, loggingConfig) 45 loggo.ResetLoggers() 46 if err := loggo.ConfigureLoggers(loggingConfig); err != nil { 47 // This shouldn't occur as the loggingConfig should be 48 // validated by the original Config before it gets here. 49 log.Warningf("configure loggers failed: %v", err) 50 // Try to reset to what we had before 51 loggo.ConfigureLoggers(logger.lastConfig) 52 } 53 logger.lastConfig = loggingConfig 54 } 55 } 56 } 57 58 func (logger *Logger) SetUp() (watcher.NotifyWatcher, error) { 59 log.Debugf("logger setup") 60 // We need to set this up initially as the NotifyWorker sucks up the first 61 // event. 62 logger.setLogging() 63 return logger.api.WatchLoggingConfig(logger.agentConfig.Tag()) 64 } 65 66 func (logger *Logger) Handle() error { 67 logger.setLogging() 68 return nil 69 } 70 71 func (logger *Logger) TearDown() error { 72 // Nothing to cleanup, only state is the watcher 73 return nil 74 }