feat: better api implementation for initialization

This commit is contained in:
Lilleman auf Larv 2022-10-16 01:07:00 +02:00
parent 67e44d06bc
commit 8b0bd8ba22
3 changed files with 39 additions and 27 deletions

View File

@ -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
```

30
main.go
View File

@ -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{}) {

View File

@ -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")