From f3d07b4ad2d83fcbc3fa3ceac7b7a41a82333681 Mon Sep 17 00:00:00 2001 From: Daniil Lashin Date: Thu, 15 Nov 2018 12:07:50 +0300 Subject: [PATCH] Add logs destination to config #133 --- config/config.go | 3 +++ config/toml.go | 3 +++ log/log.go | 23 +++++++++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 26912798e..1f4f6e258 100644 --- a/config/config.go +++ b/config/config.go @@ -237,6 +237,8 @@ type BaseConfig struct { APIPerIPLimit int `mapstructure:"api_per_ip_limit"` APIPerIPLimitWindow time.Duration `mapstructure:"api_per_ip_limit_window"` + + LogPath string `mapstructure:"log_path"` } // DefaultBaseConfig returns a default base configuration for a Tendermint node @@ -261,6 +263,7 @@ func DefaultBaseConfig() BaseConfig { APISimultaneousRequests: 100, APIPerIPLimit: 1000, APIPerIPLimitWindow: 60 * time.Second, + LogPath: "stdout", } } diff --git a/config/toml.go b/config/toml.go index 219d3a574..cbcb68d9d 100644 --- a/config/toml.go +++ b/config/toml.go @@ -102,6 +102,9 @@ db_path = "{{ js .BaseConfig.DBPath }}" # Output level for logging, including package level options log_level = "{{ .BaseConfig.LogLevel }}" +# Path to file for logs, "stdout" by default +log_path = "{{ .BaseConfig.LogPath }}" + ##### additional base config options ##### # Path to the JSON file containing the private key to use as a validator in the consensus protocol diff --git a/log/log.go b/log/log.go index 8ee9ecb5d..35f2ebdc5 100644 --- a/log/log.go +++ b/log/log.go @@ -13,8 +13,27 @@ var ( ) func init() { - logger, _ := flags.ParseLogLevel(cfg.LogLevel, log.NewTMLogger(os.Stdout), "info") - SetLogger(logger) + var l log.Logger + + if cfg.LogPath != "stdout" { + file, err := os.OpenFile(cfg.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + + if err != nil { + panic(err) + } + + l = log.NewTMLogger(file) + } else { + l = log.NewTMLogger(os.Stdout) + } + + l, err := flags.ParseLogLevel(cfg.LogLevel, l, "info") + + if err != nil { + panic(err) + } + + SetLogger(l) } func SetLogger(l log.Logger) {