Skip to content

Commit

Permalink
updated colors to include color related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilliams0305 committed Nov 6, 2023
1 parent 61d6c8c commit 36ef943
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 87 deletions.
5 changes: 2 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import (
"time"

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

func main() {

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

logger := golog.LoggingConfiguration().
Configure(golog.Verbose, "[%l %t] %m").
WriteTo(sink1).MinimuLevel(golog.Verbose).
WriteTo(&fmtwriter.FmtPrinter{}).MinimuLevel(golog.Verbose).
WriteTo(sink2).MinimuLevel(golog.Information).
CreateLogger()

Expand Down
8 changes: 0 additions & 8 deletions logger/fmtsink/colors.go

This file was deleted.

76 changes: 0 additions & 76 deletions logger/fmtsink/fmtsink.go

This file was deleted.

48 changes: 48 additions & 0 deletions writers/fmtsink/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fmtwriter

import (
"strings"

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

const (
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
)

func colorizeError(err error) string {
var sb strings.Builder
sb.WriteString(">>>>> ")
sb.WriteString(red)
sb.WriteString(err.Error())
sb.WriteString(reset)
return sb.String()
}

func getLevelColor(l *golog.LogLevel) string {
switch *l {
case golog.Verbose, golog.Debug:
return green
case golog.Information, golog.Warn:
return yellow
case golog.Error, golog.Fatal:
return red
default:
return red
}
}
func colorizeLevel(e *golog.LogEvent) string {
color := getLevelColor(&e.Level)
var sb strings.Builder

sb.WriteString(color)
sb.WriteString("[")
sb.WriteString(e.Level.ToString())
sb.WriteString("]")
sb.WriteString(reset)

return sb.String()
}
55 changes: 55 additions & 0 deletions writers/fmtsink/fmtprinter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fmtwriter

import (
"fmt"

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

// The FmtPrinter is a golog sink created for simple verbose printing to the std output.
// Messages will be formatted and published to the console.
//
// Usage Example: Create a loger and provide an instance of the FmtPrinter
//
// logger := golog.LoggingConfiguration().
//
// Configure(golog.Verbose, "[%l %t] %m").
// WriteTo(&fmtwriter.FmtPrinter{}).MinimuLevel(golog.Verbose).
// WriteTo(sink2).MinimuLevel(golog.Information).
// CreateLogger()
//
// logger.Verbose("Verbose Message %s", "VERNON")
type FmtPrinter struct {
}

// Writes to the console after formatting the log event.
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 renderErrorEvent(e)
}

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, colorizeError(e.Error))
}
return fmt.Sprintf("%v %s \n%s", e.Timestamp.Format("2006-01-02T15:04:05"), e.Message, colorizeError(e.Error))
}

0 comments on commit 36ef943

Please sign in to comment.