Commit a676b5a8 authored by Jeromy's avatar Jeromy

move eventlogs to an http endpoint

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent 152247df
......@@ -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 {
......
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
}
}
......@@ -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.
......
......@@ -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
......
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()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment