Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| db30b03f8f | |||
| 62e3eac0a9 | |||
| 75cf71e917 | |||
| 7f444673ad | |||
| 8b0bd8ba22 | |||
| 67e44d06bc | |||
| 0b21aa2e9c | |||
| 51fb9734bc |
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
Copyright (c) 2023 Larv IT AB
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
|
||||
33
README.md
33
README.md
@@ -2,7 +2,7 @@
|
||||
|
||||
## Installation
|
||||
|
||||
`go get -u https://gitea.larvit.se/pwrpln/go_log`
|
||||
`go get -u gitea.larvit.se/pwrpln/go_log`
|
||||
|
||||
## Example usage
|
||||
|
||||
@@ -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");
|
||||
@@ -38,35 +37,41 @@ log.Debug("A lot of detailed logs to debug your application");
|
||||
Using metadata for structured logging:
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
Setting a logging context to prepend metadata on all log entries:
|
||||
|
||||
```go
|
||||
log := go_log.Log{}
|
||||
log.SetDefaultValues{}
|
||||
log.Context = []go_log.Metadata{{Name: "some", Value: "thing"}}
|
||||
log := go_log.GetLog()
|
||||
log.Context = []interface{{"some", "thing"}}
|
||||
|
||||
log.Info("A message")
|
||||
// 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
|
||||
```
|
||||
|
||||
All available options, and their defaults:
|
||||
|
||||
```go
|
||||
loc, _ := time.LoadLocation("UTC")
|
||||
loc, _ := time.LoadLocation("UTC") // See more info at https://pkg.go.dev/time#LoadLocation
|
||||
log := go_log.Log{
|
||||
Context: []go_log.Metadata{}, // Will be prepended to metadata on all log entries
|
||||
MinLogLvl: go_log.Info, // Minimal log level to output
|
||||
Context: []interface{}{}, // Will be prepended to metadata on all log entries
|
||||
MinLogLvl: go_log.LogLvlFromStr("Info"), // Minimal log level to output
|
||||
Fmt: go_log.DefaultFmt, // Log message formatter
|
||||
Stderr: go_log.DefaultStderr, // Log message outputter for Debug, Verbose and Info
|
||||
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
|
||||
```
|
||||
|
||||
Or change them after initialization like this:
|
||||
|
||||
```go
|
||||
log := go_log.GetLog()
|
||||
log.MinLogLvl = go_log.LogLvlFromStr("Debug")
|
||||
log.Context = []interface{}{"key1", "value1", "key2", "value2"}
|
||||
```
|
||||
2
go.mod
2
go.mod
@@ -2,7 +2,7 @@ module gitea.larvit.se/pwrpln/go_log
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/stretchr/testify v1.8.0
|
||||
require github.com/stretchr/testify v1.8.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@@ -5,9 +5,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
134
main.go
134
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
package go_log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
@@ -15,16 +16,10 @@ const (
|
||||
Debug LogLvl = 5
|
||||
)
|
||||
|
||||
type Metadata struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
type FmtOpts struct {
|
||||
Context []Metadata
|
||||
Context []interface{}
|
||||
LogLvlName string
|
||||
Metadata []Metadata
|
||||
Msg string
|
||||
Parts []interface{}
|
||||
Timestamp time.Time
|
||||
}
|
||||
type Fmt func(FmtOpts) string
|
||||
@@ -32,7 +27,7 @@ type Fmt func(FmtOpts) string
|
||||
type Std func(string)
|
||||
|
||||
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
|
||||
Fmt Fmt // Log message formatter
|
||||
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
|
||||
}
|
||||
|
||||
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"
|
||||
@@ -80,10 +91,13 @@ func LogNameShort(logLvl LogLvl) string {
|
||||
|
||||
func DefaultFmt(opts FmtOpts) string {
|
||||
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++ {
|
||||
output += " " + opts.Metadata[i].Name + ": " + opts.Metadata[i].Value
|
||||
for i := 0; i < len(opts.Context); i = i + 2 {
|
||||
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"
|
||||
@@ -97,117 +111,69 @@ 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(msg string) {
|
||||
func (log *Log) Error(parts ...interface{}) {
|
||||
if log.MinLogLvl >= Error {
|
||||
log.Stderr(log.Fmt(FmtOpts{
|
||||
Timestamp: time.Now().In(log.TimeLocation),
|
||||
Context: log.Context,
|
||||
LogLvlName: LogNameShort(Error),
|
||||
Msg: msg,
|
||||
Metadata: log.Context,
|
||||
}))
|
||||
}
|
||||
}
|
||||
func (log *Log) ErrorM(msg string, metadata []Metadata) {
|
||||
if log.MinLogLvl >= Error {
|
||||
log.Stderr(log.Fmt(FmtOpts{
|
||||
Parts: parts,
|
||||
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 {
|
||||
log.Stderr(log.Fmt(FmtOpts{
|
||||
Context: log.Context,
|
||||
Timestamp: time.Now().In(log.TimeLocation),
|
||||
LogLvlName: LogNameShort(Warn),
|
||||
Msg: msg,
|
||||
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[:]...),
|
||||
Parts: parts,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func (log *Log) Info(msg string) {
|
||||
func (log *Log) Info(parts ...interface{}) {
|
||||
if log.MinLogLvl >= Info {
|
||||
log.Stdout(log.Fmt(FmtOpts{
|
||||
Context: log.Context,
|
||||
Timestamp: time.Now().In(log.TimeLocation),
|
||||
LogLvlName: LogNameShort(Info),
|
||||
Msg: msg,
|
||||
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[:]...),
|
||||
Parts: parts,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func (log *Log) Verbose(msg string) {
|
||||
func (log *Log) Verbose(parts ...interface{}) {
|
||||
if log.MinLogLvl >= Verbose {
|
||||
log.Stdout(log.Fmt(FmtOpts{
|
||||
Context: log.Context,
|
||||
Timestamp: time.Now().In(log.TimeLocation),
|
||||
LogLvlName: LogNameShort(Verbose),
|
||||
Msg: msg,
|
||||
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[:]...),
|
||||
Parts: parts,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
func (log *Log) Debug(msg string) {
|
||||
func (log *Log) Debug(parts ...interface{}) {
|
||||
if log.MinLogLvl >= Debug {
|
||||
log.Stdout(log.Fmt(FmtOpts{
|
||||
Context: log.Context,
|
||||
Timestamp: time.Now().In(log.TimeLocation),
|
||||
LogLvlName: LogNameShort(Debug),
|
||||
Msg: msg,
|
||||
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[:]...),
|
||||
Parts: parts,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
25
main_test.go
25
main_test.go
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package go_log
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -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
|
||||
}
|
||||
@@ -39,20 +37,13 @@ func TestError(t *testing.T) {
|
||||
}
|
||||
|
||||
// func TestMetadata(t *testing.T) {
|
||||
// testLog := Log{}
|
||||
// testLog.SetDefaultValues()
|
||||
// testLog.Context = []Metadata{
|
||||
// {
|
||||
// Name: "foo",
|
||||
// Value: "bar",
|
||||
// },
|
||||
// {
|
||||
// Name: "lur",
|
||||
// Value: "pelle",
|
||||
// },
|
||||
// testLog := GetLog()
|
||||
// testLog.Context = []interface{}{
|
||||
// "foo", "bar",
|
||||
// "lur", "pelle",
|
||||
// }
|
||||
// testLog.Info("bosse")
|
||||
// testLog.ErrorM("frasse", []Metadata{{Name: "wat", Value: ":O"}})
|
||||
// testLog.Error("frasse", "wat", ":O")
|
||||
// }
|
||||
|
||||
// func TestWoo(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user