mirror of
https://github.com/ipfs/kubo.git
synced 2025-10-25 10:27:01 +08:00
move eventlogs to an http endpoint
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -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 {
|
||||
|
||||
42
core/corehttp/logs.go
Normal file
42
core/corehttp/logs.go
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
@ -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.
|
||||
|
||||
2
thirdparty/eventlog/option.go
vendored
2
thirdparty/eventlog/option.go
vendored
@ -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
|
||||
|
||||
31
thirdparty/eventlog/writer.go
vendored
Normal file
31
thirdparty/eventlog/writer.go
vendored
Normal file
@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user