1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 09:52:20 +08:00

feat(fsrepo) add eventlog config to repo/config struct

This commit is contained in:
Brian Tiger Chow
2015-02-02 17:42:54 -08:00
parent d0583768d1
commit 454cd454ea
8 changed files with 31 additions and 14 deletions

View File

@ -24,6 +24,7 @@ type Config struct {
Bootstrap []string // local nodes's bootstrap peer addresses
Tour Tour // local node's tour position
Gateway Gateway // local node's gateway server options
Log Log
}
const (

8
repo/config/log.go Normal file
View File

@ -0,0 +1,8 @@
package config
type Log struct {
MaxSizeMB uint64
MaxBackups uint64
MaxAgeDays uint64
}

View File

@ -7,7 +7,7 @@ import (
)
type Component interface {
Open() error
Open(*config.Config) error
io.Closer
SetPath(string)
}

View File

@ -37,8 +37,10 @@ func InitConfigComponent(path string, conf *config.Config) error {
return nil
}
// Open returns an error if the config file is not present.
func (c *ConfigComponent) Open() error {
// Open returns an error if the config file is not present. This component is
// 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)
if err != nil {
return err

View File

@ -66,7 +66,7 @@ func (dsc *DatastoreComponent) SetPath(p string) {
func (dsc *DatastoreComponent) Datastore() datastore.ThreadSafeDatastore { return dsc.ds }
// 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()
defer dsLock.Unlock()

View File

@ -22,8 +22,8 @@ func TestOpenMoreThanOnceInSameProcess(t *testing.T) {
path := testRepoPath(t)
dsc1 := DatastoreComponent{path: path}
dsc2 := DatastoreComponent{path: path}
assert.Nil(dsc1.Open(), t, "first repo should open successfully")
assert.Nil(dsc2.Open(), t, "second repo should open successfully")
assert.Nil(dsc1.Open(nil), t, "first repo should open successfully")
assert.Nil(dsc2.Open(nil), t, "second repo should open successfully")
assert.Nil(dsc1.Close(), t)
assert.Nil(dsc2.Close(), t)

View File

@ -35,17 +35,22 @@ func (c *EventlogComponent) Close() error {
return nil
}
func (c *EventlogComponent) Open() error {
func (c *EventlogComponent) Open(config *config.Config) error {
// 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.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))
return nil
}
var _ Component = &EventlogComponent{}

View File

@ -316,14 +316,15 @@ func (r *FSRepo) components() []component.Component {
func componentBuilders() []componentBuilder {
return []componentBuilder{
// ConfigComponent
// ConfigComponent must be initialized first because other components
// depend on it.
componentBuilder{
Init: component.InitConfigComponent,
IsInitialized: component.ConfigComponentIsInitialized,
OpenHandler: func(r *FSRepo) error {
c := component.ConfigComponent{}
c.SetPath(r.path)
if err := c.Open(); err != nil {
if err := c.Open(nil); err != nil {
return err
}
r.configComponent = c
@ -338,7 +339,7 @@ func componentBuilders() []componentBuilder {
OpenHandler: func(r *FSRepo) error {
c := component.DatastoreComponent{}
c.SetPath(r.path)
if err := c.Open(); err != nil {
if err := c.Open(r.configComponent.Config()); err != nil {
return err
}
r.datastoreComponent = c
@ -353,7 +354,7 @@ func componentBuilders() []componentBuilder {
OpenHandler: func(r *FSRepo) error {
c := component.EventlogComponent{}
c.SetPath(r.path)
if err := c.Open(); err != nil {
if err := c.Open(r.configComponent.Config()); err != nil {
return err
}
r.eventlogComponent = c