4 Commits

Author SHA1 Message Date
8b0bd8ba22 feat: better api implementation for initialization 2022-10-16 01:07:00 +02:00
67e44d06bc feat: improved api 2022-10-16 00:22:29 +02:00
0b21aa2e9c fix: changing name on the only package 2022-10-15 23:22:06 +02:00
51fb9734bc fix: changing name on the only package 2022-10-15 23:11:56 +02:00
3 changed files with 66 additions and 112 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");
@@ -38,21 +37,20 @@ log.Debug("A lot of detailed logs to debug your application");
Using metadata for structured logging: Using metadata for structured logging:
```go ```go
log.InfoM("My log msg", []go_log.Metadata{{Name: "foo", Value: "bar"}}) log.Info("My log msg", "foo", "bar")
// 2022-10-11 07:13:49 [Inf] My log msg foo: bar // 2022-10-11 07:13:49 [Inf] 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 = []go_log.Metadata{{Name: "some", Value: "thing"}}
log.Info("A message") log.Info("A message")
// 2022-10-11 07:13:49 [Inf] A message some: thing // 2022-10-11 07:13:49 [Inf] A message some: thing
log.InfoM("Zep", []go_log.Metadata{{Name: "other", Value: "stuff"}}) log.Info("Zep", "other", "stuff")
// 2022-10-11 07:13:49 [Inf] A message some: thing other: stuff // 2022-10-11 07:13:49 [Inf] A message some: thing other: stuff
``` ```
@@ -61,12 +59,11 @@ All available options, and their defaults:
```go ```go
loc, _ := time.LoadLocation("UTC") loc, _ := time.LoadLocation("UTC")
log := go_log.Log{ log := go_log.Log{
Context: []go_log.Metadata{}, // Will be prepended to metadata on all log entries Context: []interface{}, // Will be prepended to metadata on all log entries
MinLogLvl: go_log.Info, // Minimal log level to output MinLogLvl: go_log.Info, // Minimal log level to output
Fmt: go_log.DefaultFmt, // Log message formatter Fmt: go_log.DefaultFmt, // Log message formatter
Stderr: go_log.DefaultStderr, // Log message outputter for Debug, Verbose and Info Stderr: go_log.DefaultStderr, // Log message outputter for Debug, Verbose and Info
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
``` ```

134
main.go
View File

@@ -1,6 +1,7 @@
package main package go_log
import ( import (
"fmt"
"os" "os"
"time" "time"
) )
@@ -15,16 +16,10 @@ const (
Debug LogLvl = 5 Debug LogLvl = 5
) )
type Metadata struct {
Name string
Value string
}
type FmtOpts struct { type FmtOpts struct {
Context []Metadata Context []interface{}
LogLvlName string LogLvlName string
Metadata []Metadata Parts []interface{}
Msg string
Timestamp time.Time Timestamp time.Time
} }
type Fmt func(FmtOpts) string type Fmt func(FmtOpts) string
@@ -32,7 +27,7 @@ type Fmt func(FmtOpts) string
type Std func(string) type Std func(string)
type Log struct { type Log struct {
Context []Metadata // Will be prepended to metadata on all log entries Context []interface{} // Will be prepended to metadata on all log entries
MinLogLvl LogLvl // Minimal log level to output MinLogLvl LogLvl // Minimal log level to output
Fmt Fmt // Log message formatter Fmt Fmt // Log message formatter
Stderr Std // Log message outputter for Debug, Verbose and Info Stderr Std // Log message outputter for Debug, Verbose and Info
@@ -40,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"
@@ -80,10 +91,13 @@ func LogNameShort(logLvl LogLvl) string {
func DefaultFmt(opts FmtOpts) string { func DefaultFmt(opts FmtOpts) string {
output := opts.Timestamp.Format("2006-01-02 15:04:05") output := opts.Timestamp.Format("2006-01-02 15:04:05")
output += " [" + opts.LogLvlName + "] " + opts.Msg output += " [" + opts.LogLvlName + "] " + fmt.Sprintf("%v", opts.Parts[0])
for i := 0; i < len(opts.Metadata); i++ { for i := 0; i < len(opts.Context); i = i + 2 {
output += " " + opts.Metadata[i].Name + ": " + opts.Metadata[i].Value output += " " + fmt.Sprintf("%v", opts.Context[i]) + ": " + fmt.Sprintf("%v", opts.Context[i+1])
}
for i := 1; i < len(opts.Parts); i = i + 2 {
output += " " + fmt.Sprintf("%v", opts.Parts[i]) + ": " + fmt.Sprintf("%v", opts.Parts[i+1])
} }
return output + "\n" return output + "\n"
@@ -97,117 +111,69 @@ 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(msg string) { func (log *Log) Error(parts ...interface{}) {
if log.MinLogLvl >= Error { if log.MinLogLvl >= Error {
log.Stderr(log.Fmt(FmtOpts{ log.Stderr(log.Fmt(FmtOpts{
Timestamp: time.Now().In(log.TimeLocation), Context: log.Context,
LogLvlName: LogNameShort(Error), LogLvlName: LogNameShort(Error),
Msg: msg, Parts: parts,
Metadata: log.Context,
}))
}
}
func (log *Log) ErrorM(msg string, metadata []Metadata) {
if log.MinLogLvl >= Error {
log.Stderr(log.Fmt(FmtOpts{
Timestamp: time.Now().In(log.TimeLocation), Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Error),
Msg: msg,
Metadata: append(log.Context, metadata[:]...),
})) }))
} }
} }
func (log *Log) Warn(msg string) { func (log *Log) Warn(parts ...interface{}) {
if log.MinLogLvl >= Warn { if log.MinLogLvl >= Warn {
log.Stderr(log.Fmt(FmtOpts{ log.Stderr(log.Fmt(FmtOpts{
Context: log.Context,
Timestamp: time.Now().In(log.TimeLocation), Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Warn), LogLvlName: LogNameShort(Warn),
Msg: msg, Parts: parts,
Metadata: log.Context,
}))
}
}
func (log *Log) WarnM(msg string, metadata []Metadata) {
if log.MinLogLvl >= Warn {
log.Stderr(log.Fmt(FmtOpts{
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Warn),
Msg: msg,
Metadata: append(log.Context, metadata[:]...),
})) }))
} }
} }
func (log *Log) Info(msg string) { func (log *Log) Info(parts ...interface{}) {
if log.MinLogLvl >= Info { if log.MinLogLvl >= Info {
log.Stdout(log.Fmt(FmtOpts{ log.Stdout(log.Fmt(FmtOpts{
Context: log.Context,
Timestamp: time.Now().In(log.TimeLocation), Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Info), LogLvlName: LogNameShort(Info),
Msg: msg, Parts: parts,
Metadata: log.Context,
}))
}
}
func (log *Log) InfoM(msg string, metadata []Metadata) {
if log.MinLogLvl >= Info {
log.Stdout(log.Fmt(FmtOpts{
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Info),
Msg: msg,
Metadata: append(log.Context, metadata[:]...),
})) }))
} }
} }
func (log *Log) Verbose(msg string) { func (log *Log) Verbose(parts ...interface{}) {
if log.MinLogLvl >= Verbose { if log.MinLogLvl >= Verbose {
log.Stdout(log.Fmt(FmtOpts{ log.Stdout(log.Fmt(FmtOpts{
Context: log.Context,
Timestamp: time.Now().In(log.TimeLocation), Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Verbose), LogLvlName: LogNameShort(Verbose),
Msg: msg, Parts: parts,
Metadata: log.Context,
}))
}
}
func (log *Log) VerboseM(msg string, metadata []Metadata) {
if log.MinLogLvl >= Verbose {
log.Stdout(log.Fmt(FmtOpts{
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Verbose),
Msg: msg,
Metadata: append(log.Context, metadata[:]...),
})) }))
} }
} }
func (log *Log) Debug(msg string) { func (log *Log) Debug(parts ...interface{}) {
if log.MinLogLvl >= Debug { if log.MinLogLvl >= Debug {
log.Stdout(log.Fmt(FmtOpts{ log.Stdout(log.Fmt(FmtOpts{
Context: log.Context,
Timestamp: time.Now().In(log.TimeLocation), Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Debug), LogLvlName: LogNameShort(Debug),
Msg: msg, Parts: parts,
Metadata: log.Context,
}))
}
}
func (log *Log) DebugM(msg string, metadata []Metadata) {
if log.MinLogLvl >= Debug {
log.Stdout(log.Fmt(FmtOpts{
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Debug),
Msg: msg,
Metadata: append(log.Context, metadata[:]...),
})) }))
} }
} }

View File

@@ -1,4 +1,4 @@
package main package go_log
import ( import (
"testing" "testing"
@@ -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
} }
@@ -39,20 +37,13 @@ func TestError(t *testing.T) {
} }
// func TestMetadata(t *testing.T) { // func TestMetadata(t *testing.T) {
// testLog := Log{} // testLog := GetLog()
// testLog.SetDefaultValues() // testLog.Context = []interface{}{
// testLog.Context = []Metadata{ // "foo", "bar",
// { // "lur", "pelle",
// Name: "foo",
// Value: "bar",
// },
// {
// Name: "lur",
// Value: "pelle",
// },
// } // }
// testLog.Info("bosse") // testLog.Info("bosse")
// testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}}) // testLog.Error("frasse", "wat", ":O")
// } // }
// func TestWoo(t *testing.T) { // func TestWoo(t *testing.T) {