Skip to content

Commit

Permalink
Added enum color and styles logger text
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cataldo committed Feb 8, 2024
1 parent 4a845a9 commit 0ab9cd8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ go-logger
=================
<img align="right" src="gopher-logger.png" alt="">

[![Project status](https://img.shields.io/badge/version-v1.2.4-vividgreen.svg)](https://github.com/GabrielHCataldo/go-logger/releases/tag/v1.2.4)
[![Project status](https://img.shields.io/badge/version-v1.2.5-vividgreen.svg)](https://github.com/GabrielHCataldo/go-logger/releases/tag/v1.2.5)
[![Go Report Card](https://goreportcard.com/badge/github.com/GabrielHCataldo/go-logger)](https://goreportcard.com/report/github.com/GabrielHCataldo/go-logger)
[![Coverage Status](https://coveralls.io/repos/GabrielHCataldo/go-logger/badge.svg?branch=main&service=github)](https://coveralls.io/github/GabrielHCataldo/go-logger?branch=main)
[![Open Source Helpers](https://www.codetriage.com/gabrielhcataldo/go-logger/badges/users.svg)](https://www.codetriage.com/gabrielhcataldo/go-logger)
Expand Down
15 changes: 8 additions & 7 deletions logger/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ func getLoggerNormalPrefix(lvl level, skipCaller int, opts Options) string {
if !opts.HideAllArgs && !opts.HideArgCaller {
b.WriteString(" \x1b[4m")
b.WriteString(getArgCaller(skipCaller))
b.WriteString("\x1b[0m:")
b.WriteString(StyleReset)
b.WriteString(":")
} else if helper.IsEmpty(datetimeString) {
b.WriteString(":")
}
Expand Down Expand Up @@ -332,7 +333,7 @@ func getLoggerJson(lvl level, skipCaller int, opts Options, format string, v ...
msg = helper.Sprintln(v...)
}
lg := logJson{
Level: lvl.String(),
Level: lvl.string(),
Msg: msg,
}
if opts.HideAllArgs {
Expand All @@ -353,11 +354,11 @@ func getLoggerJson(lvl level, skipCaller int, opts Options, format string, v ...
func getArgLogLevel(lvl level, opts Options) string {
color := "\x1b[1m"
if opts.DisablePrefixColors {
color += level("").ColorLevel()
color += level("").colorLevel()
} else {
color += lvl.ColorLevel()
color += lvl.colorLevel()
}
return strings.Join([]string{color, lvl.String(), "\x1b[0m"}, "")
return strings.Join([]string{color, lvl.string(), StyleReset}, "")
}

func getArgDatetime(opts Options) string {
Expand All @@ -372,7 +373,7 @@ func getArgCaller(skipCaller int) string {

func prepareMessageColorOnPrefix(lvl level, b *strings.Builder) {
if !opts.DisableMessageColors {
b.WriteString(lvl.ColorMessage())
b.WriteString(lvl.colorMessage())
}
}

Expand All @@ -381,7 +382,7 @@ func prepareMessageColor(lvl level, msg ...any) []any {
var nMsg []any
for _, vMsg := range msg {
sMsg := helper.SimpleConvertToString(vMsg)
sMsg = strings.ReplaceAll(sMsg, "\n", fmt.Sprint("\n", lvl.ColorMessage()))
sMsg = strings.ReplaceAll(sMsg, "\n", fmt.Sprint("\n", lvl.colorMessage()))
nMsg = append(nMsg, sMsg)
}
return nMsg
Expand Down
47 changes: 37 additions & 10 deletions logger/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ const (
levelWarning level = "WARNING"
levelError level = "ERROR"
)
const (
StyleReset = "\x1b[0m"
StyleBright = "\x1b[1m"
StyleDim = "\x1b[2m"
StyleUnderscore = "\x1b[4m"
StyleBlink = "\x1b[5m"
StyleReverse = "\x1b[7m"
StyleHidden = "\x1b[8m"

ForegroundBlack = "\x1b[30m"
ForegroundRed = "\x1b[31m"
ForegroundGreen = "\x1b[32m"
ForegroundYellow = "\x1b[33m"
ForegroundBlue = "\x1b[34m"
ForegroundMagenta = "\x1b[35m"
ForegroundCyan = "\x1b[36m"
ForegroundWhite = "\x1b[37m"

BackgroundBlack = "\x1b[40m"
BackgroundRed = "\x1b[41m"
BackgroundGreen = "\x1b[42m"
BackgroundYellow = "\x1b[43m"
BackgroundBlue = "\x1b[44m"
BackgroundMagenta = "\x1b[45m"
BackgroundCyan = "\x1b[46m"
BackgroundWhite = "\x1b[47m"
)

func (d DateFormat) isEnumValid() bool {
switch d {
Expand All @@ -63,31 +90,31 @@ func (d DateFormat) Format() string {
return string(DateFormatFull24h)
}

func (l level) ColorLevel() string {
func (l level) colorLevel() string {
switch l {
case levelInfo:
return "\x1b[34m"
return ForegroundBlue
case levelDebug:
return "\x1b[36m"
return ForegroundCyan
case levelWarning:
return "\x1b[33m"
return ForegroundYellow
case levelError:
return "\x1b[31m"
return ForegroundRed
default:
return "\x1b[0m"
return StyleReset
}
}

func (l level) ColorMessage() string {
func (l level) colorMessage() string {
switch l {
case levelError:
return "\x1b[31m"
return ForegroundRed
default:
return "\x1b[0m"
return StyleReset
}
}

func (l level) String() string {
func (l level) string() string {
return string(l)
}

Expand Down

0 comments on commit 0ab9cd8

Please sign in to comment.