From 2c38487eb04867d221067956c2aa35931d1795c7 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sat, 22 Nov 2014 23:22:21 -0800 Subject: [PATCH 1/3] fix(eventlog) configure logging if repo is initialized License: MIT Signed-off-by: Brian Tiger Chow --- cmd/ipfs/main.go | 3 ++- repo/repo.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 repo/repo.go diff --git a/cmd/ipfs/main.go b/cmd/ipfs/main.go index 991385435..0ca8044f2 100644 --- a/cmd/ipfs/main.go +++ b/cmd/ipfs/main.go @@ -24,6 +24,7 @@ import ( u "github.com/jbenet/go-ipfs/util" "github.com/jbenet/go-ipfs/util/debugerror" eventlog "github.com/jbenet/go-ipfs/util/eventlog" + repo "github.com/jbenet/go-ipfs/repo" ) // log is the command logger @@ -271,7 +272,7 @@ func callPreCommandHooks(details cmdDetails, req cmds.Request, root *cmds.Comman // 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 details.usesConfigAsInput() && details.usesRepo() { + if repo.IsInitialized(req.Context().ConfigRoot) { log.Debug("Calling hook: Configure Event Logger") cfg, err := req.Context().GetConfig() if err != nil { diff --git a/repo/repo.go b/repo/repo.go new file mode 100644 index 000000000..6d702d9ac --- /dev/null +++ b/repo/repo.go @@ -0,0 +1,15 @@ +package repo + +import util "github.com/jbenet/go-ipfs/util" + +// IsInitialized returns true if the path is home to an initialized IPFS +// repository. +func IsInitialized(path string) bool { + if !util.FileExists(path) { + return false + } + // TODO add logging check + // TODO add datastore check + // TODO add config file check + return true +} From 4d497f1c6ecfe156b33c9558eb10e1c224b49175 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sat, 22 Nov 2014 23:41:47 -0800 Subject: [PATCH 2/3] refactor(repo) mv ConfigureLogger to repo package License: MIT Signed-off-by: Brian Tiger Chow Conflicts: cmd/ipfs/main.go --- cmd/ipfs/main.go | 23 +---------------------- repo/logs.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 repo/logs.go diff --git a/cmd/ipfs/main.go b/cmd/ipfs/main.go index 0ca8044f2..8a30a9a71 100644 --- a/cmd/ipfs/main.go +++ b/cmd/ipfs/main.go @@ -278,7 +278,7 @@ func callPreCommandHooks(details cmdDetails, req cmds.Request, root *cmds.Comman if err != nil { return err } - configureEventLogger(cfg) + repo.ConfigureEventLogger(cfg.Logs) } return nil @@ -510,24 +510,3 @@ func allInterruptSignals() chan os.Signal { syscall.SIGTERM, syscall.SIGQUIT) return sigc } - -func configureEventLogger(config *config.Config) error { - - if u.Debug { - eventlog.Configure(eventlog.LevelDebug) - } else { - eventlog.Configure(eventlog.LevelInfo) - } - - eventlog.Configure(eventlog.LdJSONFormatter) - - rotateConf := eventlog.LogRotatorConfig{ - Filename: config.Logs.Filename, - MaxSizeMB: config.Logs.MaxSizeMB, - MaxBackups: config.Logs.MaxBackups, - MaxAgeDays: config.Logs.MaxAgeDays, - } - - eventlog.Configure(eventlog.OutputRotatingLogFile(rotateConf)) - return nil -} diff --git a/repo/logs.go b/repo/logs.go new file mode 100644 index 000000000..0ec141eee --- /dev/null +++ b/repo/logs.go @@ -0,0 +1,28 @@ +package repo + +import ( + util "github.com/jbenet/go-ipfs/util" + eventlog "github.com/jbenet/go-ipfs/util/eventlog" + config "github.com/jbenet/go-ipfs/config" +) + +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 +} From 90ed1482092d7a3ce161a63ae8a0166f2b1101da Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sun, 23 Nov 2014 17:09:18 -0800 Subject: [PATCH 3/3] fix(eventlog) apply settings upon initialization This commit applies the logging settings to the logging system upon initialization `ipfs init` and `ipfs daemon --init`. License: MIT Signed-off-by: Brian Tiger Chow --- cmd/ipfs/init.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 936342592..a8b6760c7 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -16,7 +16,8 @@ import ( chunk "github.com/jbenet/go-ipfs/importer/chunk" peer "github.com/jbenet/go-ipfs/peer" u "github.com/jbenet/go-ipfs/util" - "github.com/jbenet/go-ipfs/util/debugerror" + debugerror "github.com/jbenet/go-ipfs/util/debugerror" + repo "github.com/jbenet/go-ipfs/repo" ) const nBitsForKeypairDefault = 4096 @@ -251,6 +252,8 @@ func identityConfig(nbits int) (config.Identity, error) { return ident, nil } +// initLogs initializes the event logger at the specified path. It uses the +// default log path if no path is provided. func initLogs(logpath string) (config.Logs, error) { if len(logpath) == 0 { var err error @@ -259,15 +262,18 @@ func initLogs(logpath string) (config.Logs, error) { return config.Logs{}, debugerror.Wrap(err) } } - err := initCheckDir(logpath) if err != nil { return config.Logs{}, debugerror.Errorf("logs: %s", err) } - - return config.Logs{ + conf := config.Logs{ Filename: path.Join(logpath, "events.log"), - }, nil + } + err = repo.ConfigureEventLogger(conf) + if err != nil { + return conf, err + } + return conf, nil } // initCheckDir ensures the directory exists and is writable