Skip to content

Commit

Permalink
Create logger (#4)
Browse files Browse the repository at this point in the history
* Create base logger package

* Moved logo and mapping printing in ui module

* Updated logger

* Added tests for global log package methods

* Added tests for logger

* Moved responceprinter to logger package

* Added correct log usage

* Move loggers to ui package

* Updated loggers
  • Loading branch information
Evgeny Abramovich committed Oct 26, 2022
1 parent 5d045a6 commit 1fdf4fd
Show file tree
Hide file tree
Showing 29 changed files with 2,784 additions and 188 deletions.
15 changes: 15 additions & 0 deletions internal/contracts/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package contracts

import "net/http"

type Logger interface {
Error(a ...interface{})
Errorf(template string, a ...interface{})
Warning(a ...interface{})
Warningf(template string, a ...interface{})
Info(a ...interface{})
Infof(template string, a ...interface{})
Debug(a ...interface{})
Debugf(template string, a ...interface{})
PrintResponse(response *http.Response)
}
4 changes: 2 additions & 2 deletions internal/infrastructure/noop-logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package infrastructure

type NoopLogger struct{}

func (n NoopLogger) Infof(format string, v ...interface{}) {}
func (n NoopLogger) Infof(string, ...interface{}) {}

func (n NoopLogger) Errorf(format string, v ...interface{}) {}
func (n NoopLogger) Errorf(string, ...interface{}) {}
20 changes: 14 additions & 6 deletions internal/infrastructure/noop-logger_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package infrastructure_test

import (
"bytes"
"testing"

"github.com/evg4b/uncors/internal/infrastructure"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
)

func TestNoopLogger(t *testing.T) {
testMessage := "test message"
noopLogger := infrastructure.NoopLogger{}

t.Run("Infof do nothing", func(t *testing.T) {
noopLogger.Infof("")
})
t.Run("Infof do nothing", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
noopLogger.Infof(testMessage)

t.Run("Errorf do nothing", func(t *testing.T) {
noopLogger.Errorf("")
})
assert.Empty(t, output.String())
}))

t.Run("Errorf do nothing", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
noopLogger.Errorf(testMessage)

assert.Empty(t, output.String())
}))
}
78 changes: 78 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package log

import (
"fmt"
"io"
"os"

"github.com/pterm/pterm"
)

func Fatal(a ...interface{}) {
Error(a...)
os.Exit(0)
}

func Error(a ...interface{}) {
errorPrinter.Println(a...)
}

func Errorf(template string, a ...interface{}) {
Error(fmt.Sprintf(template, a...))
}

func Warning(a ...interface{}) {
warningPrinter.Println(a...)
}

func Warningf(template string, a ...interface{}) {
Warning(fmt.Sprintf(template, a...))
}

func Info(a ...interface{}) {
infoPrinter.Println(a...)
}

func Infof(template string, a ...interface{}) {
Info(fmt.Sprintf(template, a...))
}

func Debug(a ...interface{}) {
debugPrinter.Println(a...)
}

func Debugf(template string, a ...interface{}) {
Debug(fmt.Sprintf(template, a...))
}

func Print(a ...interface{}) {
pterm.Print(a...)
}

func EnableDebugMessages() {
pterm.EnableDebugMessages()
}

func DisableDebugMessages() {
pterm.DisableDebugMessages()
}

func DisableOutput() {
pterm.DisableOutput()
}

func EnableOutput() {
pterm.EnableOutput()
}

func DisableColor() {
pterm.DisableColor()
}

func EnableColor() {
pterm.EnableColor()
}

func SetOutput(output io.Writer) {
pterm.SetDefaultOutput(output)
}
154 changes: 154 additions & 0 deletions internal/log/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package log_test

import (
"bytes"
"testing"

"github.com/evg4b/uncors/internal/log"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
)

const loggerResult = "this is test message"
const loggerFResult = "this is %s message"

func TestGlobalLogPackage(t *testing.T) {
log.EnableOutput()
log.DisableColor()

t.Run("error logging", func(t *testing.T) {
t.Run("Error", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Error(loggerResult)

assert.Equal(t, " ERROR this is test message\n", output.String())
}))

t.Run("Errorf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Errorf(loggerFResult, "error")

assert.Equal(t, " ERROR this is error message\n", output.String())
}))
})

t.Run("warning logging", func(t *testing.T) {
t.Run("Warning", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Warning(loggerResult)

assert.Equal(t, " WARNING this is test message\n", output.String())
}))

t.Run("Warningf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Warningf(loggerFResult, "warning")

assert.Equal(t, " WARNING this is warning message\n", output.String())
}))
})

t.Run("info logging", func(t *testing.T) {
t.Run("Info", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Info(loggerResult)

assert.Equal(t, " INFO this is test message\n", output.String())
}))

t.Run("Infof", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Infof(loggerFResult, "info")

assert.Equal(t, " INFO this is info message\n", output.String())
}))
})

t.Run("debug logging", func(t *testing.T) {
t.Run("where debug output enabled", func(t *testing.T) {
log.EnableDebugMessages()

t.Run("Debug", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Debug(loggerResult)

assert.Equal(t, " DEBUG this is test message\n", output.String())
}))

t.Run("Debugf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Debugf(loggerFResult, "debug")

assert.Equal(t, " DEBUG this is debug message\n", output.String())
}))
})

t.Run("where debug output disabled", func(t *testing.T) {
log.DisableDebugMessages()

t.Run("Debug", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Debug(loggerResult)

assert.Empty(t, output.String())
}))

t.Run("Debugf", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Debugf(loggerFResult, "debug")

assert.Empty(t, output.String())
}))
})
})

t.Run("raw printing", func(t *testing.T) {
t.Run("Print", testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.Print(loggerResult)

assert.Equal(t, loggerResult, output.String())
}))
})
}

func TestColoring(t *testing.T) {
t.Run(
"should print additional tags where colors enabled",
testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.EnableColor()

log.Error("test error")

expected := "\x1b[30;101m\x1b[30;101m ERROR \x1b[0m\x1b[0m \x1b[91m\x1b[91mtest error\x1b[0m\x1b[0m\n"
assert.Equal(t, expected, output.String())
}),
)

t.Run(
"should print additional tags where colors disabled",
testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.DisableColor()

log.Error("test error")

expected := " ERROR test error\n"
assert.Equal(t, expected, output.String())
}),
)
}

func TestOutputDisabling(t *testing.T) {
log.DisableColor()

t.Run(
"should print log where output enabled",
testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.EnableOutput()

log.Error(loggerResult)

assert.Equal(t, " ERROR this is test message\n", output.String())
}),
)

t.Run(
"should not print log where output disabled",
testutils.LogTest(func(t *testing.T, output *bytes.Buffer) {
log.DisableOutput()

log.Error(loggerResult)

assert.Empty(t, output.String())
}),
)
}
69 changes: 69 additions & 0 deletions internal/log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package log

import (
"net/http"

"github.com/pterm/pterm"
)

type PrefixedLogger struct {
writer *pterm.PrefixPrinter
}

func NewLogger(name string, options ...LoggerOption) *PrefixedLogger {
logger := &PrefixedLogger{
writer: &pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.DefaultText,
Prefix: pterm.Prefix{
Style: &pterm.ThemeDefault.DefaultText,
Text: name,
},
},
}

for _, option := range options {
option(logger)
}

return logger
}

func (logger *PrefixedLogger) Error(v ...interface{}) {
logger.writer.Println(errorPrinter.Sprint(v...))
}

func (logger *PrefixedLogger) Errorf(template string, v ...interface{}) {
logger.writer.Println(errorPrinter.Sprintf(template, v...))
}

func (logger *PrefixedLogger) Warning(v ...interface{}) {
logger.writer.Println(warningPrinter.Sprint(v...))
}

func (logger *PrefixedLogger) Warningf(template string, v ...interface{}) {
logger.writer.Println(warningPrinter.Sprintf(template, v...))
}

func (logger *PrefixedLogger) Info(v ...interface{}) {
logger.writer.Println(infoPrinter.Sprint(v...))
}

func (logger *PrefixedLogger) Infof(template string, v ...interface{}) {
logger.writer.Println(infoPrinter.Sprintf(template, v...))
}

func (logger *PrefixedLogger) Debug(v ...interface{}) {
if pterm.PrintDebugMessages {
logger.writer.Println(debugPrinter.Sprint(v...))
}
}

func (logger *PrefixedLogger) Debugf(template string, v ...interface{}) {
if pterm.PrintDebugMessages {
logger.writer.Println(debugPrinter.Sprintf(template, v...))
}
}

func (logger *PrefixedLogger) PrintResponse(response *http.Response) {
logger.writer.Println(printResponse(response))
}
Loading

0 comments on commit 1fdf4fd

Please sign in to comment.