Skip to content

Commit

Permalink
Merge pull request #1 from kuttiproject/development
Browse files Browse the repository at this point in the history
Add a new, special Error log level, and change some function spellings.
  • Loading branch information
rajch authored Jul 18, 2022
2 parents 3adebb8 + af7a674 commit db92cd6
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# kuttilog

A simple levelled logging system, used throughout the kutti project.

[![Go Report Card](https://goreportcard.com/badge/github.com/kuttiproject/kuttilog)](https://goreportcard.com/report/github.com/kuttiproject/kuttilog)
Expand Down
3 changes: 3 additions & 0 deletions defaultlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (d *defaultlogger) MaxLevel() int {
}

func (d *defaultlogger) LevelPrefix(level int) string {
if level == -1 {
return ""
}
return levelprefixes[level]
}

Expand Down
9 changes: 7 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
// a default implementation, which uses a standard output logger
// created using the Go standard log package.
//
// Levels begin at 0, and can go up to a implemetation-specific
// Levels begin at 0, and can go up to a implementation-specific
// maximum. The default logger provides five levels: Quiet(0),
// Minimal(1), Info(2), Verbose(3) and Debug(4).
// Minimal(1), Info(2), Verbose(3) and Debug(4). The default
// level is Info(2).
//
// There is also a special level: Error(-1), deliberately kept
// below the minimum level. All implementations must ensure
// that logs at this level have to mandatorily produce output.
package kuttilog
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/kuttiproject/kuttilog

go 1.15
go 1.18
15 changes: 8 additions & 7 deletions kuttilog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

// Default Log levels
const (
Error = -1
Quiet = 0
Minimal = 1
Info = 2
Expand All @@ -27,23 +28,23 @@ func MaxLevel() int {
return logger.MaxLevel()
}

// Loglevel returns the current log level.
func Loglevel() int {
// LogLevel returns the current log level.
func LogLevel() int {
return loglevel
}

// Setloglevel sets the current log level.
// SetLogLevel sets the current log level.
// If level is below 0 or greater than MaxLevel, it is not changed.
func Setloglevel(newlevel int) {
func SetLogLevel(newlevel int) {
if newlevel >= 0 && newlevel <= MaxLevel() {
loglevel = newlevel
}
}

// V returns true if the specified level is between 0 and the current
// V returns true if the specified level is between -1 and the current
// log level, false otherwise.
func V(level int) bool {
return level <= loglevel && level >= 0
return level <= loglevel && level >= -1
}

func printWith(printfunc func(...interface{}), level int, v ...interface{}) {
Expand Down Expand Up @@ -71,7 +72,7 @@ func Printf(level int, format string, v ...interface{}) {
prefix := logger.LevelPrefix(level)
if prefix != "" {
output := append([]interface{}{logger.LevelPrefix(level)}, v...)
logger.Printf("%v"+format, output...)
logger.Printf("%v "+format, output...)
} else {
logger.Printf(format, v...)
}
Expand Down
8 changes: 4 additions & 4 deletions kuttilog_defaultlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestDefaultlogger(t *testing.T) {
t.Log("Testing Default Logger...")
ResetLogger()

Setloglevel(Info)
SetLogLevel(Info)

var buf bytes.Buffer
defaultLogger.log.SetOutput(&buf)
Expand Down Expand Up @@ -49,13 +49,13 @@ func TestDefaultlogger(t *testing.T) {
}

// Debug output should happen at Debug level
Setloglevel(Debug)
SetLogLevel(Debug)

buf.Reset()
Printf(Debug, "Hello %v", 42)
if buf.String() != "[DEBUG]Hello 42\n" {
if buf.String() != "[DEBUG] Hello 42\n" {
t.Errorf("\nDefault Logger Print error:\n Expected: %#v\n Got: %#v",
"[DEBUG]Hello 42\n",
"[DEBUG] Hello 42\n",
buf.String())
}
}
58 changes: 36 additions & 22 deletions kuttilog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
)

func TestSetLogLevel(t *testing.T) {
defaultloglevel := kuttilog.Loglevel()
defaultloglevel := kuttilog.LogLevel()
maxloglevel := kuttilog.MaxLevel()
t.Logf("\nDefault log level is: %v\nMax log level is: %v", defaultloglevel, maxloglevel)

t.Logf("Trying to change to negative log level")
kuttilog.Setloglevel(-1)
if kuttilog.Loglevel() != defaultloglevel {
kuttilog.SetLogLevel(-1)
if kuttilog.LogLevel() != defaultloglevel {
t.Error("SetLogLevel allowed setting negative level")
t.FailNow()
}

t.Logf("Trying to change to greater than max log level")
kuttilog.Setloglevel(maxloglevel + 1)
if kuttilog.Loglevel() != defaultloglevel {
kuttilog.SetLogLevel(maxloglevel + 1)
if kuttilog.LogLevel() != defaultloglevel {
t.Error("SetLogLevel allowed setting level greater than max.")
t.FailNow()
}
Expand All @@ -42,18 +42,20 @@ func TestV(t *testing.T) {
{setlevel: 2, level: 3, result: false},
{setlevel: 2, level: 4, result: false},
{setlevel: 2, level: 5, result: false},
{setlevel: 2, level: -1, result: false},
{setlevel: 2, level: -2, result: false},
{setlevel: 2, level: -1, result: true},
{setlevel: 1, level: 0, result: true},
{setlevel: 1, level: 1, result: true},
{setlevel: 1, level: 2, result: false},
{setlevel: 1, level: 3, result: false},
{setlevel: 1, level: 4, result: false},
{setlevel: 1, level: 5, result: false},
{setlevel: 1, level: -1, result: false},
{setlevel: 1, level: -2, result: false},
{setlevel: 1, level: -1, result: true},
}

for _, testrow := range testvtable {
kuttilog.Setloglevel(testrow.setlevel)
kuttilog.SetLogLevel(testrow.setlevel)
receivedresult := kuttilog.V(testrow.level)
if receivedresult != testrow.result {
t.Errorf(
Expand Down Expand Up @@ -87,6 +89,9 @@ func (d *testlogger) MaxLevel() int {
}

func (d *testlogger) LevelPrefix(level int) string {
if level == -1 {
return ""
}
return testloggerlevelprefixes[level]
}

Expand Down Expand Up @@ -119,28 +124,31 @@ func TestPrint(t *testing.T) {
{setlevel: 2, level: 3, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: 4, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: 5, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: -1, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: -2, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: -1, inputtext: "Hello", outputtext: "Hello"},
{setlevel: 0, level: 0, inputtext: "Hello", outputtext: "Hello"},
{setlevel: 0, level: 1, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 2, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 3, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 4, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 5, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: -1, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: -2, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: -1, inputtext: "Hello", outputtext: "Hello"},
{setlevel: 4, level: 0, inputtext: "Hello", outputtext: "Hello"},
{setlevel: 4, level: 1, inputtext: "Hello", outputtext: "Hello"},
{setlevel: 4, level: 2, inputtext: "Hello", outputtext: "Hello"},
{setlevel: 4, level: 3, inputtext: "Hello", outputtext: "[V]Hello"},
{setlevel: 4, level: 4, inputtext: "Hello", outputtext: "[D]Hello"},
{setlevel: 4, level: 5, inputtext: "Hello", outputtext: ""},
{setlevel: 4, level: -1, inputtext: "Hello", outputtext: ""},
{setlevel: 4, level: -2, inputtext: "Hello", outputtext: ""},
{setlevel: 4, level: -1, inputtext: "Hello", outputtext: "Hello"},
}

tl := &testlogger{}
kuttilog.SetLogger(tl)
for _, testrow := range testprinttable {
tl.logstring = ""
kuttilog.Setloglevel(testrow.setlevel)
kuttilog.SetLogLevel(testrow.setlevel)
kuttilog.Print(testrow.level, testrow.inputtext)
if tl.logstring != testrow.outputtext {
t.Errorf(
Expand Down Expand Up @@ -173,28 +181,31 @@ func TestPrintf(t *testing.T) {
{setlevel: 2, level: 3, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 2, level: 4, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 2, level: 5, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 2, level: -1, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 2, level: -2, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 2, level: -1, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
{setlevel: 0, level: 0, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
{setlevel: 0, level: 1, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: 2, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: 3, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: 4, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: 5, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: -1, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: -2, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 0, level: -1, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
{setlevel: 4, level: 0, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
{setlevel: 4, level: 1, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
{setlevel: 4, level: 2, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
{setlevel: 4, level: 3, inputtext: "Hello %v\n", parameter: 42, outputtext: "[V]Hello 42\n"},
{setlevel: 4, level: 4, inputtext: "Hello %v\n", parameter: 42, outputtext: "[D]Hello 42\n"},
{setlevel: 4, level: 3, inputtext: "Hello %v\n", parameter: 42, outputtext: "[V] Hello 42\n"},
{setlevel: 4, level: 4, inputtext: "Hello %v\n", parameter: 42, outputtext: "[D] Hello 42\n"},
{setlevel: 4, level: 5, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 4, level: -1, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 4, level: -2, inputtext: "Hello %v\n", parameter: 42, outputtext: ""},
{setlevel: 4, level: -1, inputtext: "Hello %v\n", parameter: 42, outputtext: "Hello 42\n"},
}

tl := &testlogger{}
kuttilog.SetLogger(tl)
for _, testrow := range testprintftable {
tl.logstring = ""
kuttilog.Setloglevel(testrow.setlevel)
kuttilog.SetLogLevel(testrow.setlevel)
kuttilog.Printf(testrow.level, testrow.inputtext, testrow.parameter)
if tl.logstring != testrow.outputtext {
t.Errorf(
Expand Down Expand Up @@ -228,28 +239,31 @@ func TestPrintln(t *testing.T) {
{setlevel: 2, level: 3, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: 4, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: 5, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: -1, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: -2, inputtext: "Hello", outputtext: ""},
{setlevel: 2, level: -1, inputtext: "Hello", outputtext: "Hello\n"},
{setlevel: 0, level: 0, inputtext: "Hello", outputtext: "Hello\n"},
{setlevel: 0, level: 1, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 2, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 3, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 4, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: 5, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: -1, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: -2, inputtext: "Hello", outputtext: ""},
{setlevel: 0, level: -1, inputtext: "Hello", outputtext: "Hello\n"},
{setlevel: 4, level: 0, inputtext: "Hello", outputtext: "Hello\n"},
{setlevel: 4, level: 1, inputtext: "Hello", outputtext: "Hello\n"},
{setlevel: 4, level: 2, inputtext: "Hello", outputtext: "Hello\n"},
{setlevel: 4, level: 3, inputtext: "Hello", outputtext: "[V] Hello\n"},
{setlevel: 4, level: 4, inputtext: "Hello", outputtext: "[D] Hello\n"},
{setlevel: 4, level: 5, inputtext: "Hello", outputtext: ""},
{setlevel: 4, level: -1, inputtext: "Hello", outputtext: ""},
{setlevel: 4, level: -2, inputtext: "Hello", outputtext: ""},
{setlevel: 4, level: -1, inputtext: "Hello", outputtext: "Hello\n"},
}

tl := &testlogger{}
kuttilog.SetLogger(tl)
for _, testrow := range testprintlntable {
tl.logstring = ""
kuttilog.Setloglevel(testrow.setlevel)
kuttilog.SetLogLevel(testrow.setlevel)
kuttilog.Println(testrow.level, testrow.inputtext)
if tl.logstring != testrow.outputtext {
t.Errorf(
Expand Down
18 changes: 14 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package kuttilog

// Logger contains methods for implemetation-specific logging.
// Logger contains methods for implementation-specific logging.
// Exact destination, formatting etc. is implementation-specific.
// Kuttilog will call each print method only if needed. It will
// also append a level prefix string as the first item output.
// Implementations can provide their own prefix strings.
// Implementations can provide their own prefix strings via the
// LevelPrefix method.
type Logger interface {
MaxLevel() int // The maximum level permitted by this logger
LevelPrefix(level int) string // The logger-supplied prefix string for a level.
// MaxLevel returns the maximum level permitted by this logger.
MaxLevel() int
// LevelPrefix returns rhe logger-supplied prefix string for a
// level. It may return an empty string.
LevelPrefix(level int) string
// Print prints to the log.
// Arguments are handled in the manner of fmt.Print.
Print(v ...interface{})
// Printf prints to the log.
// Arguments are handled in the manner of fmt.Printf.
Printf(format string, v ...interface{})
// Println prints to the log.
// Arguments are handled in the manner of fmt.Println.
Println(v ...interface{})
}

0 comments on commit db92cd6

Please sign in to comment.