-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Event and Structured Logger
- Loading branch information
Showing
5 changed files
with
173 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package events | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/suared/core/types" | ||
) | ||
|
||
var debugLogger zerolog.Logger | ||
var eventLogger zerolog.Logger | ||
|
||
func init() { | ||
//For Developer Output, not intended to be removed, create new method/level if have temp only need but try to avoid to extent possible outside dev env | ||
debugLogger = zerolog.New(os.Stderr).With().Timestamp().Str("type", "MSG").Str("level", "DEBUG").Logger() | ||
//Events to integrate into event processes | ||
eventLogger = zerolog.New(os.Stderr).With().Timestamp().Str("type", "EVENT").Str("level", "EVENT").Logger() | ||
|
||
} | ||
|
||
//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) { | ||
if keyVals == nil { | ||
debugLogger.Log().Str("loc", loc).Msg(msg) | ||
} else { | ||
//dictionary, string, int, float | ||
logger := debugLogger.Log().Str("loc", loc) | ||
for _, keyVal := range keyVals { | ||
switch keyVal.Typ { | ||
case types.TYPESTRING: | ||
logger = logger.Str(keyVal.Key, keyVal.Val) | ||
case types.TYPEINT: | ||
logger = logger.Int(keyVal.Key, keyVal.ValueAsInt()) | ||
case types.TYPEFLOAT: | ||
logger = logger.Float64(keyVal.Key, keyVal.ValueAsFloat()) | ||
} | ||
|
||
} | ||
logger.Msg(msg) | ||
} | ||
|
||
} | ||
|
||
//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) { | ||
if keyVals == nil { | ||
eventLogger.Log().Str("loc", loc).Msg(msg) | ||
} else { | ||
//dictionary, string, int, float | ||
logger := eventLogger.Log().Str("loc", loc) | ||
for _, keyVal := range keyVals { | ||
switch keyVal.Typ { | ||
case types.TYPESTRING: | ||
logger = logger.Str(keyVal.Key, keyVal.Val) | ||
case types.TYPEINT: | ||
logger = logger.Int(keyVal.Key, keyVal.ValueAsInt()) | ||
case types.TYPEFLOAT: | ||
logger = logger.Float64(keyVal.Key, keyVal.ValueAsFloat()) | ||
} | ||
|
||
} | ||
logger.Msg(msg) | ||
} | ||
|
||
} |
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,18 @@ | ||
package events | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/suared/core/types" | ||
) | ||
|
||
func TestEvents(t *testing.T) { | ||
Debug("testA.x", "Whatever") | ||
time.Sleep(1 * time.Second) | ||
Debug("testB.x", "Dude") | ||
time.Sleep(1 * time.Second) | ||
Debug("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)) | ||
} |
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,65 @@ | ||
package types | ||
|
||
import ( | ||
"log" | ||
"strconv" | ||
) | ||
|
||
const ( | ||
//TYPESTRING string | ||
TYPESTRING = iota | ||
//TYPEINT int | ||
TYPEINT | ||
//TYPEFLOAT float | ||
TYPEFLOAT | ||
//TYPEDICT dict | ||
TYPEDICT | ||
) | ||
|
||
//KeyVal - Generic Key/Value struct for the library | ||
type KeyVal struct { | ||
Key string | ||
Val string | ||
Typ uint16 | ||
} | ||
|
||
//ValueAsInt return int | ||
func (keyVal KeyVal) ValueAsInt() int { | ||
res, err := strconv.Atoi(keyVal.Val) | ||
if err != nil { | ||
log.Printf("Error: Unable to convert value to Int for KeyValAsInt, received: %v, sending -1", keyVal.Val) | ||
res = -1 | ||
} | ||
return res | ||
} | ||
|
||
//ValueAsFloat return float | ||
func (keyVal KeyVal) ValueAsFloat() float64 { | ||
res, err := strconv.ParseFloat(keyVal.Val, 5) | ||
if err != nil { | ||
log.Printf("Error: Unable to convert value to Float for KeyValAsFloat, received: %v, sending -1", keyVal.Val) | ||
res = -1 | ||
} | ||
return res | ||
} | ||
|
||
//Str keyVal | ||
func Str(key string, val string) KeyVal { | ||
return KeyVal{Key: key, Val: val, Typ: TYPESTRING} | ||
} | ||
|
||
//Int keyVal | ||
func Int(key string, val int) KeyVal { | ||
return KeyVal{Key: key, Val: strconv.Itoa(val), Typ: TYPEINT} | ||
} | ||
|
||
//Float keyVal | ||
func Float(key string, val float64) KeyVal { | ||
return KeyVal{Key: key, Val: strconv.FormatFloat(val, 'f', 5, 64), Typ: TYPEFLOAT} | ||
} | ||
|
||
/* do this later, don't thinkI will need in short run, probably add a val array that the type then dictates to undo | ||
func NewDictKeyVal(key string, vals ...KeyVal) KeyVal { | ||
return KeyVal{Key: key, Val: val, Typ: TYPEDICT} | ||
} | ||
*/ |