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" import "gitea.larvit.se/pwrpln/go_log"
func main() { func main() {
log := go_log.Log{} log := go_log.GetLog()
log.SetDefaultValues{}
log.Error("Apocalypse! :O"); // stderr log.Error("Apocalypse! :O"); // stderr
log.Warn("The chaos is near"); // stderr log.Warn("The chaos is near"); // stderr
log.Info("All is well, but this message is important"); // stdout log.Info("All is well, but this message is important"); // stdout
@ -27,8 +26,8 @@ func main() {
Set log level: Set log level:
```go ```go
log := go_log.Log{MinLogLvl: go_log.Debug} log := go_log.GetLog()
log.SetDefaultValues{} log.MinLogLvl = go_log.Debug
// Will now show on stdout // Will now show on stdout
log.Debug("A lot of detailed logs to debug your application"); 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: Setting a logging context to prepend metadata on all log entries:
```go ```go
log := go_log.Log{} log := go_log.GetLog()
log.SetDefaultValues{}
log.Context = []interface{{"some", "thing"}} log.Context = []interface{{"some", "thing"}}
log.Info("A message") log.Info("A message")
@ -68,5 +66,4 @@ log := go_log.Log{
Stdout: go_log.DefaultStdout, // Log message outputter for Warning and Error Stdout: go_log.DefaultStdout, // Log message outputter for Warning and Error
TimeLocation: loc, // Timestamp location/time zone setting 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 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 { func LogName(logLvl LogLvl) string {
if logLvl == 0 { if logLvl == 0 {
return "Error" return "Error"
@ -95,14 +111,16 @@ func DefaultStdout(msg string) {
os.Stdout.WriteString(msg) os.Stdout.WriteString(msg)
} }
func (log *Log) SetDefaultValues() { func GetLog() Log {
if log.MinLogLvl == 0 { log := Log{
log.MinLogLvl = 3 Fmt: DefaultFmt,
MinLogLvl: Info,
Stderr: DefaultStderr,
Stdout: DefaultStdout,
} }
log.Fmt = DefaultFmt
log.Stderr = DefaultStderr
log.Stdout = DefaultStdout
log.TimeLocation, _ = time.LoadLocation("UTC") log.TimeLocation, _ = time.LoadLocation("UTC")
return log
} }
func (log *Log) Error(parts ...interface{}) { func (log *Log) Error(parts ...interface{}) {

View File

@ -9,8 +9,7 @@ import (
func TestDefault(t *testing.T) { func TestDefault(t *testing.T) {
stdout := "" stdout := ""
stderr := "" stderr := ""
testLog := Log{} testLog := GetLog()
testLog.SetDefaultValues()
testLog.Stdout = func(msg string) { testLog.Stdout = func(msg string) {
stdout += msg stdout += msg
} }
@ -29,8 +28,7 @@ func TestDefault(t *testing.T) {
func TestError(t *testing.T) { func TestError(t *testing.T) {
stderr := "" stderr := ""
testLog := Log{} testLog := GetLog()
testLog.SetDefaultValues()
testLog.Stderr = func(msg string) { testLog.Stderr = func(msg string) {
stderr += msg stderr += msg
} }
@ -38,16 +36,15 @@ func TestError(t *testing.T) {
assert.Equal(t, "[\x1b[31mErr\x1b[0m] lureri\n", stderr[20:]) assert.Equal(t, "[\x1b[31mErr\x1b[0m] lureri\n", stderr[20:])
} }
func TestMetadata(t *testing.T) { // func TestMetadata(t *testing.T) {
testLog := Log{} // testLog := GetLog()
testLog.SetDefaultValues() // testLog.Context = []interface{}{
testLog.Context = []interface{}{ // "foo", "bar",
"foo", "bar", // "lur", "pelle",
"lur", "pelle", // }
} // testLog.Info("bosse")
testLog.Info("bosse") // testLog.Error("frasse", "wat", ":O")
testLog.Error("frasse", "wat", ":O") // }
}
// func TestWoo(t *testing.T) { // func TestWoo(t *testing.T) {
// loc, _ := time.LoadLocation("UTC") // loc, _ := time.LoadLocation("UTC")