Skip to content

Commit 4153715

Browse files
committed
adding custom colors for logging
1 parent d6b7535 commit 4153715

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

Logging/Persisters/console.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,41 @@ import (
88

99
type consoleLogger struct {
1010
logUntil loggingC.LogType
11+
colors map[loggingC.LogType]ConsoleLoggerColorDelegate
1112
}
1213

1314
// NewConsoleLogger -
14-
func NewConsoleLogger(logUntil loggingC.LogType) loggingC.Logger {
15+
func NewConsoleLogger(logUntil loggingC.LogType, colors map[loggingC.LogType]ConsoleLoggerColorDelegate) loggingC.Logger {
1516
return &consoleLogger{
1617
logUntil: logUntil,
18+
colors: colors,
1719
}
1820
}
1921

22+
// NewConsoleLoggerDefaultColors -
23+
func NewConsoleLoggerDefaultColors() map[loggingC.LogType]ConsoleLoggerColorDelegate {
24+
return map[loggingC.LogType]ConsoleLoggerColorDelegate{
25+
loggingC.TypeDisable: WhiteString,
26+
loggingC.TypeTrace: BlackStringYellowBG,
27+
loggingC.TypePanic: RedString,
28+
loggingC.TypeFatal: RedString,
29+
loggingC.TypeError: RedString,
30+
loggingC.TypeWarning: YellowString,
31+
loggingC.TypeInfo: WhiteString,
32+
loggingC.TypeDebug: CyanString,
33+
}
34+
}
35+
36+
// ConsoleLoggerColorDelegate -
37+
type ConsoleLoggerColorDelegate func(string, ...interface{}) string
38+
39+
// BlackStringYellowBG -
2040
func BlackStringYellowBG(format string, a ...interface{}) string {
2141
c := New(FgBlack, BgYellow)
2242
return c.Sprintf(format, a...)
2343
}
2444

45+
// BlackStringWhiteBG -
2546
func BlackStringWhiteBG(format string, a ...interface{}) string {
2647
c := New(FgBlack, BgWhite)
2748
return c.Sprintf(format, a...)
@@ -38,14 +59,18 @@ func (thisRef consoleLogger) Log(logEntry loggingC.LogEntry) {
3859
}
3960

4061
if logEntry.Type == loggingC.TypeTrace {
41-
fmt.Println(BlackStringYellowBG(logEntry.Message))
62+
fmt.Println(thisRef.colors[loggingC.TypeTrace](logEntry.Message))
63+
4264
} else if logEntry.Type < loggingC.TypeWarning {
43-
fmt.Println(RedString(logEntry.Message))
65+
fmt.Println(thisRef.colors[loggingC.TypeError](logEntry.Message))
66+
4467
} else if logEntry.Type == loggingC.TypeWarning {
45-
fmt.Println(YellowString(logEntry.Message))
68+
fmt.Println(thisRef.colors[loggingC.TypeWarning](logEntry.Message))
69+
4670
} else if logEntry.Type == loggingC.TypeInfo {
47-
fmt.Println(WhiteString(logEntry.Message))
71+
fmt.Println(thisRef.colors[loggingC.TypeInfo](logEntry.Message))
72+
4873
} else if logEntry.Type == loggingC.TypeDebug {
49-
fmt.Println(CyanString(logEntry.Message))
74+
fmt.Println(thisRef.colors[loggingC.TypeDebug](logEntry.Message))
5075
}
5176
}

Logging/logging.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ func Init(logger loggingC.EasyLogger) {
2020

2121
// NewConsoleLogger -
2222
func NewConsoleLogger() loggingC.EasyLogger {
23+
return NewConsoleLoggerCustomColors(loggingP.NewConsoleLoggerDefaultColors())
24+
}
25+
26+
// NewConsoleLoggerCustomColors -
27+
func NewConsoleLoggerCustomColors(colors map[loggingC.LogType]loggingP.ConsoleLoggerColorDelegate) loggingC.EasyLogger {
2328
return housekeeping.NewDefaultHelperImplmentation(
24-
loggingP.NewConsoleLogger(loggingC.TypeDebug),
29+
loggingP.NewConsoleLogger(loggingC.TypeDebug, colors),
2530
)
2631
}
2732

@@ -32,6 +37,7 @@ func NewFileLogger() loggingC.EasyLogger {
3237
)
3338
}
3439

40+
// NewEasyLoggerForLogger -
3541
func NewEasyLoggerForLogger(logger loggingC.Logger) loggingC.EasyLogger {
3642
return housekeeping.NewDefaultHelperImplmentation(logger)
3743
}

Logging/tests/02-log-smart_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Test_02(t *testing.T) {
1515
loggingF.NewSimpleFormatterLogger(
1616
loggingM.NewMultiLogger(
1717
[]loggingC.Logger{
18-
loggingP.NewConsoleLogger(loggingC.TypeDebug),
18+
loggingP.NewConsoleLogger(loggingC.TypeDebug, loggingP.NewConsoleLoggerDefaultColors()),
1919
loggingP.NewFileLogger(loggingC.TypeDebug, "log.log"),
2020
},
2121
),

Logging/tests/03-log-async-and_or-buffered_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Test_03(t *testing.T) {
1717
loggingM.NewBufferedLogger(
1818
loggingM.NewMultiLogger(
1919
[]loggingC.Logger{
20-
loggingP.NewConsoleLogger(loggingC.TypeDebug),
20+
loggingP.NewConsoleLogger(loggingC.TypeDebug, loggingP.NewConsoleLoggerDefaultColors()),
2121
loggingP.NewFileLogger(loggingC.TypeDebug, "log.log"),
2222
},
2323
),

Service/system-service.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package Service
22

3-
import "fmt"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
47

58
// SystemService - represents a generic system service configuration
69
type SystemService interface {
@@ -38,6 +41,16 @@ type ServiceCommand struct {
3841
RunAsGroup string
3942
}
4043

44+
func (thisRef ServiceCommand) String() string {
45+
bytes, err := json.Marshal(thisRef)
46+
if err != nil {
47+
// INFO: in normal app you could log this
48+
return ""
49+
}
50+
51+
return string(bytes)
52+
}
53+
4154
// ServiceStatus is a generic representation of the service running on the system
4255
type ServiceStatus struct {
4356
Running bool
@@ -54,8 +67,8 @@ const (
5467
ServiceErrorOther = iota
5568
)
5669

57-
func (e ServiceErrorType) String() string {
58-
switch e {
70+
func (thisRef ServiceErrorType) String() string {
71+
switch thisRef {
5972
case ServiceErrorSuccess:
6073
return "Success"
6174

@@ -66,7 +79,7 @@ func (e ServiceErrorType) String() string {
6679
return "Other error occured"
6780

6881
default:
69-
return fmt.Sprintf("%d", int(e))
82+
return fmt.Sprintf("%d", int(thisRef))
7083
}
7184
}
7285

0 commit comments

Comments
 (0)