-
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.
- Loading branch information
1 parent
fc71444
commit dad7e40
Showing
6 changed files
with
99 additions
and
25 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 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 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,8 @@ | ||
package fmtsink | ||
|
||
const ( | ||
reset = "\033[0m" | ||
red = "\033[31m" | ||
green = "\033[32m" | ||
yellow = "\033[33m" | ||
) |
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,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() + "]" | ||
} | ||
} |
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 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