Compare commits

..

No commits in common. "master" and "rc-0.2.0" have entirely different histories.

10 changed files with 282 additions and 60 deletions

View File

@ -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.
- [2021] Refactored to support custom `io.Writer`'s
<center><img src="screenshot.png"></center>
<center><img src="/screenshot.png"></center>
## Install

View File

@ -14,7 +14,7 @@
package main
import "git.zio.sh/astra/logger"
import "github.com/kris-nova/novaarchive/logger"
func main() {

View File

@ -15,7 +15,7 @@
package main
import (
"git.zio.sh/astra/logger"
"github.com/kris-nova/logger"
lol "github.com/kris-nova/lolgopher"
)

View File

@ -17,7 +17,7 @@ package main
import (
"os"
"git.zio.sh/astra/logger"
"github.com/kris-nova/novaarchive/logger"
)
func main() {

8
go.mod
View File

@ -1,8 +0,0 @@
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
)

11
go.sum
View File

@ -1,11 +0,0 @@
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=

View File

@ -24,6 +24,17 @@ import (
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 (
// [Log Constants]
//
@ -91,7 +102,7 @@ func LineBytes(prefix, format string, a ...interface{}) []byte {
}
// Line will format a log line, and return a string
var Line = func(prefix, format string, a ...interface{}) string {
func Line(prefix, format string, a ...interface{}) string {
if !strings.Contains(format, "\n") {
format = fmt.Sprintf("%s%s", format, "\n")
}

108
logger_legacy.go 100644
View File

@ -0,0 +1,108 @@
// 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
}

View File

@ -0,0 +1,158 @@
// 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()
}

View File

@ -17,12 +17,7 @@ package logger
import (
"bytes"
"fmt"
"strings"
"testing"
"time"
"git.zio.sh/astra/logger"
"github.com/fatih/color"
)
const (
@ -211,34 +206,3 @@ func TestAlwaysCriticalDebugOnly(t *testing.T) {
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()
}
}