@cryptotaxi247 / kubo / commits / a676b5a8a

move eventlogs to an http endpoint

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jun 16, 2015 at 19:45 UTC a676b5a8ac2848a57318283f4b2b52d7d8686323
5 files changed +77 -7
cmd/ipfs/daemon.go
+1
@@ -285,6 +285,7 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
285 corehttp.VersionOption(),
286 defaultMux("/debug/vars"),
287 defaultMux("/debug/pprof/"),
288 + corehttp.LogOption(),
289 }
290
291 if len(cfg.Gateway.RootRedirect) > 0 {
core/corehttp/logs.go new
+42
@@ -0,0 +1,42 @@
1 +package corehttp
2 +
3 +import (
4 + "io"
5 + "net/http"
6 +
7 + core "github.com/ipfs/go-ipfs/core"
8 + "github.com/ipfs/go-ipfs/thirdparty/eventlog"
9 +)
10 +
11 +type writeErrNotifier struct {
12 + w io.Writer
13 + errs chan error
14 +}
15 +
16 +func newWriteErrNotifier(w io.Writer) (io.Writer, <-chan error) {
17 + ch := make(chan error, 1)
18 + return &writeErrNotifier{
19 + w: w,
20 + errs: ch,
21 + }, ch
22 +}
23 +
24 +func (w *writeErrNotifier) Write(b []byte) (int, error) {
25 + n, err := w.w.Write(b)
26 + if err != nil {
27 + w.errs <- err
28 + }
29 + return n, err
30 +}
31 +
32 +func LogOption() ServeOption {
33 + return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
34 + mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
35 + w.WriteHeader(200)
36 + wnf, errs := newWriteErrNotifier(w)
37 + eventlog.WriterGroup.AddWriter(wnf)
38 + <-errs
39 + })
40 + return mux, nil
41 + }
42 +}
repo/fsrepo/fsrepo.go
+1 -7
@@ -368,13 +368,7 @@ func (r *FSRepo) openDatastore() error {
368 func configureEventLoggerAtRepoPath(c *config.Config, repoPath string) {
369 eventlog.Configure(eventlog.LevelInfo)
370 eventlog.Configure(eventlog.LdJSONFormatter)
371 - rotateConf := eventlog.LogRotatorConfig{
372 - Filename: path.Join(repoPath, "logs", "events.log"),
373 - MaxSizeMB: c.Log.MaxSizeMB,
374 - MaxBackups: c.Log.MaxBackups,
375 - MaxAgeDays: c.Log.MaxAgeDays,
376 - }
377 - eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
371 + eventlog.Configure(eventlog.Output(eventlog.WriterGroup))
372 }
373
374 // Close closes the FSRepo, releasing held resources.
thirdparty/eventlog/option.go
+2
@@ -18,6 +18,8 @@ func init() {
18 Configure(LevelError)
19 }
20
21 +var WriterGroup = new(MirrorWriter)
22 +
23 type Option func()
24
25 // Configure applies the provided options sequentially from left to right
thirdparty/eventlog/writer.go new
+31
@@ -0,0 +1,31 @@
1 +package eventlog
2 +
3 +import (
4 + "io"
5 + "sync"
6 +)
7 +
8 +type MirrorWriter struct {
9 + writers []io.Writer
10 + lk sync.Mutex
11 +}
12 +
13 +func (mw *MirrorWriter) Write(b []byte) (int, error) {
14 + mw.lk.Lock()
15 + var filter []io.Writer
16 + for _, w := range mw.writers {
17 + _, err := w.Write(b)
18 + if err == nil {
19 + filter = append(filter, w)
20 + }
21 + }
22 + mw.writers = filter
23 + mw.lk.Unlock()
24 + return len(b), nil
25 +}
26 +
27 +func (mw *MirrorWriter) AddWriter(w io.Writer) {
28 + mw.lk.Lock()
29 + mw.writers = append(mw.writers, w)
30 + mw.lk.Unlock()
31 +}