Skip to content

Commit 8129109

Browse files
authored
feat: pretty print (#109)
1 parent 77e4ce1 commit 8129109

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

internals/config/loader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/codeshelldev/secured-signal-api/internals/config/structure"
1111
"github.com/codeshelldev/secured-signal-api/utils/configutils"
12-
jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils"
1312
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1413

1514
"github.com/knadh/koanf/parsers/yaml"
@@ -58,9 +57,11 @@ func Load() {
5857
InitEnv()
5958

6059
log.Info("Finished Loading Configuration")
60+
}
6161

62-
log.Dev("Loaded Config:\n" + jsonutils.ToJson(mainConf.Layer.All()))
63-
log.Dev("Loaded Token Configs:\n" + jsonutils.ToJson(tokenConf.Layer.All()))
62+
func Log() {
63+
log.Dev("Loaded Config:", mainConf.Layer.All())
64+
log.Dev("Loaded Token Configs:", tokenConf.Layer.All())
6465
}
6566

6667
func Clear() {

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func main() {
3737
log.Dev("CTRL+S config to Print to Console")
3838
}
3939

40+
config.Log()
41+
4042
proxy = reverseProxy.Create(ENV.API_URL)
4143

4244
handler := proxy.Init()

utils/jsonutils/jsonutils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ func ToJson[T any](obj T) string {
7676

7777
return string(bytes)
7878
}
79+
80+
func Pretty[T any](obj T) string {
81+
bytes, _ := json.MarshalIndent(obj, "", " ")
82+
83+
return string(bytes)
84+
}

utils/logger/logger.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package logger
22

33
import (
44
"fmt"
5+
"image/color"
6+
"strconv"
57
"strings"
68

9+
"github.com/codeshelldev/secured-signal-api/utils/jsonutils"
710
"go.uber.org/zap"
811
"go.uber.org/zap/zapcore"
912
)
@@ -47,36 +50,55 @@ func Init(level string) {
4750
}
4851
}
4952

53+
func Format(data ...any) string {
54+
res := ""
55+
56+
for _, item := range data {
57+
switch value := item.(type) {
58+
case string:
59+
res += value
60+
case int:
61+
res += strconv.Itoa(value)
62+
default:
63+
res += "\n" + ColorCode(jsonutils.Pretty(value), color.RGBA{
64+
R: 0, G: 215, B: 135,
65+
})
66+
}
67+
}
68+
69+
return res
70+
}
71+
5072
func Level() string {
5173
return LevelString(_log.Level())
5274
}
5375

54-
func Info(msg ...string) {
55-
_log.Info(strings.Join(msg, ""))
76+
func Info(data ...any) {
77+
_log.Info(Format(data...))
5678
}
5779

58-
func Debug(msg ...string) {
59-
_log.Debug(strings.Join(msg, ""))
80+
func Debug(data ...any) {
81+
_log.Debug(Format(data...))
6082
}
6183

62-
func Dev(msg ...string) {
63-
ok := _log.Check(DeveloperLevel, strings.Join(msg, ""))
84+
func Dev(data ...any) {
85+
ok := _log.Check(DeveloperLevel, Format(data...))
6486

6587
if ok != nil {
6688
ok.Write()
6789
}
6890
}
6991

70-
func Error(msg ...string) {
71-
_log.Error(strings.Join(msg, ""))
92+
func Error(data ...any) {
93+
_log.Error(Format(data...))
7294
}
7395

74-
func Fatal(msg ...string) {
75-
_log.Fatal(strings.Join(msg, ""))
96+
func Fatal(data ...any) {
97+
_log.Fatal(Format(data...))
7698
}
7799

78-
func Warn(msg ...string) {
79-
_log.Warn(strings.Join(msg, ""))
100+
func Warn(data ...any) {
101+
_log.Warn(Format(data...))
80102
}
81103

82104
func Sync() {

0 commit comments

Comments
 (0)