-
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.
(r) LogMessage is extended map[string]interface{}
Showing
6 changed files
with
74 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import ( | |
) | ||
|
||
type Consumer struct { | ||
queue chan *LogMessage | ||
queue chan LogMessage | ||
|
||
BaseConsumer | ||
} | ||
|
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,51 @@ | ||
package main | ||
|
||
import log "github.com/sirupsen/logrus" | ||
|
||
var ( | ||
HostFieldName string // name of "host" field in json log message | ||
MessageFieldName string // name of "message" field in json log message | ||
RouteFieldName string // name of "route" field in json log message | ||
) | ||
|
||
func init() { | ||
HostFieldName = GetenvStr("FLUX_HOST_FIELD_NAME", "HOST") | ||
MessageFieldName = GetenvStr("FLUX_MESSAGE_FIELD_NAME", "MESSAGE") | ||
RouteFieldName = GetenvStr("FLUX_ROUTE_FIELD_NAME", "ROUTE") | ||
} | ||
|
||
type LogMessage map[string]interface{} | ||
|
||
func (l LogMessage) Host() string { | ||
return l.getFieldStr(HostFieldName) | ||
} | ||
|
||
func (l LogMessage) Message() string { | ||
return l.getFieldStr(MessageFieldName) | ||
} | ||
|
||
func (l LogMessage) Route() string { | ||
return l.getFieldStr(RouteFieldName) | ||
} | ||
|
||
func (l LogMessage) Validate() bool { | ||
return l.hasField(HostFieldName) && | ||
l.hasField(MessageFieldName) && | ||
l.hasField(RouteFieldName) | ||
} | ||
|
||
func (l LogMessage) getFieldStr(name string) string { | ||
if value, ok := l[name].(string); ok { | ||
return value | ||
} else { | ||
log.WithField("field_name", name). | ||
WithField("value", l[name]). | ||
Error("can't find/convert field from JSON to string") | ||
return "" | ||
} | ||
} | ||
|
||
func (l LogMessage) hasField(name string) bool { | ||
_, found := l[name] | ||
return found | ||
} |
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