Skip to content

Commit

Permalink
created fmt sink with colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilliams0305 committed Nov 6, 2023
1 parent fc71444 commit dad7e40
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 25 deletions.
24 changes: 6 additions & 18 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"time"

golog "github.com/ewilliams0305/golog/logger"
"github.com/ewilliams0305/golog/logger/fmtsink"
)

func main() {

sink1 := &FmtPrinter{}
sink1 := &fmtsink.FmtPrinter{}
sink2 := &FmtPrinterSlow{}

logger := golog.LoggingConfiguration().
Expand All @@ -35,12 +36,14 @@ func main() {
level := golog.CreateLevelFromString(response)
logger.SwitchLevel(level)

newError := errors.New("ERROR MESSAGE PASSED TO LOG")

logger.Verbose("Verbose Message %s", "VERNON")
logger.Debug("Debug Message %s %d", "BILLY", 20)
logger.Information("Information Message %s", "IMAC")
logger.Warn("Warn Message %s %d", "Alice", 30)
logger.Error("Error Message", errors.New("ERROR"))
logger.Fatal("Fatal Message", errors.New("FATAL"))
logger.Error("Error Message", newError)
logger.Fatal("Fatal Message", newError)

}

Expand All @@ -54,21 +57,6 @@ func formatTemplate(template string, args ...interface{}) string {
*
****************************/

type FmtPrinter struct {
}

func (f *FmtPrinter) WriteTo(message golog.LogEvent) error {

_, e := fmt.Println(message.RenderMessage())
return e
}

/***************************
*
* Mock Logger that is FAST
*
****************************/

type FmtPrinterSlow struct {
}

Expand Down
16 changes: 16 additions & 0 deletions logger/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type LogEvent struct {
Level LogLevel
// The Message templated used to render the displayed log Message.
Message string
// An optional error to send in the log event.
// Errors can optional be rendered seperately from the rest of the log message.
Error error
// Optional properties to add to your structured logs.
// Properties can include complex objects, simply strings, any
Args []interface{}
Expand All @@ -29,6 +32,10 @@ type FormatMessage interface {
// TODO: Provide message template to facilitate formatting.
func (e *LogEvent) RenderMessage() string {

if e.Error != nil {
return e.RenderErrorEvent()
}

if len(e.Args) > 0 {

formattedArgs := formatTemplate(e.Message, e.Args...)
Expand All @@ -40,3 +47,12 @@ func (e *LogEvent) RenderMessage() string {
func formatTemplate(template string, args ...any) string {
return fmt.Sprintf(template, args...)
}

func (e *LogEvent) RenderErrorEvent() string {
if len(e.Args) > 0 {

formattedArgs := formatTemplate(e.Message, e.Args...)
return fmt.Sprintf("[%s %v] %s \n%s", e.Level.ToString(), e.Timestamp.Format("2006-01-02T15:04:05"), formattedArgs, e.Error)
}
return fmt.Sprintf("[%s %v] %s \n%s", e.Level.ToString(), e.Timestamp.Format("2006-01-02T15:04:05"), e.Message, e.Error)
}
8 changes: 8 additions & 0 deletions logger/fmtsink/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fmtsink

const (
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
)
60 changes: 60 additions & 0 deletions logger/fmtsink/fmtsink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fmtsink

import (
"fmt"

golog "github.com/ewilliams0305/golog/logger"
)

type FmtPrinter struct {
}

func (f *FmtPrinter) WriteTo(message golog.LogEvent) error {
_, e := fmt.Println(colorizeLevel(&message), RenderMessage(&message))
return e
}

func RenderMessage(e *golog.LogEvent) string {

if e.Error != nil {
return e.RenderErrorEvent()
}

if len(e.Args) > 0 {
formattedArgs := formatTemplate(e.Message, e.Args...)
return fmt.Sprintf(" %v %s", e.Timestamp.Format("2006-01-02T15:04:05"), formattedArgs)
}
return fmt.Sprintf(" %v %s", e.Timestamp.Format("2006-01-02T15:04:05"), e.Message)
}

func formatTemplate(template string, args ...any) string {
return fmt.Sprintf(template, args...)
}

func RenderErrorEvent(e *golog.LogEvent) string {
if len(e.Args) > 0 {

formattedArgs := formatTemplate(e.Message, e.Args...)
return fmt.Sprintf(" %v %s \n%s", e.Timestamp.Format("2006-01-02T15:04:05"), formattedArgs, e.Error)
}
return fmt.Sprintf(" %v %s \n%s", e.Timestamp.Format("2006-01-02T15:04:05"), e.Message, e.Error)
}

func colorizeLevel(e *golog.LogEvent) string {
switch e.Level {
case golog.Verbose:
return green + "[" + e.Level.ToString() + "]"
case golog.Debug:
return green + "[" + e.Level.ToString() + "]"
case golog.Information:
return yellow + "[" + e.Level.ToString() + "]"
case golog.Warn:
return yellow + "[" + e.Level.ToString() + "]"
case golog.Error:
return red + "[" + e.Level.ToString() + "]"
case golog.Fatal:
return red + "[" + e.Level.ToString() + "]"
default:
return red + "[" + e.Level.ToString() + "]"
}
}
15 changes: 8 additions & 7 deletions logger/golog.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,35 @@ func (gl *goLog) CurrentLevel() LogLevel {

func (gl *goLog) Verbose(message string, args ...interface{}) {

gl.write(message, Verbose, args...)
gl.write(message, Verbose, nil, args...)
}

func (gl *goLog) Debug(message string, args ...interface{}) {

gl.write(message, Debug, args...)
gl.write(message, Debug, nil, args...)
}

func (gl *goLog) Information(message string, args ...interface{}) {

gl.write(message, Information, args...)
gl.write(message, Information, nil, args...)
}

func (gl *goLog) Warn(message string, args ...interface{}) {

gl.write(message, Warn, args...)
gl.write(message, Warn, nil, args...)
}

func (gl *goLog) Error(message string, err error, args ...interface{}) {

gl.write(message, Error, args...)
gl.write(message, Error, err, args...)
}

func (gl *goLog) Fatal(message string, err error, args ...interface{}) {

gl.write(message, Fatal, args...)
gl.write(message, Fatal, err, args...)
}

func (gl *goLog) write(message string, level LogLevel, args ...interface{}) {
func (gl *goLog) write(message string, level LogLevel, err error, args ...interface{}) {

resultChan := make(chan string, len(gl.sinks))
var wg sync.WaitGroup
Expand All @@ -132,6 +132,7 @@ func (gl *goLog) write(message string, level LogLevel, args ...interface{}) {
Timestamp: time.Now(),
Level: level,
Message: message,
Error: err,
Args: args,
}, resultChan)
defer wg.Done()
Expand Down
1 change: 1 addition & 0 deletions logger/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s LogLevel) ToString() string {
return "UNKNOWN"
}

// Creates a level from a string value.
func CreateLevelFromString(s string) LogLevel {

switch strings.ToUpper(s) {
Expand Down

0 comments on commit dad7e40

Please sign in to comment.