diff --git a/README.md b/README.md index d525c04..7e98eb0 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,7 @@ Most basic usage with default settings: import "gitea.larvit.se/pwrpln/go_log" func main() { - log := go_log.Log{} - log.SetDefaultValues{} + log := go_log.GetLog() log.Error("Apocalypse! :O"); // stderr log.Warn("The chaos is near"); // stderr log.Info("All is well, but this message is important"); // stdout @@ -27,8 +26,8 @@ func main() { Set log level: ```go -log := go_log.Log{MinLogLvl: go_log.Debug} -log.SetDefaultValues{} +log := go_log.GetLog() +log.MinLogLvl = go_log.Debug // Will now show on stdout log.Debug("A lot of detailed logs to debug your application"); @@ -45,8 +44,7 @@ log.Info("My log msg", "foo", "bar") Setting a logging context to prepend metadata on all log entries: ```go -log := go_log.Log{} -log.SetDefaultValues{} +log := go_log.GetLog() log.Context = []interface{{"some", "thing"}} log.Info("A message") @@ -68,5 +66,4 @@ log := go_log.Log{ Stdout: go_log.DefaultStdout, // Log message outputter for Warning and Error TimeLocation: loc, // Timestamp location/time zone setting } -// Don't run log.SetDefaultValues{} since it will override your settings ``` \ No newline at end of file diff --git a/main.go b/main.go index b985871..c9436b9 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,22 @@ type Log struct { TimeLocation *time.Location // Timestamp location/time zone setting } +func LogLvlFromStr(logLvl string) LogLvl { + if logLvl == "Error" { + return Error + } else if logLvl == "Warn" { + return Warn + } else if logLvl == "Info" { + return Info + } else if logLvl == "Verbose" { + return Verbose + } else if logLvl == "Debug" { + return Debug + } else { + return 0 + } +} + func LogName(logLvl LogLvl) string { if logLvl == 0 { return "Error" @@ -95,14 +111,16 @@ func DefaultStdout(msg string) { os.Stdout.WriteString(msg) } -func (log *Log) SetDefaultValues() { - if log.MinLogLvl == 0 { - log.MinLogLvl = 3 +func GetLog() Log { + log := Log{ + Fmt: DefaultFmt, + MinLogLvl: Info, + Stderr: DefaultStderr, + Stdout: DefaultStdout, } - log.Fmt = DefaultFmt - log.Stderr = DefaultStderr - log.Stdout = DefaultStdout log.TimeLocation, _ = time.LoadLocation("UTC") + + return log } func (log *Log) Error(parts ...interface{}) { diff --git a/main_test.go b/main_test.go index 2e0e665..004399b 100644 --- a/main_test.go +++ b/main_test.go @@ -9,8 +9,7 @@ import ( func TestDefault(t *testing.T) { stdout := "" stderr := "" - testLog := Log{} - testLog.SetDefaultValues() + testLog := GetLog() testLog.Stdout = func(msg string) { stdout += msg } @@ -29,8 +28,7 @@ func TestDefault(t *testing.T) { func TestError(t *testing.T) { stderr := "" - testLog := Log{} - testLog.SetDefaultValues() + testLog := GetLog() testLog.Stderr = func(msg string) { stderr += msg } @@ -38,16 +36,15 @@ func TestError(t *testing.T) { assert.Equal(t, "[\x1b[31mErr\x1b[0m] lureri\n", stderr[20:]) } -func TestMetadata(t *testing.T) { - testLog := Log{} - testLog.SetDefaultValues() - testLog.Context = []interface{}{ - "foo", "bar", - "lur", "pelle", - } - testLog.Info("bosse") - testLog.Error("frasse", "wat", ":O") -} +// func TestMetadata(t *testing.T) { +// testLog := GetLog() +// testLog.Context = []interface{}{ +// "foo", "bar", +// "lur", "pelle", +// } +// testLog.Info("bosse") +// testLog.Error("frasse", "wat", ":O") +// } // func TestWoo(t *testing.T) { // loc, _ := time.LoadLocation("UTC")