-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
204 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package loggers | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/lerenn/asyncapi-codegen/pkg/extensions" | ||
) | ||
|
||
func insertLogIntoStruct(key, value string, m map[string]any) map[string]any { | ||
// Split key | ||
l := strings.Split(key, ".") | ||
|
||
// Check if there is no depth, just add it to the map | ||
if len(l) == 1 { | ||
m[key] = value | ||
return m | ||
} | ||
|
||
// Check if the submap exists, otherwise create it | ||
var subm map[string]any | ||
if v, ok := m[l[0]]; !ok { | ||
subm = make(map[string]any) | ||
} else { | ||
subm, ok = v.(map[string]any) | ||
if !ok { | ||
// Explicitly drop the old value | ||
subm = make(map[string]any) | ||
} | ||
} | ||
|
||
// Insert the log into the submap | ||
subm = insertLogIntoStruct(strings.Join(l[1:], "."), value, subm) | ||
|
||
// Insert the submap into the map | ||
m[l[0]] = subm | ||
|
||
return m | ||
} | ||
|
||
func structureLogs(info []extensions.LogInfo) map[string]any { | ||
structuredLog := make(map[string]any) | ||
for _, logInfo := range info { | ||
structuredLog = insertLogIntoStruct(logInfo.Key, fmt.Sprintf("%+v", logInfo.Value), structuredLog) | ||
} | ||
return structuredLog | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package loggers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/lerenn/asyncapi-codegen/pkg/extensions" | ||
) | ||
|
||
// Text is a logger that will print logs in Elastic Common Schema format. | ||
type Text struct { | ||
boldRedPrinter *color.Color | ||
boldOrangePrinter *color.Color | ||
boldWhitePrinter *color.Color | ||
greyPrinter *color.Color | ||
} | ||
|
||
// NewText creates a new Human logger. | ||
func NewText() Text { | ||
// Create red color | ||
red := color.New(color.FgHiRed) | ||
boldRed := red.Add(color.Bold) | ||
|
||
// Create orange color | ||
orange := color.New(color.FgHiYellow) | ||
boldOrange := orange.Add(color.Bold) | ||
|
||
// Create white color | ||
white := color.New(color.FgWhite) | ||
boldWhite := white.Add(color.Bold) | ||
|
||
return Text{ | ||
boldRedPrinter: boldRed, | ||
boldOrangePrinter: boldOrange, | ||
boldWhitePrinter: boldWhite, | ||
greyPrinter: color.New(color.FgHiBlack), | ||
} | ||
} | ||
|
||
func (tl Text) humanizeStructuredLogs(sl map[string]any, msgFmt *color.Color, prefixes ...string) string { | ||
var s string | ||
joinedPrefixes := strings.Join(prefixes, "") | ||
|
||
// Put timestamp and message first if it tsExists | ||
ts, tsExists := sl["@Timestamp"] | ||
msg, msgExists := sl["Message"] | ||
if tsExists && msgExists { | ||
s += msgFmt.Sprintf("> %s%s: %s\n", joinedPrefixes, ts, msg) | ||
delete(sl, "@Timestamp") | ||
delete(sl, "Message") | ||
return s + tl.humanizeStructuredLogs(sl, msgFmt, append(prefixes, " ")...) | ||
} | ||
|
||
// Generate other keys | ||
for k, v := range sl { | ||
switch tv := v.(type) { | ||
case map[string]any: | ||
children := tl.humanizeStructuredLogs(tv, msgFmt, append(prefixes, " ")...) | ||
s += tl.greyPrinter.Sprintf("%s%s:\n%s", joinedPrefixes, k, children) | ||
default: | ||
s += tl.greyPrinter.Sprintf("%s%s: %v\n", joinedPrefixes, k, tv) | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func (tl Text) setInfoFromContext(ctx context.Context, msg string, info ...extensions.LogInfo) []extensions.LogInfo { | ||
// Add additional keys from context | ||
extensions.IfContextSetWith(ctx, extensions.ContextKeyIsChannel, func(value any) { | ||
info = append(info, extensions.LogInfo{Key: "Channel", Value: value}) | ||
}) | ||
extensions.IfContextSetWith(ctx, extensions.ContextKeyIsCorrelationID, func(value any) { | ||
info = append(info, extensions.LogInfo{Key: "CorrelationID", Value: value}) | ||
}) | ||
extensions.IfContextSetWith(ctx, extensions.ContextKeyIsMessage, func(value any) { | ||
info = append(info, extensions.LogInfo{Key: "Content", Value: value}) | ||
}) | ||
|
||
// Add additional keys | ||
info = append(info, extensions.LogInfo{ | ||
Key: "Message", | ||
Value: msg, | ||
}) | ||
info = append(info, extensions.LogInfo{ | ||
Key: "@Timestamp", | ||
Value: time.Now().UTC().Format(time.RFC3339Nano), | ||
}) | ||
|
||
// Return info | ||
return info | ||
} | ||
|
||
func (tl Text) formatLog(ctx context.Context, msgFmt *color.Color, msg string, info ...extensions.LogInfo) string { | ||
// Set additional fields | ||
info = tl.setInfoFromContext(ctx, msg, info...) | ||
|
||
// Structure log | ||
sl := structureLogs(info) | ||
|
||
// Humanize structured logs | ||
return tl.humanizeStructuredLogs(sl, msgFmt) | ||
} | ||
|
||
// Info logs a message at info level with context and additional info. | ||
func (tl Text) Info(ctx context.Context, msg string, info ...extensions.LogInfo) { | ||
fmt.Println(tl.formatLog(ctx, tl.boldWhitePrinter, msg, info...)) | ||
} | ||
|
||
// Warning logs a message at warning level with context and additional info. | ||
func (tl Text) Warning(ctx context.Context, msg string, info ...extensions.LogInfo) { | ||
fmt.Println(tl.formatLog(ctx, tl.boldOrangePrinter, msg, info...)) | ||
} | ||
|
||
// Error logs a message at error level with context and additional info. | ||
func (tl Text) Error(ctx context.Context, msg string, info ...extensions.LogInfo) { | ||
fmt.Println(tl.formatLog(ctx, tl.boldRedPrinter, msg, info...)) | ||
} |