-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated colors to include color related functions
- Loading branch information
1 parent
61d6c8c
commit 36ef943
Showing
5 changed files
with
105 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |