~rogpeppe/juju-core/themue-058-debug-log-api

« back to all changes in this revision

Viewing changes to state/apiserver/logger/logger.go

  • Committer: Frank Mueller
  • Date: 2013-12-20 11:36:11 UTC
  • Revision ID: frank.mueller@canonical.com-20131220113611-0abqsnf63yx8bt0s
apiserver/logger: added logtailer and fixed tailer

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
        "launchpad.net/juju-core/state/watcher"
13
13
)
14
14
 
15
 
var logger = loggo.GetLogger("juju.api.logger")
 
15
var (
 
16
        logger = loggo.GetLogger("juju.api.logger")
 
17
 
 
18
        logLocation = "/var/log/juju/all-machines.log"
 
19
)
16
20
 
17
21
// Logger defines the methods on the logger API end point.  Unfortunately, the
18
22
// api infrastructure doesn't allow interfaces to be used as an actual
21
25
type Logger interface {
22
26
        WatchLoggingConfig(args params.Entities) params.NotifyWatchResults
23
27
        LoggingConfig(args params.Entities) params.StringResults
 
28
        WatchDebugLog(args params.DebugLogWatchings) params.StringsWatchResults
24
29
}
25
30
 
26
31
// LoggerAPI implements the Logger interface and is the concrete
91
96
        }
92
97
        return params.StringResults{results}
93
98
}
 
99
 
 
100
// WatchDebugLog starts a StringsWatcher to watch the debug log for entries
 
101
// regarding the machines or units passed in args.
 
102
func (api *LoggerAPI) WatchDebugLog(args params.DebugLogWatchings) params.StringsWatchResults {
 
103
        result := params.StringsWatchResults{
 
104
                Results: make([]params.StringsWatchResult, len(args.Watchings)),
 
105
        }
 
106
        for i, watching := range args.Watchings {
 
107
                debugResult, err := api.watchOneDebugLog(watching)
 
108
                result.Results[i] = debugResult
 
109
                result.Results[i].Error = common.ServerError(err)
 
110
        }
 
111
        return result
 
112
}
 
113
 
 
114
// watchOneDebugLog starts a StringWatcher for the log entries of
 
115
// one machine or unit.
 
116
func (api *LoggerAPI) watchOneDebugLog(arg params.DebugLogWatching) (params.StringsWatchResult, error) {
 
117
        nothing := params.StringsWatchResult{}
 
118
        logTailer, err := startLogTailer(logLocation, arg.Lines, arg.Tag)
 
119
        if err != nil {
 
120
                return nothing, err
 
121
        }
 
122
        defer logTailer.Stop()
 
123
        // Consume the initial event and forward it to the result.
 
124
        if changes, ok := <-logTailer.Changes(); ok {
 
125
                return params.StringsWatchResult{
 
126
                        StringsWatcherId: api.resources.Register(logTailer),
 
127
                        Changes:          changes,
 
128
                }, nil
 
129
        }
 
130
        return nothing, watcher.MustErr(logTailer)
 
131
}