Skip to content
Snippets Groups Projects
Commit a8c719f7 authored by Jan Semmelink's avatar Jan Semmelink
Browse files

Added color to logger

parent 9cd8cc8a
No related branches found
No related tags found
No related merge requests found
package logger package logger
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"strings" "strings"
"time" "time"
"github.com/fatih/color"
"gitlab.com/uafrica/go-utils/errors" "gitlab.com/uafrica/go-utils/errors"
) )
...@@ -102,20 +104,42 @@ func (l Logger) log(level Level, skip int, msg string) { ...@@ -102,20 +104,42 @@ func (l Logger) log(level Level, skip int, msg string) {
// } // }
// l.writer.Write(append(jsonEntry, []byte("\n")...)) // l.writer.Write(append(jsonEntry, []byte("\n")...))
source := fmt.Sprintf("%s(%d)", entry.Caller.File, entry.Caller.Line) source := fmt.Sprintf("%s/%s:%d", entry.Caller.Package, entry.Caller.File, entry.Caller.Line)
if len(source) > 25 { if len(source) > 40 {
source = source[len(source)-25:] source = source[len(source)-40:]
} }
textEntry := fmt.Sprintf("%s %5.5s %-25.25s %s",
entry.Timestamp.Format("2006-01-02 15:04:05"), buffer := bytes.NewBuffer(nil)
entry.Level, red := color.New(color.FgRed).FprintfFunc()
source, magenta := color.New(color.FgMagenta).FprintfFunc()
entry.Message) yellow := color.New(color.FgYellow).FprintfFunc()
blue := color.New(color.FgBlue).FprintfFunc()
green := color.New(color.FgGreen).FprintfFunc()
cyan := color.New(color.FgCyan).FprintfFunc()
blue(buffer, entry.Timestamp.Format("2006-01-02 15:04:05"))
levelString := fmt.Sprintf(" %5.5s", entry.Level)
switch entry.Level {
case LevelFatal:
red(buffer, levelString)
case LevelError:
red(buffer, levelString)
case LevelWarn:
magenta(buffer, levelString)
case LevelInfo:
yellow(buffer, levelString)
case LevelDebug:
green(buffer, levelString)
}
cyan(buffer, fmt.Sprintf(" %-40.40s| ", source))
buffer.Write([]byte(entry.Message))
if len(entry.Data) > 0 { if len(entry.Data) > 0 {
jsonData, _ := json.Marshal(entry.Data) jsonData, _ := json.Marshal(entry.Data)
textEntry += " " + string(jsonData) green(buffer, " "+string(jsonData))
} }
l.writer.Write(append([]byte(textEntry), []byte("\n")...)) buffer.WriteString("\n")
l.writer.Write(buffer.Bytes())
} }
} }
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"os" "os"
"testing" "testing"
"github.com/fatih/color"
"gitlab.com/uafrica/go-utils/errors" "gitlab.com/uafrica/go-utils/errors"
"gitlab.com/uafrica/go-utils/logger" "gitlab.com/uafrica/go-utils/logger"
) )
...@@ -34,3 +35,12 @@ func TestLogs(t *testing.T) { ...@@ -34,3 +35,12 @@ func TestLogs(t *testing.T) {
//logs.Errorf("Debugging %d!", 456) //logs.Errorf("Debugging %d!", 456)
//logs.Error("Info") //logs.Error("Info")
} }
func TestColor(t *testing.T) {
blue := color.New(color.FgBlue).FprintfFunc()
blue(os.Stdout, "important notice: %s", "ssss")
// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(os.Stdout, " don't forget this...")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment