Skip to content

Commit

Permalink
move eventlogs to an http endpoint
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Jun 18, 2015
1 parent 152247d commit a676b5a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func serveHTTPApi(req cmds.Request) (error, <-chan error) {
corehttp.VersionOption(),
defaultMux("/debug/vars"),
defaultMux("/debug/pprof/"),
corehttp.LogOption(),
}

if len(cfg.Gateway.RootRedirect) > 0 {
Expand Down
42 changes: 42 additions & 0 deletions core/corehttp/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package corehttp

import (
"io"
"net/http"

core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/thirdparty/eventlog"
)

type writeErrNotifier struct {
w io.Writer
errs chan error
}

func newWriteErrNotifier(w io.Writer) (io.Writer, <-chan error) {
ch := make(chan error, 1)
return &writeErrNotifier{
w: w,
errs: ch,
}, ch
}

func (w *writeErrNotifier) Write(b []byte) (int, error) {
n, err := w.w.Write(b)
if err != nil {
w.errs <- err
}
return n, err
}

func LogOption() ServeOption {
return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
wnf, errs := newWriteErrNotifier(w)
eventlog.WriterGroup.AddWriter(wnf)
<-errs
})
return mux, nil
}
}
8 changes: 1 addition & 7 deletions repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,7 @@ func (r *FSRepo) openDatastore() error {
func configureEventLoggerAtRepoPath(c *config.Config, repoPath string) {
eventlog.Configure(eventlog.LevelInfo)
eventlog.Configure(eventlog.LdJSONFormatter)
rotateConf := eventlog.LogRotatorConfig{
Filename: path.Join(repoPath, "logs", "events.log"),
MaxSizeMB: c.Log.MaxSizeMB,
MaxBackups: c.Log.MaxBackups,
MaxAgeDays: c.Log.MaxAgeDays,
}
eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
eventlog.Configure(eventlog.Output(eventlog.WriterGroup))
}

// Close closes the FSRepo, releasing held resources.
Expand Down
2 changes: 2 additions & 0 deletions thirdparty/eventlog/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func init() {
Configure(LevelError)
}

var WriterGroup = new(MirrorWriter)

type Option func()

// Configure applies the provided options sequentially from left to right
Expand Down
31 changes: 31 additions & 0 deletions thirdparty/eventlog/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package eventlog

import (
"io"
"sync"
)

type MirrorWriter struct {
writers []io.Writer
lk sync.Mutex
}

func (mw *MirrorWriter) Write(b []byte) (int, error) {
mw.lk.Lock()
var filter []io.Writer
for _, w := range mw.writers {
_, err := w.Write(b)
if err == nil {
filter = append(filter, w)
}
}
mw.writers = filter
mw.lk.Unlock()
return len(b), nil
}

func (mw *MirrorWriter) AddWriter(w io.Writer) {
mw.lk.Lock()
mw.writers = append(mw.writers, w)
mw.lk.Unlock()
}

0 comments on commit a676b5a

Please sign in to comment.