From ac69aba56188280bcc09cb4c6188dcee8426d06e Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sun, 16 Nov 2014 03:58:13 -0800 Subject: [PATCH] feat(2/main) configure event logger License: MIT Signed-off-by: Brian Tiger Chow # TYPES # feat # fix # docs # style (formatting, missing semi colons, etc; no code change): # refactor # test (adding missing tests, refactoring tests; no production code change) # chore (updating grunt tasks etc; no production code change) Signed-off-by: Brian Tiger Chow --- cmd/ipfs2/main.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go index ce62a791f..52873dfe1 100644 --- a/cmd/ipfs2/main.go +++ b/cmd/ipfs2/main.go @@ -232,6 +232,17 @@ func callPreCommandHooks(command 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 command.usesConfigAsInput() && command.usesRepo() { + log.Debug("Calling hook: Configure Event Logger") + cfg, err := req.Context().GetConfig() + if err != nil { + return err + } + configureEventLogger(cfg) + } + return nil } @@ -481,3 +492,24 @@ func allInterruptSignals() chan os.Signal { syscall.SIGTERM, syscall.SIGQUIT) return sigc } + +func configureEventLogger(config *config.Config) error { + + if u.Debug { + elog.Configure(elog.LevelDebug) + } else { + elog.Configure(elog.LevelInfo) + } + + elog.Configure(elog.LdJSONFormatter) + + rotateConf := elog.LogRotatorConfig{ + Filename: config.Logs.Filename, + MaxSizeMB: config.Logs.MaxSizeMB, + MaxBackups: config.Logs.MaxBackups, + MaxAgeDays: config.Logs.MaxAgeDays, + } + + elog.Configure(elog.OutputRotatingLogFile(rotateConf)) + return nil +}