Init Eslogad
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package appcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type key string
|
||||
|
||||
const (
|
||||
CorrelationIDKey = key("CorrelationID")
|
||||
OrganizationIDKey = key("OrganizationIDKey")
|
||||
UserIDKey = key("UserID")
|
||||
OutletIDKey = key("OutletID")
|
||||
RoleIDKey = key("RoleID")
|
||||
AppVersionKey = key("AppVersion")
|
||||
AppIDKey = key("AppID")
|
||||
AppTypeKey = key("AppType")
|
||||
PlatformKey = key("platform")
|
||||
DeviceOSKey = key("deviceOS")
|
||||
UserLocaleKey = key("userLocale")
|
||||
UserRoleKey = key("userRole")
|
||||
)
|
||||
|
||||
func LogFields(ctx interface{}) map[string]interface{} {
|
||||
fields := make(map[string]interface{})
|
||||
fields[string(CorrelationIDKey)] = value(ctx, CorrelationIDKey)
|
||||
fields[string(OrganizationIDKey)] = value(ctx, OrganizationIDKey)
|
||||
fields[string(OutletIDKey)] = value(ctx, OutletIDKey)
|
||||
fields[string(AppVersionKey)] = value(ctx, AppVersionKey)
|
||||
fields[string(AppIDKey)] = value(ctx, AppIDKey)
|
||||
fields[string(AppTypeKey)] = value(ctx, AppTypeKey)
|
||||
fields[string(UserIDKey)] = value(ctx, UserIDKey)
|
||||
fields[string(PlatformKey)] = value(ctx, PlatformKey)
|
||||
fields[string(DeviceOSKey)] = value(ctx, DeviceOSKey)
|
||||
fields[string(UserLocaleKey)] = value(ctx, UserLocaleKey)
|
||||
return fields
|
||||
}
|
||||
|
||||
func value(ctx interface{}, key key) string {
|
||||
switch c := ctx.(type) {
|
||||
case *gin.Context:
|
||||
return getFromGinContext(c, key)
|
||||
case context.Context:
|
||||
return getFromGoContext(c, key)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func uuidValue(ctx interface{}, key key) uuid.UUID {
|
||||
switch c := ctx.(type) {
|
||||
case *gin.Context:
|
||||
val, _ := uuid.Parse(getFromGinContext(c, key))
|
||||
return val
|
||||
case context.Context:
|
||||
val, _ := uuid.Parse(getFromGoContext(c, key))
|
||||
return val
|
||||
default:
|
||||
return uuid.New()
|
||||
}
|
||||
}
|
||||
|
||||
func getFromGinContext(c *gin.Context, key key) string {
|
||||
keyStr := string(key)
|
||||
if val, exists := c.Get(keyStr); exists {
|
||||
if str, ok := val.(string); ok {
|
||||
return str
|
||||
}
|
||||
}
|
||||
return getFromGoContext(c.Request.Context(), key)
|
||||
}
|
||||
|
||||
func getFromGoContext(ctx context.Context, key key) string {
|
||||
if val, ok := ctx.Value(key).(string); ok {
|
||||
return val
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package appcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type logCtxKeyType struct{}
|
||||
|
||||
var logCtxKey = logCtxKeyType(struct{}{})
|
||||
|
||||
type Logger struct {
|
||||
*logrus.Logger
|
||||
}
|
||||
|
||||
var log *Logger
|
||||
|
||||
type ContextInfo struct {
|
||||
CorrelationID string
|
||||
UserID uuid.UUID
|
||||
OrganizationID uuid.UUID
|
||||
OutletID uuid.UUID
|
||||
AppVersion string
|
||||
AppID string
|
||||
AppType string
|
||||
Platform string
|
||||
DeviceOS string
|
||||
UserLocale string
|
||||
UserRole string
|
||||
}
|
||||
|
||||
type ctxKeyType struct{}
|
||||
|
||||
var ctxKey = ctxKeyType(struct{}{})
|
||||
|
||||
func NewAppContext(ctx context.Context, info *ContextInfo) context.Context {
|
||||
ctx = NewContext(ctx, map[string]interface{}{
|
||||
"correlation_id": info.CorrelationID,
|
||||
"user_id": info.UserID,
|
||||
"app_version": info.AppVersion,
|
||||
"app_id": info.AppID,
|
||||
"app_type": info.AppType,
|
||||
"platform": info.Platform,
|
||||
"device_os": info.DeviceOS,
|
||||
"user_locale": info.UserLocale,
|
||||
})
|
||||
return context.WithValue(ctx, ctxKey, info)
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, baseFields map[string]interface{}) context.Context {
|
||||
entry, ok := ctx.Value(logCtxKey).(*logrus.Entry)
|
||||
if !ok {
|
||||
entry = log.WithFields(map[string]interface{}{})
|
||||
}
|
||||
|
||||
return context.WithValue(ctx, logCtxKey, entry.WithFields(baseFields))
|
||||
}
|
||||
|
||||
func FromGinContext(ctx context.Context) *ContextInfo {
|
||||
return &ContextInfo{
|
||||
CorrelationID: value(ctx, CorrelationIDKey),
|
||||
UserID: uuidValue(ctx, UserIDKey),
|
||||
OutletID: uuidValue(ctx, OutletIDKey),
|
||||
OrganizationID: uuidValue(ctx, OrganizationIDKey),
|
||||
AppVersion: value(ctx, AppVersionKey),
|
||||
AppID: value(ctx, AppIDKey),
|
||||
AppType: value(ctx, AppTypeKey),
|
||||
Platform: value(ctx, PlatformKey),
|
||||
DeviceOS: value(ctx, DeviceOSKey),
|
||||
UserLocale: value(ctx, UserLocaleKey),
|
||||
UserRole: value(ctx, UserRoleKey),
|
||||
}
|
||||
}
|
||||
|
||||
func FromContext(ctx context.Context) *ContextInfo {
|
||||
if info, ok := ctx.Value(ctxKey).(*ContextInfo); ok {
|
||||
return info
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user