Skip to content

Commit be968a8

Browse files
Initial commit
Why: * Multi format (html,plain) logger with color labels Solution: * Added mult fuctional logging formatter Task link: * NA Validtion steps: * NA Author: * Luigi Zuccarelli Date: * 02/04/2021 16:55
0 parents  commit be968a8

File tree

11 files changed

+495
-0
lines changed

11 files changed

+495
-0
lines changed

Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.PHONY: all test build clean
2+
3+
all: test build
4+
5+
build:
6+
mkdir -p build
7+
go build -o build ./...
8+
9+
build-dev:
10+
mkdir -p build
11+
GOOS=linux go build -ldflags="-s -w" -o build ./...
12+
13+
test:
14+
go test -v -coverprofile=tests/results/cover.out ./...
15+
16+
cover:
17+
go tool cover -html=tests/results/cover.out -o tests/results/cover.html
18+
19+
clean:
20+
rm -rf build/*
21+
go clean ./...

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# multi.Logger
2+
3+
really ? - a versatile logger that can format and print with color, in plain or html outputs

build/example

1.95 MB
Binary file not shown.

cmd/example/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/microlib/logger/pkg/multi"
7+
"github.com/microlib/simple"
8+
)
9+
10+
// main - needs no explanation :)
11+
func main() {
12+
// older version
13+
var log = &simple.Logger{Level: "trace"}
14+
fmt.Println("Test")
15+
log.Info("Test")
16+
17+
// initial new mutli functional logger
18+
var logger = multi.NewLogger(multi.COLOR, multi.TRACE)
19+
20+
logger.Error("This is an error message")
21+
logger.Info("This is an info message")
22+
logger.Debug("This is a debug message")
23+
logger.Warn("This is a warn message")
24+
logger.Trace("This is a trace message")
25+
logger.Errorf("This is an error message %s", "hello reedge")
26+
logger.Infof("This is an info message %s", "hello reedge")
27+
logger.Debugf("This is a debug message %s", "hello reedge")
28+
logger.Warnf("This is a warn message %s", "hello reedge")
29+
logger.Tracef("This is a trace message %s", "hello reedge")
30+
31+
logger = multi.NewLogger(multi.PLAIN, multi.TRACE)
32+
logger.Error("This is an error message")
33+
logger.Info("This is an info message")
34+
logger.Debug("This is a debug message")
35+
logger.Warn("This is a warn message")
36+
logger.Trace("This is a trace message")
37+
logger.Errorf("This is an error message %s", "hello reedge")
38+
logger.Infof("This is an info message %s", "hello reedge")
39+
logger.Debugf("This is a debug message %s", "hello reedge")
40+
logger.Warnf("This is a warn message %s", "hello reedge")
41+
logger.Tracef("This is a trace message %s", "hello reedge")
42+
43+
logger = multi.NewLogger(multi.HTML, multi.TRACE)
44+
logger.Error("This is an error message")
45+
logger.Info("This is an info message")
46+
logger.Debug("This is a debug message")
47+
logger.Warn("This is a warn message")
48+
logger.Trace("This is a trace message")
49+
logger.Errorf("This is an error message %s", "hello reedge")
50+
logger.Infof("This is an info message %s", "hello reedge")
51+
logger.Debugf("This is a debug message %s", "hello reedge")
52+
logger.Warnf("This is a warn message %s", "hello reedge")
53+
logger.Tracef("This is a trace message %s", "hello reedge")
54+
55+
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/microlib/logger
2+
3+
go 1.16
4+
5+
require github.com/microlib/simple v1.0.1

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/microlib/simple v1.0.1 h1:hkJjfQg0PejJUJ6XiNG/1hxJa6dDsf3EPSh9ZbWDLjc=
2+
github.com/microlib/simple v1.0.1/go.mod h1:AIAkCaaQxDOkppDihi2iI0xOHak5dJjGtzGS35H8lcQ=

html/example.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2021/04/02 16:21:55 <span style="color:#eb4034">&nbsp;[ERROR]</span>&nbsp;:&nbsp;This is an error message</br>
2+
2021/04/02 16:21:55 <span style="color:#3498eb">&nbsp;[INFO]</span>&nbsp;:&nbsp;This is an info message</br>
3+
2021/04/02 16:21:55 <span style="color:#34eb40">&nbsp;[DEBUG]</span>&nbsp;:&nbsp;This is a debug message</br>
4+
2021/04/02 16:21:55 <span style="color:#eba434">&nbsp;[WARN]</span>&nbsp;:&nbsp;This is a warn message</br>
5+
2021/04/02 16:21:55 <span style="color:#d234eb">&nbsp;[TRACE]</span>&nbsp;:&nbsp;This is a trace message</br>
6+
2021/04/02 16:21:55 <span style="color:#eb4034">&nbsp;[ERROR]</span>&nbsp;:&nbsp;This is an error message hello reedge</br>
7+
2021/04/02 16:21:55 <span style="color:#3498eb">&nbsp;[INFO]</span>&nbsp;:&nbsp;This is an info message hello reedge</br>
8+
2021/04/02 16:21:55 <span style="color:#34eb40">&nbsp;[DEBUG]</span>&nbsp;:&nbsp;This is a debug message hello reedge</br>
9+
2021/04/02 16:21:55 <span style="color:#eba434">&nbsp;[WARN]</span>&nbsp;:&nbsp;This is a warn message hello reedge</br>
10+
2021/04/02 16:21:55 <span style="color:#d234eb">&nbsp;[TRACE]</span>&nbsp;:&nbsp;This is a trace message hello reedge</br>

pkg/multi/logger.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package multi
2+
3+
import (
4+
"fmt"
5+
"log"
6+
)
7+
8+
const (
9+
INFO = "info"
10+
DEBUG = "debug"
11+
WARN = "warn"
12+
ERROR = "error"
13+
TRACE = "trace"
14+
PLAIN = "plain"
15+
HTML = "html"
16+
COLOR = "color"
17+
)
18+
19+
type colorRegistry struct {
20+
Red string
21+
Green string
22+
Blue string
23+
Yellow string
24+
Magento string
25+
}
26+
27+
func NewLogger(mode, level string) *Logger {
28+
var cr *colorRegistry
29+
switch mode {
30+
case "color":
31+
cr = &colorRegistry{
32+
Red: "\x1b[1;31m [%s] \x1b[0m\t: %s",
33+
Green: "\x1b[1;36m [%s] \x1b[0m\t: %s",
34+
Blue: "\x1b[1;32m [%s] \x1b[0m\t: %s",
35+
Yellow: "\x1b[1;33m [%s] \x1b[0m\t: %s",
36+
Magento: "\x1b[1;35m [%s] \x1b[0m\t: %s",
37+
}
38+
break
39+
case "html":
40+
cr = &colorRegistry{
41+
Red: "<span style=\"color:#eb4034\">&nbsp;[%s]</span>&nbsp;:&nbsp;%s</br>",
42+
Green: "<span style=\"color:#34eb40\">&nbsp;[%s]</span>&nbsp;:&nbsp;%s</br>",
43+
Blue: "<span style=\"color:#3498eb\">&nbsp;[%s]</span>&nbsp;:&nbsp;%s</br>",
44+
Yellow: "<span style=\"color:#eba434\">&nbsp;[%s]</span>&nbsp;:&nbsp;%s</br>",
45+
Magento: "<span style=\"color:#d234eb\">&nbsp;[%s]</span>&nbsp;:&nbsp;%s</br>",
46+
}
47+
break
48+
case "plain":
49+
cr = &colorRegistry{
50+
Red: " [%s]\t: %s",
51+
Green: " [%s]\t: %s",
52+
Blue: " [%s]\t: %s",
53+
Yellow: " [%s]\t: %s",
54+
Magento: " [%s]\t: %s",
55+
}
56+
break
57+
}
58+
return &Logger{Level: level, Colors: cr}
59+
}
60+
61+
type Logger struct {
62+
Level string
63+
Colors *colorRegistry
64+
}
65+
66+
func (logger Logger) Debug(message string) {
67+
if logger.Level == DEBUG || logger.Level == TRACE {
68+
log.Printf(logger.Colors.Green, "DEBUG", message)
69+
}
70+
}
71+
72+
func (logger Logger) Error(message string) {
73+
log.Printf(logger.Colors.Red, "ERROR", message)
74+
}
75+
76+
func (logger Logger) Warn(message string) {
77+
if logger.Level == WARN || logger.Level == DEBUG || logger.Level == INFO || logger.Level == TRACE {
78+
log.Printf(logger.Colors.Yellow, "WARN", message)
79+
}
80+
}
81+
82+
func (logger Logger) Trace(message string) {
83+
if logger.Level == TRACE {
84+
log.Printf(logger.Colors.Magento, "TRACE", message)
85+
}
86+
}
87+
88+
func (logger Logger) Info(message string) {
89+
if logger.Level == INFO || logger.Level == DEBUG || logger.Level == TRACE {
90+
log.Printf(logger.Colors.Blue, "INFO", message)
91+
}
92+
}
93+
94+
func (logger Logger) Errorf(msg string, val ...interface{}) {
95+
logger.Error(fmt.Sprintf(msg, val...))
96+
}
97+
98+
func (logger Logger) Debugf(msg string, val ...interface{}) {
99+
logger.Debug(fmt.Sprintf(msg, val...))
100+
}
101+
102+
func (logger Logger) Warnf(msg string, val ...interface{}) {
103+
logger.Warn(fmt.Sprintf(msg, val...))
104+
}
105+
106+
func (logger Logger) Tracef(msg string, val ...interface{}) {
107+
logger.Trace(fmt.Sprintf(msg, val...))
108+
}
109+
110+
func (logger Logger) Infof(msg string, val ...interface{}) {
111+
logger.Info(fmt.Sprintf(msg, val...))
112+
}

pkg/multi/logger_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package multi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/microlib/simple"
7+
)
8+
9+
func TestAll(t *testing.T) {
10+
11+
var log = &simple.Logger{Level: "debug"}
12+
13+
t.Run("Logging : should pass", func(t *testing.T) {
14+
log.Info("Testing new logger - this is the old version")
15+
16+
// plainno color
17+
l := NewLogger(PLAIN, TRACE)
18+
l.Info("This is the new version")
19+
20+
l.Info("Log Info")
21+
l.Debug("Log Debug")
22+
l.Trace("Log Trace")
23+
l.Error("Log Error")
24+
l.Infof("Log Info %s", "test")
25+
l.Debugf("Log Debug %s", "test")
26+
l.Tracef("Log Trace %s", "test")
27+
l.Errorf("Log Error %s", "test")
28+
29+
// with terminal color
30+
l = NewLogger(COLOR, TRACE)
31+
l.Info("Log Info")
32+
l.Debug("Log Debug")
33+
l.Trace("Log Trace")
34+
l.Error("Log Error")
35+
l.Infof("Log Info %s", "test")
36+
l.Debugf("Log Debug %s", "test")
37+
l.Tracef("Log Trace %s", "test")
38+
l.Errorf("Log Error %s", "test")
39+
40+
// with html decoration
41+
l = NewLogger(HTML, TRACE)
42+
l.Info("Log Info")
43+
l.Debug("Log Debug")
44+
l.Trace("Log Trace")
45+
l.Error("Log Error")
46+
l.Infof("Log Info %s", "test")
47+
l.Debugf("Log Debug %s", "test")
48+
l.Tracef("Log Trace %s", "test")
49+
l.Errorf("Log Error %s", "test")
50+
51+
})
52+
}

0 commit comments

Comments
 (0)