Init configuration from file #3

Merged
jonasfranz merged 11 commits from zeripath/log:nicer-configuration into master 2019-06-08 10:12:22 +00:00
Owner

This PR provides a way of configuring the loggers independently from the program.

The main benefit is that logging can be set for unit tests

This PR provides a way of configuring the loggers independently from the program. The main benefit is that logging can be set for unit tests
Author
Owner

OK so an example test program looking like:

package main

import "gitea.com/gitea/log"

func main() {
	defer log.Close()
	log.Trace("Trace test")
	log.Debug("Debug test")
	log.Info("Info test")
	log.Warn("Warn test")
	log.Error("Error test")
	log.Critical("Critical test")
}

with configuration:

{
    "DEFAULT_BUFFER_LEN":1000,
    "default": {
        "console": {
            "config": {
                "colorize":true, 
                "flags":"stdflags",
                "stacktraceLevel":"error"
            }
        }
    }
}

works and changes the log configuration.

Now the reason for doing config in init is that unit tests in go are quite annoying - they don't have an easy way of doing set-up consistently without requiring a TestMain for each package.

If we allow our configuration to occur in init we can always pass log configuration in to any test.

OK so an example test program looking like: ```go package main import "gitea.com/gitea/log" func main() { defer log.Close() log.Trace("Trace test") log.Debug("Debug test") log.Info("Info test") log.Warn("Warn test") log.Error("Error test") log.Critical("Critical test") } ``` with configuration: ```json { "DEFAULT_BUFFER_LEN":1000, "default": { "console": { "config": { "colorize":true, "flags":"stdflags", "stacktraceLevel":"error" } } } } ``` works and changes the log configuration. Now the reason for doing config in init is that unit tests in go are quite annoying - they don't have an easy way of doing set-up consistently without requiring a TestMain for each package. If we allow our configuration to occur in init we can always pass log configuration in to any test.
Author
Owner

One final issue is the requirement to add a defer log.Close() to ensure that the final logs are written. I'll look at this. - Looks like there is no other way.

One final issue is the requirement to add a `defer log.Close()` to ensure that the final logs are written. I'll look at this. - Looks like there is no other way.
Owner

Maybe we can call runtime.SetFinalizer(obj, func(obj *typeObj)) to instead defer log.Close.

Maybe we can call `runtime.SetFinalizer(obj, func(obj *typeObj))` to instead `defer log.Close`.
Author
Owner

hmm... so that looked like good idea but it's neither guaranteed to run on os.Exit(...) nor is it guaranteed to run for objects allocated in init().

hmm... so that looked like good idea but it's neither guaranteed to run on `os.Exit(...)` nor is it guaranteed to run for objects allocated in `init()`.
Author
Owner

I think we just have to tolerate that people should add a defer log.Close() to main() and suggest defer log.Flush() added to unit tests.

I think we just have to tolerate that people should add a `defer log.Close()` to `main()` and suggest `defer log.Flush()` added to unit tests.
Owner

Please resolve the conflicts

Please resolve the conflicts
Author
Owner

Done.

Done.
lunny requested changes 2019-06-04 03:22:43 +00:00
Dismissed
log.go Outdated
@ -173,3 +200,3 @@
}
// Close closes all the loggers
// Close closes the default logger

Why not all the loggers?

Why not all the loggers?
Author
Owner

Sorry missed changing that comment. Done now.

Sorry missed changing that comment. Done now.
@ -230,2 +280,4 @@
}
// create a default log to write to console
NewLogger(1000, "std", "console", fmt.Sprintf(`{"flags":%d, "stacktraceLevel":"error"}`,

If config load successfully, the default logger should be disabled I think.

If config load successfully, the default logger should be disabled I think.
Author
Owner

Take a look at line 278:

	if configFile != "" {
		err = ConfigureFromFile(configFile)
		if err == nil { 
			return // <- Line 278
		}
	}

if there's no error during configuration from the provided configuration then init() returns here.

Take a look at line 278: ```go if configFile != "" { err = ConfigureFromFile(configFile) if err == nil { return // <- Line 278 } } ``` if there's no error during configuration from the provided configuration then `init()` returns here.

OK. Just found we need add reactions on code comments.

OK. Just found we need add reactions on code comments.
lunny approved these changes 2019-06-06 09:31:58 +00:00
Dismissed
jonasfranz approved these changes 2019-06-08 10:11:38 +00:00
Dismissed
jonasfranz closed this pull request 2019-06-08 10:12:22 +00:00
This repo is archived. You cannot comment on pull requests.
No description provided.