From a183e24d98130055e5b140b3ade5cf76c4422e79 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Mon, 8 Apr 2024 01:31:19 +0330 Subject: [PATCH 1/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20refactor:=20using?= =?UTF-8?q?=20github.com/fatih/color=20for=20colors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- func.go | 29 ++++++++++++----------------- go.mod | 5 ++++- go.sum | 11 +++++++++++ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/func.go b/func.go index e6d209f..bce9087 100644 --- a/func.go +++ b/func.go @@ -4,19 +4,15 @@ import ( "fmt" "strings" + "github.com/fatih/color" "github.com/sirupsen/logrus" ) //nolint:gochecknoglobals // used as constants var ( - emojisLevel = [7]string{"💀", "🤬", "💩", "🙈", "🙃", "🤷", "🤮"} - colors = [7]string{"[44;1m", "[31;1m", "[31;1m", "[33m", "[36m", "[37;1m", "[35;1m"} -) - -const ( - logFieldColor = "[35;1m" - start = "\033" - end = start + "[0m" + emojisLevel = [7]string{"💀", "🤬", "💩", "🙈", "🙃", "🤷", "🤮"} + colors = [7]*color.Color{color.New(color.BgMagenta), color.New(color.FgRed), color.New(color.FgRed), color.New(color.FgYellow), color.New(color.FgCyan), color.New(color.FgWhite), color.New(color.FgMagenta)} + logFieldColor = color.New(color.FgMagenta).SprintFunc() ) // Format building log message. @@ -37,13 +33,12 @@ func (f *Config) Format(entry *logrus.Entry) ([]byte, error) { emoji := emojisLevel[i] l := level m := entry.Message - fieldPattern := "%s" - if f.Color { - color := colors[i] - l = start + color + level + end - m = start + color + entry.Message + end - fieldPattern = start + logFieldColor + fieldPattern + end - } + + color.NoColor = !f.Color + color := colors[i].SprintFunc() + + l = color(level) + m = color(entry.Message) replacer := strings.NewReplacer( "%time%", entry.Time.Format(timestampFormat), @@ -58,9 +53,9 @@ func (f *Config) Format(entry *logrus.Entry) ([]byte, error) { switch val := val.(type) { case []string: v := strings.Join(val, "\n\t - ") - output += fmt.Sprintf("\n\t"+fieldPattern+": \n\t - %v", k, v) + output += fmt.Sprintf("\n\t%s: \n\t - %v", logFieldColor(k), v) default: - output += fmt.Sprintf("\n\t"+fieldPattern+": %v", k, val) + output += fmt.Sprintf("\n\t%s: %v", logFieldColor(k), val) } } output += "\n" diff --git a/go.mod b/go.mod index 9a5b596..3fa8942 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,10 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fatih/color v1.16.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + golang.org/x/sys v0.14.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fbdf025..31065fc 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,13 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= @@ -13,6 +20,10 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 5a61b558852c717de0c535d3ac42dc13c5481df0 Mon Sep 17 00:00:00 2001 From: Muhammed Hussein Karimi Date: Mon, 8 Apr 2024 01:37:26 +0330 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20typo:=20remove=20du?= =?UTF-8?q?plicate=20variable=20assignment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Muhammed Hussein Karimi --- func.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/func.go b/func.go index bce9087..d8a75fd 100644 --- a/func.go +++ b/func.go @@ -31,14 +31,12 @@ func (f *Config) Format(entry *logrus.Entry) ([]byte, error) { i, _ := logrus.ParseLevel(level) emoji := emojisLevel[i] - l := level - m := entry.Message color.NoColor = !f.Color color := colors[i].SprintFunc() - l = color(level) - m = color(entry.Message) + l := color(level) + m := color(entry.Message) replacer := strings.NewReplacer( "%time%", entry.Time.Format(timestampFormat),