Added more levels
parent
960319a27a
commit
12f4cd439c
|
@ -14,8 +14,11 @@ type Level int
|
||||||
|
|
||||||
// Available logging levels
|
// Available logging levels
|
||||||
const (
|
const (
|
||||||
Fatal Level = iota
|
Null Level = iota
|
||||||
|
Fatal
|
||||||
Silent
|
Silent
|
||||||
|
Label
|
||||||
|
Misc
|
||||||
Error
|
Error
|
||||||
Info
|
Info
|
||||||
Warning
|
Warning
|
||||||
|
@ -33,6 +36,7 @@ var (
|
||||||
labels = map[Level]string{
|
labels = map[Level]string{
|
||||||
Warning: "WRN",
|
Warning: "WRN",
|
||||||
Error: "ERR",
|
Error: "ERR",
|
||||||
|
Label: "WRN",
|
||||||
Fatal: "FTL",
|
Fatal: "FTL",
|
||||||
Info: "INF",
|
Info: "INF",
|
||||||
}
|
}
|
||||||
|
@ -59,7 +63,7 @@ func wrap(label string, level Level) string {
|
||||||
return aurora.Bold(aurora.Red(label)).String()
|
return aurora.Bold(aurora.Red(label)).String()
|
||||||
case Error:
|
case Error:
|
||||||
return aurora.Red(label).String()
|
return aurora.Red(label).String()
|
||||||
case Warning:
|
case Warning, Label:
|
||||||
return aurora.Yellow(label).String()
|
return aurora.Yellow(label).String()
|
||||||
default:
|
default:
|
||||||
return label
|
return label
|
||||||
|
@ -70,9 +74,9 @@ func wrap(label string, level Level) string {
|
||||||
// and the label passed.
|
// and the label passed.
|
||||||
func getLabel(level Level, label string, sb *strings.Builder) {
|
func getLabel(level Level, label string, sb *strings.Builder) {
|
||||||
switch level {
|
switch level {
|
||||||
case Silent:
|
case Silent, Misc:
|
||||||
return
|
return
|
||||||
case Error, Fatal, Info, Warning:
|
case Error, Fatal, Info, Warning, Label:
|
||||||
sb.WriteString("[")
|
sb.WriteString("[")
|
||||||
sb.WriteString(wrap(labels[level], level))
|
sb.WriteString(wrap(labels[level], level))
|
||||||
sb.WriteString("]")
|
sb.WriteString("]")
|
||||||
|
@ -91,6 +95,11 @@ func getLabel(level Level, label string, sb *strings.Builder) {
|
||||||
|
|
||||||
// log logs the actual message to the screen
|
// log logs the actual message to the screen
|
||||||
func log(level Level, label string, format string, args ...interface{}) {
|
func log(level Level, label string, format string, args ...interface{}) {
|
||||||
|
// Don't log if the level is null
|
||||||
|
if level == Null {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if level <= MaxLevel {
|
if level <= MaxLevel {
|
||||||
// Build the log message using the string builder pool
|
// Build the log message using the string builder pool
|
||||||
sb := stringBuilderPool.Get().(*strings.Builder)
|
sb := stringBuilderPool.Get().(*strings.Builder)
|
||||||
|
@ -147,3 +156,13 @@ func Fatalf(format string, args ...interface{}) {
|
||||||
log(Fatal, "", format, args...)
|
log(Fatal, "", format, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Printf prints a string on screen without any extra stuff
|
||||||
|
func Printf(format string, args ...interface{}) {
|
||||||
|
log(Misc, "", format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Labelf prints a string on screen with a label interface
|
||||||
|
func Labelf(format string, args ...interface{}) {
|
||||||
|
log(Label, "", format, args...)
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/logrusorgru/aurora"
|
"github.com/logrusorgru/aurora"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -27,8 +28,7 @@ func TestGetLabel(t *testing.T) {
|
||||||
sb.Reset()
|
sb.Reset()
|
||||||
getLabel(test.level, test.label, sb)
|
getLabel(test.level, test.label, sb)
|
||||||
data := sb.String()
|
data := sb.String()
|
||||||
if !strings.EqualFold(data, test.expected) {
|
|
||||||
t.Fatalf("Expected %s got %s\n", test.expected, data)
|
assert.Equal(t, data, test.expected, "Expected message and generate message don't match")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue