~bayindirh/nudge

3c9da8a4d748318d869b64b2d8c289bcb41a027a — Hakan Bayindir 4 months ago 73aed78
refactor: Move logging initialization to start.

Since logging file support is going away, a more streamlined and compact logging setup is implemented. This means we can setup and start the logger at the very beginning and log any and everything happening inside the code.
2 files changed, 12 insertions(+), 21 deletions(-)

M CHANGELOG.md
M src/nudge.go
M CHANGELOG.md => CHANGELOG.md +1 -0
@@ 5,6 5,7 @@
## 2023-10-16

- 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.

## 2023-10-15


M src/nudge.go => src/nudge.go +11 -21
@@ 392,26 392,10 @@ 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

	// Start by applying default configuration before building things up.
	// Start with initializing valid configuration space.
	initializeValidConfigurationOptions(&validConfigurationOptions)

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

	// TODO: Prettify logger with some stylistic configuration here.
	// Relevant documentation is here: https://pkg.go.dev/go.uber.org/zap#hdr-Configuring_Zap
	/*
	 * To be able to ship 0.2 as quickly as possible, Zap will be configured as follows:
	 * 1- Create a good enough configuration with JSON based on system defaults.
	 * 2- Build Zap with that configuration.
	 * 3- Reconfigure Zap after parsing all the options and creating the final runtime
	 *    configuration.
	 *
	 * XXX: This JSON config part will be replaced with a programmatic block later.
	 */

	
	// 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(`{
	  "level": "debug",
	  "encoding": "console",


@@ 427,7 411,6 @@ func main() {
	}`)

	var zapRuntimeConfig zap.Config

	if err := json.Unmarshal(zapDefaultConfigJSON, &zapRuntimeConfig); err != nil {
		panic(err)
	}


@@ 452,6 435,13 @@ func main() {
	sugaredLogger := logger.Sugar()
	sugaredLogger.Debugf("Logger is up.")

	// Start by applying default configuration before building things up.
	// Start with initializing valid configuration space.
	initializeValidConfigurationOptions(&validConfigurationOptions)

	// 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.