Skip to content

Commit

Permalink
Merge pull request #12 from leonidasdeim/11-should-be-changed-to-defa…
Browse files Browse the repository at this point in the history
…ult-text-format

different colors for levels
leodeim authored Sep 15, 2023
2 parents a20266f + 076e4a2 commit 5cc7b87
Showing 2 changed files with 27 additions and 7 deletions.
30 changes: 25 additions & 5 deletions formatter.go
Original file line number Diff line number Diff line change
@@ -22,9 +22,12 @@ const (
)

var (
green = color.New(color.FgGreen).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
blue = color.New(color.FgBlue).SprintFunc()

magenta = color.New(color.FgMagenta).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
)

type _formatter struct{}
@@ -52,13 +55,30 @@ func (_formatter) TextColor(l *log, level Level, message string) (string, error)
}
return fmt.Sprintf(
ColorTextLogFormat,
yellow(time.Now().Format(l.global.dateFormat)),
green(level),
time.Now().Format(l.global.dateFormat),
levelToColor(level),
blue(name),
message,
), nil
}

func levelToColor(level Level) string {
switch level {
case Fatal:
return red(level)
case Error:
return red(level)
case Warning:
return yellow(level)
case Info:
return green(level)
case Debug:
return magenta(level)
default:
return string(level)
}
}

func (_formatter) Json(l *log, level Level, message string) (string, error) {
b, err := json.Marshal(map[string]string{
"time": time.Now().Format(l.global.dateFormat),
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ var levels = map[Level]int{
const (
DefaultLevel = Info
DefaultName = "<...>"
DefaultFormat = FormatText
DefaultFormat = FormatTextColor
DefaultWriteMode = ModeBlocking
DefaultDateFormat = "2006/01/02 15:04:05"
)
@@ -141,7 +141,7 @@ func New(opts ...Op) Logger {
if len(gp.writers) == 0 {
gp.writers = append(gp.writers, writer{
writer: os.Stdout,
format: FormatTextColor,
format: DefaultFormat,
})
}

0 comments on commit 5cc7b87

Please sign in to comment.