mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 14:34:24 +08:00
refactor(eventlog) integrate into fsrepo
now, eventlogger works wherever the fsrepo is used
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"path"
|
||||
|
||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||
cmds "github.com/jbenet/go-ipfs/commands"
|
||||
@ -14,7 +13,6 @@ import (
|
||||
ipns "github.com/jbenet/go-ipfs/fuse/ipns"
|
||||
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
||||
repo "github.com/jbenet/go-ipfs/repo"
|
||||
config "github.com/jbenet/go-ipfs/repo/config"
|
||||
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
@ -110,9 +108,6 @@ func doInit(repoRoot string, force bool, nBitsForKeypair int) (interface{}, erro
|
||||
if err := fsrepo.Init(repoRoot, conf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := repo.ConfigureEventLogger(conf.Logs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = addTheWelcomeFile(repoRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -196,11 +191,6 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logConfig, err := initLogs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bootstrapPeers, err := corecmds.DefaultBootstrapPeers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -220,7 +210,6 @@ func initConfig(nBitsForKeypair int) (*config.Config, error) {
|
||||
|
||||
Bootstrap: bootstrapPeers,
|
||||
Datastore: *ds,
|
||||
Logs: *logConfig,
|
||||
Identity: identity,
|
||||
|
||||
// setup the node mount points.
|
||||
@ -268,15 +257,3 @@ func identityConfig(nbits int) (config.Identity, error) {
|
||||
fmt.Printf("peer identity: %s\n", ident.PeerID)
|
||||
return ident, nil
|
||||
}
|
||||
|
||||
// initLogs initializes the event logger.
|
||||
func initLogs() (*config.Logs, error) {
|
||||
logpath, err := config.LogsPath("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conf := config.Logs{
|
||||
Filename: path.Join(logpath, "events.log"),
|
||||
}
|
||||
return &conf, nil
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import (
|
||||
cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
|
||||
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
|
||||
core "github.com/jbenet/go-ipfs/core"
|
||||
repo "github.com/jbenet/go-ipfs/repo"
|
||||
config "github.com/jbenet/go-ipfs/repo/config"
|
||||
fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
|
||||
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||
@ -282,17 +281,6 @@ func callPreCommandHooks(ctx context.Context, details cmdDetails, req cmds.Reque
|
||||
}
|
||||
}
|
||||
|
||||
// When the upcoming command may use the config and repo, we know it's safe
|
||||
// for the log config hook to touch the config/repo
|
||||
if repo.IsInitialized(req.Context().ConfigRoot) {
|
||||
log.Debug("Calling hook: Configure Event Logger")
|
||||
cfg, err := req.Context().GetConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo.ConfigureEventLogger(cfg.Logs)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,6 @@ type Config struct {
|
||||
Version Version // local node's version management
|
||||
Bootstrap []BootstrapPeer // local nodes's bootstrap peers
|
||||
Tour Tour // local node's tour position
|
||||
Logs Logs // local node's event log configuration
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -1,18 +1 @@
|
||||
package config
|
||||
|
||||
// LogsDefaultDirectory is the directory to store all IPFS event logs.
|
||||
var LogsDefaultDirectory = "logs"
|
||||
|
||||
// Logs tracks the configuration of the event logger
|
||||
type Logs struct {
|
||||
Filename string
|
||||
MaxSizeMB uint64
|
||||
MaxBackups uint64
|
||||
MaxAgeDays uint64
|
||||
}
|
||||
|
||||
// LogsPath returns the default path for event logs given a configuration root
|
||||
// (set an empty string to have the default configuration root)
|
||||
func LogsPath(configroot string) (string, error) {
|
||||
return Path(configroot, LogsDefaultDirectory)
|
||||
}
|
||||
|
51
repo/fsrepo/component/eventlog.go
Normal file
51
repo/fsrepo/component/eventlog.go
Normal file
@ -0,0 +1,51 @@
|
||||
package component
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
config "github.com/jbenet/go-ipfs/repo/config"
|
||||
dir "github.com/jbenet/go-ipfs/thirdparty/dir"
|
||||
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||
)
|
||||
|
||||
func InitEventlogComponent(repoPath string, conf *config.Config) error {
|
||||
if err := dir.Writable(path.Join(repoPath, "logs")); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func EventlogComponentIsInitialized(path string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type EventlogComponent struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (c *EventlogComponent) SetPath(path string) {
|
||||
c.path = path // FIXME necessary?
|
||||
}
|
||||
|
||||
func (c *EventlogComponent) Close() error {
|
||||
// TODO It isn't part of the current contract, but callers may like for us
|
||||
// to disable logging once the component is closed.
|
||||
eventlog.Configure(eventlog.Output(os.Stderr))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *EventlogComponent) Open() error {
|
||||
// log.Debugf("writing eventlogs to ...", c.path)
|
||||
return configureEventLoggerAtRepoPath(c.path)
|
||||
}
|
||||
|
||||
func configureEventLoggerAtRepoPath(repoPath string) error {
|
||||
eventlog.Configure(eventlog.LevelInfo)
|
||||
eventlog.Configure(eventlog.LdJSONFormatter)
|
||||
rotateConf := eventlog.LogRotatorConfig{
|
||||
Filename: path.Join(repoPath, "logs", "events.log"),
|
||||
}
|
||||
eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
|
||||
return nil
|
||||
}
|
@ -54,6 +54,7 @@ type FSRepo struct {
|
||||
// TODO test
|
||||
configComponent component.ConfigComponent
|
||||
datastoreComponent component.DatastoreComponent
|
||||
eventlogComponent component.EventlogComponent
|
||||
}
|
||||
|
||||
type componentBuilder struct {
|
||||
@ -163,14 +164,6 @@ func (r *FSRepo) Open() error {
|
||||
}
|
||||
}
|
||||
|
||||
logpath, err := config.LogsPath("")
|
||||
if err != nil {
|
||||
return debugerror.Wrap(err)
|
||||
}
|
||||
if err := dir.Writable(logpath); err != nil {
|
||||
return debugerror.Errorf("logs: %s", err)
|
||||
}
|
||||
|
||||
return r.transitionToOpened()
|
||||
}
|
||||
|
||||
@ -352,5 +345,20 @@ func componentBuilders() []componentBuilder {
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
||||
// EventlogComponent
|
||||
componentBuilder{
|
||||
Init: component.InitEventlogComponent,
|
||||
IsInitialized: component.EventlogComponentIsInitialized,
|
||||
OpenHandler: func(r *FSRepo) error {
|
||||
c := component.EventlogComponent{}
|
||||
c.SetPath(r.path)
|
||||
if err := c.Open(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.eventlogComponent = c
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
24
repo/logs.go
24
repo/logs.go
@ -1,24 +0,0 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
config "github.com/jbenet/go-ipfs/repo/config"
|
||||
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||
util "github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
func ConfigureEventLogger(config config.Logs) error {
|
||||
if util.Debug {
|
||||
eventlog.Configure(eventlog.LevelDebug)
|
||||
} else {
|
||||
eventlog.Configure(eventlog.LevelInfo)
|
||||
}
|
||||
eventlog.Configure(eventlog.LdJSONFormatter)
|
||||
rotateConf := eventlog.LogRotatorConfig{
|
||||
Filename: config.Filename,
|
||||
MaxSizeMB: config.MaxSizeMB,
|
||||
MaxBackups: config.MaxBackups,
|
||||
MaxAgeDays: config.MaxAgeDays,
|
||||
}
|
||||
eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf))
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user