~bayindirh/nudge

ebec628e66b3d01ad0083f8dd938938c038addbc — Hakan Bayindir 4 months ago 4ddd594
refactor: Remove logging level support from conf.

- refactor: Remove logging level support from configuration file.
- refactor: Remove logging relates examples from “nudge.conf.example” file.
- chore: Run through “gofmt”.
3 files changed, 13 insertions(+), 24 deletions(-)

M CHANGELOG.md
M src/conf/nudge.conf.example
M src/nudge.go
M CHANGELOG.md => CHANGELOG.md +2 -0
@@ 4,6 4,8 @@

## 2023-10-16

- refactor: Remove logging level support from configuration file.
- refactor: Remove logging relates examples from `nudge.conf.example`.
- refactor: Logging file path support removed from `runTimeConfiguration` struct and `readAndApplyConfiguration` function.
- refactor: Remove a couple of information lines from `printState` function. Because logging file support is going away.
- refactor: Move logger initialization to the beginning of the process.

M src/conf/nudge.conf.example => src/conf/nudge.conf.example +0 -7
@@ 6,10 6,3 @@
# Both keys should be 30 characters long.
api_key  = '!-CHANGE-WITH-API-KEY-CAFEBABE'
user_key = '!CHANGE-WITH-USER-KEY-CAFED00D'

[logging]
# For more details using logging_paths, see https://pkg.go.dev/go.uber.org/zap#example-package-BasicConfiguration
logging_paths  = ['stdout', '/var/log/nudge.log']
# A default, fairly verbose log level to see what the code is doing.
# NOTE: Log levels are case sensitive.
log_level = 'info'

M src/nudge.go => src/nudge.go +11 -17
@@ 51,13 51,13 @@ type Notification struct {

// This struct contains the configuration information we are going to use during execution.
type RuntimeConfiguration struct {
	applicationName string   // Stores the sanitized version of the application name.
	version         string   // Stores the version of the application.
	dryrun          bool     // Setting to true will make code switch to "simulation" mode.
	configFilePath  string   // This variable stores our configuration file's path.
	apiKey          string   // Secret key we use for sending messages.
	userKey         string   // Secret key of the user, used as a recipient address.
	logLevel        string   // Contains the logging level the application starts with.
	applicationName string // Stores the sanitized version of the application name.
	version         string // Stores the version of the application.
	dryrun          bool   // Setting to true will make code switch to "simulation" mode.
	configFilePath  string // This variable stores our configuration file's path.
	apiKey          string // Secret key we use for sending messages.
	userKey         string // Secret key of the user, used as a recipient address.
	logLevel        string // Contains the logging level the application starts with.
}

// Following struct is for temporarily storing flag values passed during program invocation.


@@ 167,8 167,8 @@ func readAndApplyConfiguration(configurationFilePath *string, runtimeConfigurati
	if err != nil {
		logger.Debugf("Cannot find any configuration file, continuing using built-in defaults (%s).", err.Error())
	} else {

		// If we've reached here, we can take note of the file path now.
		// This also means that we have read the config file successfully.
		runtimeConfiguration.configFilePath = viper.ConfigFileUsed()

		// Let the user know where the config file is.


@@ 186,12 186,6 @@ func readAndApplyConfiguration(configurationFilePath *string, runtimeConfigurati
			runtimeConfiguration.userKey = viper.GetString("pushover.user_key")
			logger.Debugf("Pushover User key is found & set to %s.", runtimeConfiguration.userKey)
		}

		// Logging level:
		if viper.IsSet("logging.log_level") {
			runtimeConfiguration.logLevel = viper.GetString("logging.log_level")
			logger.Debugf("Logging level is found and set to %s.", runtimeConfiguration.logLevel)
		}
	}
}



@@ 383,8 377,8 @@ func main() {

	// Since there's no easy way to check whether a flag is set, I need a list of set flags.
	var setFlags []string
	
	// Initialize Zap logger once and for all here. Because except log levels, Zap 

	// Initialize Zap logger once and for all here. Because except log levels, Zap
	// doesn't support reconfiguration. For a completely reconfigurable variant,
	// there's Thales' flume (https://github.com/ThalesGroup/flume)
	zapDefaultConfigJSON := []byte(`{


@@ 432,7 426,7 @@ func main() {

	// Next, apply the defaults:
	applyDefaultConfiguration(&runtimeConfiguration, &notificationToSend)
	

	// Then let's see what we have at hand (options, parameters, flags).
	// Flags are parsed first, stored in a secondary config area.
	// This allows us to override config file with flags more gracefully.