提交 edb88dd9 作者: Brian Tiger Chow

Merge pull request #731 from jbenet/feat/repo-component-eventlog-config

Allow FSRepo components to depend on the config, add log config to eventlog
......@@ -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 (
......
......@@ -41,6 +41,9 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
Bootstrap: BootstrapPeerStrings(bootstrapPeers),
Datastore: *ds,
Identity: identity,
Log: Log{
MaxSizeMB: 500,
},
// setup the node mount points.
Mounts: Mounts{
......@@ -54,7 +57,7 @@ func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
Gateway: Gateway{
RootRedirect: "",
Writable: false,
Writable: false,
},
}
......
package config
type Log struct {
MaxSizeMB uint64
MaxBackups uint64
MaxAgeDays uint64
}
......@@ -7,7 +7,7 @@ import (
)
type Component interface {
Open() error
Open(*config.Config) error
io.Closer
SetPath(string)
}
......
......@@ -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
......
......@@ -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()
......
......@@ -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)
......
......@@ -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"),
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{}
......@@ -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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论