Skip to content

Commit

Permalink
Add context to enable userID to be auto printed and any future needed…
Browse files Browse the repository at this point in the history
… context items
  • Loading branch information
suared committed Jun 27, 2021
1 parent 56e9fc0 commit e2dcadb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
24 changes: 18 additions & 6 deletions events/events.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package events

import (
"context"
"os"

"github.com/rs/zerolog"
"github.com/suared/core/security"
"github.com/suared/core/types"
)

Expand All @@ -20,12 +22,17 @@ func init() {

//Debug write debug message to stdout
//Using Loc as user provided to enable flex on how to define - e.g. domain.method or unique identifier, etc as makes sense. Will ensure it is always thought about to start
func Debug(loc string, msg string, keyVals ...types.KeyVal) {
func Debug(ctx context.Context, loc string, msg string, keyVals ...types.KeyVal) {
auth := security.GetAuth(ctx)
userID := ""
if auth != nil {
userID = auth.GetUser()
}
if keyVals == nil {
debugLogger.Log().Str("loc", loc).Msg(msg)
debugLogger.Log().Str("loc", loc).Str("userID", userID).Msg(msg)
} else {
//dictionary, string, int, float
logger := debugLogger.Log().Str("loc", loc)
logger := debugLogger.Log().Str("loc", loc).Str("userID", userID)
for _, keyVal := range keyVals {
switch keyVal.Typ {
case types.TYPESTRING:
Expand All @@ -44,12 +51,17 @@ func Debug(loc string, msg string, keyVals ...types.KeyVal) {

//Event write event message to stdout - note: events are handled outside the scope of the module by design
//See debug comments, same concept here, is just marked as event to enable later processing of event stream
func Event(loc string, msg string, keyVals ...types.KeyVal) {
func Event(ctx context.Context, loc string, msg string, keyVals ...types.KeyVal) {
auth := security.GetAuth(ctx)
userID := ""
if auth != nil {
userID = auth.GetUser()
}
if keyVals == nil {
eventLogger.Log().Str("loc", loc).Msg(msg)
eventLogger.Log().Str("loc", loc).Str("userID", userID).Msg(msg)
} else {
//dictionary, string, int, float
logger := eventLogger.Log().Str("loc", loc)
logger := eventLogger.Log().Str("loc", loc).Str("userID", userID)
for _, keyVal := range keyVals {
switch keyVal.Typ {
case types.TYPESTRING:
Expand Down
14 changes: 10 additions & 4 deletions events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import (
)

func TestEvents(t *testing.T) {
Debug("testA.x", "Whatever")
Debug(nil, "testA.x", "Whatever")
time.Sleep(1 * time.Second)
Debug("testB.x", "Dude")
Debug(nil, "testB.x", "Dude")
time.Sleep(1 * time.Second)
Debug("test1.x", "First Test!", types.Str("name", "david"), types.Int("age", 46))
Debug(nil, "test1.x", "First Test!", types.Str("name", "david"), types.Int("age", 46))
time.Sleep(1 * time.Second)
Event("test2.x", "Second Test!", types.Str("name", "colleen"), types.Int("age", 48))
Event(nil, "test2.x", "Second Test!", types.Str("name", "colleen"), types.Int("age", 48))
Event(nil, "test3.1", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48))
Event(nil, "test3.2", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48))
Event(nil, "test3.3", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48))
Event(nil, "test3.4", "Third Test!", types.Str("name", "colleen"), types.Int("age", 48))
//doesn't drop, good - uncomment to test in future if create buffer
//panic("not drop test3.4")
}
3 changes: 3 additions & 0 deletions security/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (t *BasicAuth) GetAuthHeader() string {

//GetAuth - returns Auth from the provided context
func GetAuth(ctx context.Context) Auth {
if ctx == nil {
return nil
}
authKey := ctx.Value(authKey)
if authKey == nil {
return nil
Expand Down

0 comments on commit e2dcadb

Please sign in to comment.