Compare commits
12 Commits
Author | SHA1 | Date |
---|---|---|
Astra | 181161616c | |
Astra | 4512aae71c | |
Astra | c7a3e64167 | |
Astra | d79bef6774 | |
Astra | 8fb99799c7 | |
Astra | a0974739c0 | |
Astra | c1e08a4657 | |
Kris Nóva | 195d85016a | |
Kris Nóva | 352a85877c | |
Kris Nóva | dc4e3aa5d3 | |
Mike | da056f7cbb | |
Kris Nóva | 5a22a5876c |
|
@ -5,7 +5,7 @@
|
||||||
- [2017] Originally ported from it's [original location](https://github.com/kubicorn/kubicorn/tree/master/pkg/logger) in the Kubicorn code base.
|
- [2017] Originally ported from it's [original location](https://github.com/kubicorn/kubicorn/tree/master/pkg/logger) in the Kubicorn code base.
|
||||||
- [2021] Refactored to support custom `io.Writer`'s
|
- [2021] Refactored to support custom `io.Writer`'s
|
||||||
|
|
||||||
<center><img src="/screenshot.png"></center>
|
<center><img src="screenshot.png"></center>
|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/kris-nova/novaarchive/logger"
|
import "git.zio.sh/astra/logger"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kris-nova/logger"
|
"git.zio.sh/astra/logger"
|
||||||
lol "github.com/kris-nova/lolgopher"
|
lol "github.com/kris-nova/lolgopher"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/kris-nova/novaarchive/logger"
|
"git.zio.sh/astra/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
module git.zio.sh/astra/logger
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/fatih/color v1.10.0
|
||||||
|
github.com/kris-nova/lolgopher v0.0.0-20210112022122-73f0047e8b65
|
||||||
|
)
|
|
@ -0,0 +1,11 @@
|
||||||
|
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
||||||
|
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
|
||||||
|
github.com/kris-nova/lolgopher v0.0.0-20210112022122-73f0047e8b65 h1:g+tnN/LHRq6LaUfeREPluv9g6jOtN3P1hQwTHofQTSw=
|
||||||
|
github.com/kris-nova/lolgopher v0.0.0-20210112022122-73f0047e8b65/go.mod h1:V0HF/ZBlN86HqewcDC/cVxMmYDiRukWjSrgKLUAn9Js=
|
||||||
|
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
|
||||||
|
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||||
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
||||||
|
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
13
logger.go
13
logger.go
|
@ -24,17 +24,6 @@ import (
|
||||||
|
|
||||||
type LoggerFunc func(format string, a ...interface{})
|
type LoggerFunc func(format string, a ...interface{})
|
||||||
|
|
||||||
//type Logger interface {
|
|
||||||
// LineBytes(prefix, format string, a ...interface{}) []byte
|
|
||||||
// Line(prefix, format string, a ...interface{}) string
|
|
||||||
// Always(format string, a ...interface{})
|
|
||||||
// Success(format string, a ...interface{})
|
|
||||||
// Debug(format string, a ...interface{})
|
|
||||||
// Info(format string, a ...interface{})
|
|
||||||
// Warning(format string, a ...interface{})
|
|
||||||
// Critical(format string, a ...interface{})
|
|
||||||
//}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// [Log Constants]
|
// [Log Constants]
|
||||||
//
|
//
|
||||||
|
@ -102,7 +91,7 @@ func LineBytes(prefix, format string, a ...interface{}) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Line will format a log line, and return a string
|
// Line will format a log line, and return a string
|
||||||
func Line(prefix, format string, a ...interface{}) string {
|
var Line = func(prefix, format string, a ...interface{}) string {
|
||||||
if !strings.Contains(format, "\n") {
|
if !strings.Contains(format, "\n") {
|
||||||
format = fmt.Sprintf("%s%s", format, "\n")
|
format = fmt.Sprintf("%s%s", format, "\n")
|
||||||
}
|
}
|
||||||
|
|
108
logger_legacy.go
108
logger_legacy.go
|
@ -1,108 +0,0 @@
|
||||||
// Copyright © 2021 Kris Nóva <kris@nivenly.com>
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Legacy Logic for 0.1.0
|
|
||||||
//
|
|
||||||
// Here we store the legacy (0.1.0) compatible configuration options
|
|
||||||
// that will eventually be deprecated.
|
|
||||||
//
|
|
||||||
var (
|
|
||||||
|
|
||||||
// Timestamps are used to toggle timestamps
|
|
||||||
// deprecated
|
|
||||||
Timestamps = true
|
|
||||||
|
|
||||||
// TestMode is used for running
|
|
||||||
// the regression tests.
|
|
||||||
// deprecated
|
|
||||||
TestMode = false
|
|
||||||
|
|
||||||
// Color is no longer used
|
|
||||||
// deprecated
|
|
||||||
Color = true
|
|
||||||
|
|
||||||
// Level is the legacy log level
|
|
||||||
//
|
|
||||||
// 0 (Least verbose)
|
|
||||||
// 1
|
|
||||||
// 2
|
|
||||||
// 3
|
|
||||||
// 4 (Most verbose)
|
|
||||||
//
|
|
||||||
// deprecated
|
|
||||||
Level = -1
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
testRaceMutex = sync.Mutex{}
|
|
||||||
annoyed = false
|
|
||||||
)
|
|
||||||
|
|
||||||
// checkDeprecatedValues is a singleton
|
|
||||||
// that will only execute once.
|
|
||||||
// This will convert the legacy logger.Level
|
|
||||||
// to the new logger.BitwiseLevel
|
|
||||||
//
|
|
||||||
// LogEverything =
|
|
||||||
func checkDeprecatedValues() {
|
|
||||||
testRaceMutex.Lock()
|
|
||||||
defer testRaceMutex.Unlock()
|
|
||||||
if Level != -1 {
|
|
||||||
if !annoyed {
|
|
||||||
Deprecated("********")
|
|
||||||
Deprecated("***")
|
|
||||||
Deprecated("*")
|
|
||||||
Deprecated("logger.Level is deprecated. Use logger.BitwiseLevel")
|
|
||||||
Deprecated("*")
|
|
||||||
Deprecated("***")
|
|
||||||
Deprecated("********")
|
|
||||||
annoyed = true
|
|
||||||
}
|
|
||||||
if Level == 4 {
|
|
||||||
BitwiseLevel = LogDeprecated | LogAlways | LogSuccess | LogCritical | LogWarning | LogInfo | LogDebug
|
|
||||||
} else if Level == 3 {
|
|
||||||
BitwiseLevel = LogDeprecated | LogAlways | LogSuccess | LogCritical | LogWarning | LogInfo
|
|
||||||
} else if Level == 2 {
|
|
||||||
BitwiseLevel = LogDeprecated | LogAlways | LogSuccess | LogCritical | LogWarning
|
|
||||||
} else if Level == 1 {
|
|
||||||
BitwiseLevel = LogDeprecated | LogAlways | LogSuccess | LogCritical
|
|
||||||
} else if Level == 0 {
|
|
||||||
BitwiseLevel = LogDeprecated | LogAlways | LogSuccess
|
|
||||||
} else {
|
|
||||||
BitwiseLevel = LogDeprecated | LogEverything
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// legacyFindWriter will check if there is an io.Writer
|
|
||||||
// appended to the end of the arguments passed to the logger.
|
|
||||||
//
|
|
||||||
// deprecated
|
|
||||||
func legacyFindWriter(a ...interface{}) []interface{} {
|
|
||||||
if n := len(a); n > 0 {
|
|
||||||
// extract an io.Writer at the end of a
|
|
||||||
if newWriter, ok := a[n-1].(io.Writer); ok {
|
|
||||||
Writer = newWriter
|
|
||||||
a = a[0 : n-1]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
}
|
|
|
@ -1,158 +0,0 @@
|
||||||
// Copyright © 2021 Kris Nóva <kris@nivenly.com>
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
//
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
//
|
|
||||||
// Legacy Regression tests for 0.1.0
|
|
||||||
//
|
|
||||||
// Note: @christopherhein added these tests and I am unsure if Amazon
|
|
||||||
// is still using the old io.Writer append() convention.
|
|
||||||
// Either way, I am keeping legacy (deprecated) support for it.
|
|
||||||
//
|
|
||||||
// All of these regression tests should still pass.
|
|
||||||
//
|
|
||||||
// ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
format = "%v, %v, %v, all eyes on me!"
|
|
||||||
formatExp = `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.* \[%s\] \d, \d, \d, all eyes on me!`
|
|
||||||
formatWOTimeExp = `\[%s\] \d, \d, \d, all eyes on me!`
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
a = []interface{}{1, 2, 3}
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
TestMode = true
|
|
||||||
m.Run()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAlwaysLegacy(t *testing.T) {
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatExp, PreAlways))
|
|
||||||
g := captureLoggerOutput(Always, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Always should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCriticalLegacy(t *testing.T) {
|
|
||||||
Level = 1
|
|
||||||
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatExp, PreCritical))
|
|
||||||
g := captureLoggerOutput(Critical, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Critical should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInfoLegacy(t *testing.T) {
|
|
||||||
Level = 3
|
|
||||||
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatExp, PreInfo))
|
|
||||||
g := captureLoggerOutput(Info, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Info should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSuccessLegacy(t *testing.T) {
|
|
||||||
Level = 3
|
|
||||||
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatExp, PreSuccess))
|
|
||||||
g := captureLoggerOutput(Success, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Success should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDebugLegacy(t *testing.T) {
|
|
||||||
Level = 4
|
|
||||||
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatExp, PreDebug))
|
|
||||||
g := captureLoggerOutput(Debug, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Info should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWarningLegacy(t *testing.T) {
|
|
||||||
Level = 2
|
|
||||||
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatExp, PreWarning))
|
|
||||||
g := captureLoggerOutput(Warning, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Info should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithoutTimestampsLegacy(t *testing.T) {
|
|
||||||
Timestamps = false
|
|
||||||
e, err := regexp.Compile(fmt.Sprintf(formatWOTimeExp, PreAlways))
|
|
||||||
g := captureLoggerOutput(Always, format, a)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !e.MatchString(g) {
|
|
||||||
t.Fatalf("Always should produce a pattern '%v' but produces: %v", e.String(), g)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// captureLoggerOutput is used to test the log functions
|
|
||||||
func captureLoggerOutput(l LoggerFunc, format string, a []interface{}) string {
|
|
||||||
b := new(bytes.Buffer)
|
|
||||||
l(format, append(a, b)...)
|
|
||||||
return b.String()
|
|
||||||
}
|
|
|
@ -17,7 +17,12 @@ package logger
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.zio.sh/astra/logger"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -206,3 +211,34 @@ func TestAlwaysCriticalDebugOnly(t *testing.T) {
|
||||||
t.Errorf("Expected (%s) Actual (%s)", expected, actual)
|
t.Errorf("Expected (%s) Actual (%s)", expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note: Taken from the use case in eksctl
|
||||||
|
// https://github.com/michaelbeaumont/eksctl/blob/a1564e4cf825be9fd8936794e230d3b5c2d267ea/cmd/eksctl/main.go#L78-L79
|
||||||
|
func TestLineOverride(t *testing.T) {
|
||||||
|
Level = -1
|
||||||
|
BitwiseLevel = LogAlways
|
||||||
|
Line = func(prefix, format string, a ...interface{}) string {
|
||||||
|
if !strings.Contains(format, "\n") {
|
||||||
|
format = fmt.Sprintf("%s%s", format, "\n")
|
||||||
|
}
|
||||||
|
if logger.Timestamps {
|
||||||
|
now := time.Now()
|
||||||
|
fNow := now.Format(logger.Layout)
|
||||||
|
prefix = fmt.Sprintf("%s [%s]", fNow, prefix)
|
||||||
|
} else {
|
||||||
|
prefix = fmt.Sprintf("[%s]", prefix)
|
||||||
|
}
|
||||||
|
out := fmt.Sprintf("%s %s", prefix, fmt.Sprintf(format, a...))
|
||||||
|
out = color.GreenString(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
buffer := new(bytes.Buffer)
|
||||||
|
Writer = buffer
|
||||||
|
Always(TestFormat, testA...)
|
||||||
|
actual := buffer.String()
|
||||||
|
expected := Line(PreAlways, TestFormat, testA...)
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf("actual(%s) != expected(%s)", actual, expected)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue