Skip to content

Commit

Permalink
Bug fix where line break was not being painted with the respective co…
Browse files Browse the repository at this point in the history
…lor by the level, also added the underline in the file and line name arguments and added bold in the level name.
  • Loading branch information
Gabriel Cataldo committed Jan 26, 2024
1 parent 165cbd6 commit 757b01c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 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.1.8-vividgreen.svg)](https://github.com/GabrielHCataldo/go-logger/releases/tag/v1.1.8)
[![Project status](https://img.shields.io/badge/version-v1.1.9-vividgreen.svg)](https://github.com/GabrielHCataldo/go-logger/releases/tag/v1.1.9)
[![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
45 changes: 28 additions & 17 deletions logger/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func executePrintLog(lvl level, skipCaller int, opts Options, format string, tag
if helper.Equals(opts.Mode, ModeJson) {
printJsonMsg(logger, lvl, skipCaller+1, opts, format, msg...)
} else {
printDefaultMsg(logger, format, msg...)
printDefaultMsg(logger, lvl, format, msg...)
}
}

Expand All @@ -155,13 +155,14 @@ func prepareMsg(tag string, opts Options, msgContents ...any) []any {
return processedMsg
}

func printDefaultMsg(logger *log.Logger, format string, msg ...any) {
func printDefaultMsg(logger *log.Logger, lvl level, format string, msg ...any) {
nMsg := prepareMessageColor(lvl, msg...)
if helper.IsNotEmpty(format) {
logger.Printf(format, msg...)
logger.Printf(format, nMsg...)
} else if opts.RemoveSpace {
logger.Print(msg...)
logger.Print(nMsg...)
} else {
logger.Println(msg...)
logger.Println(nMsg...)
}
}

Expand Down Expand Up @@ -299,7 +300,9 @@ func getLoggerNormalPrefix(lvl level, skipCaller int, opts Options) string {
b.WriteString(getArgLogLevel(lvl, opts))
b.WriteString(datetimeString)
if !opts.HideAllArgs && !opts.HideArgCaller {
b.WriteString(" " + getArgCaller(skipCaller) + ":")
b.WriteString(" \x1b[4m")
b.WriteString(getArgCaller(skipCaller))
b.WriteString("\x1b[0m:")
} else if helper.IsEmpty(datetimeString) {
b.WriteString(":")
}
Expand All @@ -308,7 +311,7 @@ func getLoggerNormalPrefix(lvl level, skipCaller int, opts Options) string {
b.WriteString(opts.CustomAfterPrefixText)
b.WriteString(" ")
}
prepareMessageColor(lvl, &b)
prepareMessageColorOnPrefix(lvl, &b)
return b.String()
}

Expand Down Expand Up @@ -348,13 +351,13 @@ func getLoggerJson(lvl level, skipCaller int, opts Options, format string, v ...
}

func getArgLogLevel(lvl level, opts Options) string {
var color string
color := "\x1b[1m"
if opts.DisablePrefixColors {
color = level("").Color()
color += level("").ColorLevel()
} else {
color = lvl.Color()
color += lvl.ColorLevel()
}
return strings.Join([]string{color, lvl.String(), "\u001B[0m"}, "")
return strings.Join([]string{color, lvl.String(), "\x1b[0m"}, "")
}

func getArgDatetime(opts Options) string {
Expand All @@ -367,15 +370,23 @@ func getArgCaller(skipCaller int) string {
return fileName + ":" + line
}

func prepareMessageColor(lvl level, b *strings.Builder) {
func prepareMessageColorOnPrefix(lvl level, b *strings.Builder) {
if !opts.DisableMessageColors {
switch lvl {
case levelError:
b.WriteString(lvl.Color())
b.WriteString(" ")
break
b.WriteString(lvl.ColorMessage())
}
}

func prepareMessageColor(lvl level, msg ...any) []any {
if !opts.DisableMessageColors {
var nMsg []any
for _, vMsg := range msg {
sMsg := helper.SimpleConvertToString(vMsg)
sMsg = strings.ReplaceAll(sMsg, "\n", fmt.Sprint("\n", lvl.ColorMessage()))
nMsg = append(nMsg, sMsg)
}
return nMsg
}
return msg
}

func prepareValue(value any, tag string) any {
Expand Down
21 changes: 15 additions & 6 deletions logger/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,27 @@ func (d DateFormat) Format() string {
return string(DateFormatFull24h)
}

func (l level) Color() string {
func (l level) ColorLevel() string {
switch l {
case levelInfo:
return "\u001b[34m"
return "\x1b[34m"
case levelDebug:
return "\u001b[36m"
return "\x1b[36m"
case levelWarning:
return "\u001b[33m"
return "\x1b[33m"
case levelError:
return "\u001b[31m"
return "\x1b[31m"
default:
return "\u001B[0m"
return "\x1b[0m"
}
}

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

Expand Down
5 changes: 5 additions & 0 deletions logger/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"github.com/GabrielHCataldo/go-helper/helper"
"go.mongodb.org/mongo-driver/bson/primitive"
"runtime/debug"
"time"
)

Expand Down Expand Up @@ -218,6 +219,9 @@ func initTables() []tableTest {
{
"datetime argument", "%v %v", 1, []any{"datetime:", primitive.NewDateTimeFromTime(time.Now())},
},
{
"datetime argument", "%v %v", 1, []any{"debug stack:", string(debug.Stack())},
},
}
}

Expand All @@ -228,6 +232,7 @@ func initOptionsTest() {

func initOptionsTestNil() {
SetOptions(nil)
//SetOptions(&Options{Mode: ModeJson})
}

func getOptionsTest() *Options {
Expand Down

0 comments on commit 757b01c

Please sign in to comment.