mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 22:49:13 +08:00
feat(fsrepo) add eventlog config to repo/config struct
This commit is contained in:
@ -24,6 +24,7 @@ type Config struct {
|
|||||||
Bootstrap []string // local nodes's bootstrap peer addresses
|
Bootstrap []string // local nodes's bootstrap peer addresses
|
||||||
Tour Tour // local node's tour position
|
Tour Tour // local node's tour position
|
||||||
Gateway Gateway // local node's gateway server options
|
Gateway Gateway // local node's gateway server options
|
||||||
|
Log Log
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
8
repo/config/log.go
Normal file
8
repo/config/log.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
|
||||||
|
type Log struct {
|
||||||
|
MaxSizeMB uint64
|
||||||
|
MaxBackups uint64
|
||||||
|
MaxAgeDays uint64
|
||||||
|
}
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Component interface {
|
type Component interface {
|
||||||
Open() error
|
Open(*config.Config) error
|
||||||
io.Closer
|
io.Closer
|
||||||
SetPath(string)
|
SetPath(string)
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,10 @@ func InitConfigComponent(path string, conf *config.Config) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open returns an error if the config file is not present.
|
// Open returns an error if the config file is not present. This component is
|
||||||
func (c *ConfigComponent) Open() error {
|
// always called with a nil config parameter. Other components rely on the
|
||||||
|
// config, to keep the interface uniform, it is special-cased.
|
||||||
|
func (c *ConfigComponent) Open(_ *config.Config) error {
|
||||||
configFilename, err := config.Filename(c.path)
|
configFilename, err := config.Filename(c.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -66,7 +66,7 @@ func (dsc *DatastoreComponent) SetPath(p string) {
|
|||||||
func (dsc *DatastoreComponent) Datastore() datastore.ThreadSafeDatastore { return dsc.ds }
|
func (dsc *DatastoreComponent) Datastore() datastore.ThreadSafeDatastore { return dsc.ds }
|
||||||
|
|
||||||
// Open returns an error if the config file is not present.
|
// Open returns an error if the config file is not present.
|
||||||
func (dsc *DatastoreComponent) Open() error {
|
func (dsc *DatastoreComponent) Open(*config.Config) error {
|
||||||
|
|
||||||
dsLock.Lock()
|
dsLock.Lock()
|
||||||
defer dsLock.Unlock()
|
defer dsLock.Unlock()
|
||||||
|
@ -22,8 +22,8 @@ func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
|
|||||||
path := testRepoPath(t)
|
path := testRepoPath(t)
|
||||||
dsc1 := DatastoreComponent{path: path}
|
dsc1 := DatastoreComponent{path: path}
|
||||||
dsc2 := DatastoreComponent{path: path}
|
dsc2 := DatastoreComponent{path: path}
|
||||||
assert.Nil(dsc1.Open(), t, "first repo should open successfully")
|
assert.Nil(dsc1.Open(nil), t, "first repo should open successfully")
|
||||||
assert.Nil(dsc2.Open(), t, "second repo should open successfully")
|
assert.Nil(dsc2.Open(nil), t, "second repo should open successfully")
|
||||||
|
|
||||||
assert.Nil(dsc1.Close(), t)
|
assert.Nil(dsc1.Close(), t)
|
||||||
assert.Nil(dsc2.Close(), t)
|
assert.Nil(dsc2.Close(), t)
|
||||||
|
@ -35,17 +35,22 @@ func (c *EventlogComponent) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventlogComponent) Open() error {
|
func (c *EventlogComponent) Open(config *config.Config) error {
|
||||||
// log.Debugf("writing eventlogs to ...", c.path)
|
// log.Debugf("writing eventlogs to ...", c.path)
|
||||||
return configureEventLoggerAtRepoPath(c.path)
|
return configureEventLoggerAtRepoPath(config, c.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureEventLoggerAtRepoPath(repoPath string) error {
|
func configureEventLoggerAtRepoPath(c *config.Config, repoPath string) error {
|
||||||
eventlog.Configure(eventlog.LevelInfo)
|
eventlog.Configure(eventlog.LevelInfo)
|
||||||
eventlog.Configure(eventlog.LdJSONFormatter)
|
eventlog.Configure(eventlog.LdJSONFormatter)
|
||||||
rotateConf := eventlog.LogRotatorConfig{
|
rotateConf := eventlog.LogRotatorConfig{
|
||||||
Filename: path.Join(repoPath, "logs", "events.log"),
|
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.OutputRotatingLogFile(rotateConf))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Component = &EventlogComponent{}
|
||||||
|
@ -316,14 +316,15 @@ func (r *FSRepo) components() []component.Component {
|
|||||||
func componentBuilders() []componentBuilder {
|
func componentBuilders() []componentBuilder {
|
||||||
return []componentBuilder{
|
return []componentBuilder{
|
||||||
|
|
||||||
// ConfigComponent
|
// ConfigComponent must be initialized first because other components
|
||||||
|
// depend on it.
|
||||||
componentBuilder{
|
componentBuilder{
|
||||||
Init: component.InitConfigComponent,
|
Init: component.InitConfigComponent,
|
||||||
IsInitialized: component.ConfigComponentIsInitialized,
|
IsInitialized: component.ConfigComponentIsInitialized,
|
||||||
OpenHandler: func(r *FSRepo) error {
|
OpenHandler: func(r *FSRepo) error {
|
||||||
c := component.ConfigComponent{}
|
c := component.ConfigComponent{}
|
||||||
c.SetPath(r.path)
|
c.SetPath(r.path)
|
||||||
if err := c.Open(); err != nil {
|
if err := c.Open(nil); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.configComponent = c
|
r.configComponent = c
|
||||||
@ -338,7 +339,7 @@ func componentBuilders() []componentBuilder {
|
|||||||
OpenHandler: func(r *FSRepo) error {
|
OpenHandler: func(r *FSRepo) error {
|
||||||
c := component.DatastoreComponent{}
|
c := component.DatastoreComponent{}
|
||||||
c.SetPath(r.path)
|
c.SetPath(r.path)
|
||||||
if err := c.Open(); err != nil {
|
if err := c.Open(r.configComponent.Config()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.datastoreComponent = c
|
r.datastoreComponent = c
|
||||||
@ -353,7 +354,7 @@ func componentBuilders() []componentBuilder {
|
|||||||
OpenHandler: func(r *FSRepo) error {
|
OpenHandler: func(r *FSRepo) error {
|
||||||
c := component.EventlogComponent{}
|
c := component.EventlogComponent{}
|
||||||
c.SetPath(r.path)
|
c.SetPath(r.path)
|
||||||
if err := c.Open(); err != nil {
|
if err := c.Open(r.configComponent.Config()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.eventlogComponent = c
|
r.eventlogComponent = c
|
||||||
|
Reference in New Issue
Block a user