Skip to content

Commit

Permalink
Merge pull request #1365 from mysteriumnetwork/1290-log-classifications
Browse files Browse the repository at this point in the history
Improve logging classifications
  • Loading branch information
tadaskay authored Oct 25, 2019
2 parents 1ab12f7 + 9d5b9ef commit c883af1
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 1,424 deletions.
1,136 changes: 0 additions & 1,136 deletions Gopkg.lock

This file was deleted.

174 changes: 0 additions & 174 deletions Gopkg.toml

This file was deleted.

3 changes: 2 additions & 1 deletion cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type Dependencies struct {

// Bootstrap initiates all container dependencies
func (di *Dependencies) Bootstrap(nodeOptions node.Options) error {
logconfig.BootstrapWith(&nodeOptions.LogOptions)
logconfig.Configure(&nodeOptions.LogOptions)
nats_discovery.Bootstrap()

log.Info().Msg("Starting Mysterium Node " + metadata.VersionAsString())
Expand Down Expand Up @@ -255,6 +255,7 @@ func (di *Dependencies) Bootstrap(nodeOptions node.Options) error {

appconfig.Current.EnableEventPublishing(di.EventBus)

log.Info().Msg("Mysterium node started!")
return nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/mysterium_node/mysterium_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
)

func main() {
logconfig.Bootstrap()
app, err := NewCommand()
if err != nil {
log.Error().Err(err).Msg("Failed to create command: ")
Expand Down
46 changes: 26 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ import (
"github.com/spf13/cast"
)

// Topic returns event bus topic for the given config key to listen for its updates
// Topic returns event bus topic for the given config key to listen for its updates.
func Topic(configKey string) string {
return "config:" + configKey
}

// Config stores app configuration: default values + user configuration + CLI flags
// Config stores application configuration in 3 separate maps (listed from the lowest priority to the highest):
//
// • Default values
//
// • User configuration (config.toml)
//
// • CLI flags
type Config struct {
userConfigLocation string
defaults map[string]interface{}
Expand All @@ -43,14 +49,14 @@ type Config struct {
eventBus eventbus.EventBus
}

// Current global configuration instance
// Current global configuration instance.
var Current *Config

func init() {
Current = NewConfig()
}

// NewConfig creates a new configuration instance
// NewConfig creates a new configuration instance.
func NewConfig() *Config {
return &Config{
userConfigLocation: "",
Expand All @@ -64,12 +70,12 @@ func (cfg *Config) userConfigLoaded() bool {
return cfg.userConfigLocation != ""
}

// EnableEventPublishing enables config event publishing to the event bus
// EnableEventPublishing enables config event publishing to the event bus.
func (cfg *Config) EnableEventPublishing(eb eventbus.EventBus) {
cfg.eventBus = eb
}

// LoadUserConfig loads and remembers user config location
// LoadUserConfig loads and remembers user config location.
func (cfg *Config) LoadUserConfig(location string) error {
log.Debug().Msg("Loading user configuration: " + location)
cfg.userConfigLocation = location
Expand All @@ -81,11 +87,11 @@ func (cfg *Config) LoadUserConfig(location string) error {
if err != nil {
return err
}
log.Info().Msg("user configuration loaded: \n" + cfgJson)
log.Info().Msg("User configuration loaded: \n" + cfgJson)
return nil
}

// SaveUserConfig saves user configuration to the file from which it was loaded
// SaveUserConfig saves user configuration to the file from which it was loaded.
func (cfg *Config) SaveUserConfig() error {
log.Info().Msg("Saving user configuration")
if !cfg.userConfigLoaded() {
Expand All @@ -108,40 +114,40 @@ func (cfg *Config) SaveUserConfig() error {
return nil
}

// GetUserConfig returns user configuration
// GetUserConfig returns user configuration.
func (cfg *Config) GetUserConfig() map[string]interface{} {
return cfg.user
}

// SetDefault sets default value for key
// SetDefault sets default value for key.
func (cfg *Config) SetDefault(key string, value interface{}) {
cfg.set(&cfg.defaults, key, value)
}

// SetUser sets user configuration value for key
// SetUser sets user configuration value for key.
func (cfg *Config) SetUser(key string, value interface{}) {
if cfg.eventBus != nil {
cfg.eventBus.Publish(Topic(key), value)
}
cfg.set(&cfg.user, key, value)
}

// SetCLI sets value passed via CLI flag for key
// SetCLI sets value passed via CLI flag for key.
func (cfg *Config) SetCLI(key string, value interface{}) {
cfg.set(&cfg.cli, key, value)
}

// RemoveUser removes user configuration value for key
// RemoveUser removes user configuration value for key.
func (cfg *Config) RemoveUser(key string) {
cfg.remove(&cfg.user, key)
}

// RemoveCLI removes configured CLI flag value by key
// RemoveCLI removes configured CLI flag value by key.
func (cfg *Config) RemoveCLI(key string) {
cfg.remove(&cfg.cli, key)
}

// set internal method for setting value in a certain configuration value map
// set sets value to a particular configuration value map.
func (cfg *Config) set(configMap *map[string]interface{}, key string, value interface{}) {
key = strings.ToLower(key)
segments := strings.Split(key, ".")
Expand All @@ -153,7 +159,7 @@ func (cfg *Config) set(configMap *map[string]interface{}, key string, value inte
deepestMap[lastKey] = value
}

// remove internal method for removing a configured value in a certain configuration map
// remove removes a configured value from a particular configuration map.
func (cfg *Config) remove(configMap *map[string]interface{}, key string) {
key = strings.ToLower(key)
segments := strings.Split(key, ".")
Expand All @@ -165,7 +171,7 @@ func (cfg *Config) remove(configMap *map[string]interface{}, key string) {
delete(deepestMap, lastKey)
}

// Get gets stored config value as-is
// Get returns stored config value as-is.
func (cfg *Config) Get(key string) interface{} {
segments := strings.Split(strings.ToLower(key), ".")
cliValue := cfg.searchMap(cfg.cli, segments)
Expand All @@ -183,17 +189,17 @@ func (cfg *Config) Get(key string) interface{} {
return defaultValue
}

// GetInt gets config value as int
// GetInt returns config value as int.
func (cfg *Config) GetInt(key string) int {
return cast.ToInt(cfg.Get(key))
}

// GetString gets config value as string
// GetString returns config value as string.
func (cfg *Config) GetString(key string) string {
return cast.ToString(cfg.Get(key))
}

// GetBool gets config value as bool
// GetBool returns config value as bool.
func (cfg *Config) GetBool(key string) bool {
return cast.ToBool(cfg.Get(key))
}
2 changes: 1 addition & 1 deletion config/urfavecli/clicontext/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

// LoadUserConfig determines config location from the context
// and makes sure that the config file actually exists, creating it if necessary
// and makes sure that the config file actually exists, creating it if necessary.
func LoadUserConfig(ctx *cli.Context) error {
configDir, configFilePath := resolveLocation(ctx)
err := createDirIfNotExists(configDir)
Expand Down
2 changes: 1 addition & 1 deletion config/urfavecli/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Package urfavecli/cliflags is an adapter to load configuration from urfave/cli.v1 flags
// Package urfavecli/cliflags is an adapter to load configuration from urfave/cli.v1 flags.

package cliflags

Expand Down
14 changes: 6 additions & 8 deletions firewall/noop_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ func (nb NoopVendor) Reset() {
// BlockOutgoingTraffic just logs the call
func (nb NoopVendor) BlockOutgoingTraffic() (RemoveRule, error) {
log.Info().Msg("Outgoing traffic block requested")
return nb.logRemoval("Outgoing traffic block removed"), nil
return func() {
log.Info().Msg("Outgoing traffic block removed")
}, nil
}

// AllowIPAccess logs ip for which access was requested
// AllowIPAccess logs IP for which access was requested
func (nb NoopVendor) AllowIPAccess(ip string) (RemoveRule, error) {
log.Info().Msgf("Allow %s access", ip)
return nb.logRemoval("Rule for ip: ", ip, " removed"), nil
}

func (nb NoopVendor) logRemoval(vals ...interface{}) RemoveRule {
return func() {
log.Info().Msgf("%+v", vals...)
}
log.Info().Msgf("Rule for IP: %s removed", ip)
}, nil
}

var _ Vendor = NoopVendor{}
Loading

0 comments on commit c883af1

Please sign in to comment.