Skip to content

Commit

Permalink
add uuid to log
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kruse committed Aug 15, 2021
1 parent d687bf7 commit a8b438c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/Placons/oneapp-logger
go 1.14

require (
github.com/antonfisher/nested-logrus-formatter v1.3.0 // indirect
github.com/antonfisher/nested-logrus-formatter v1.3.0
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.7.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/antonfisher/nested-logrus-formatter v1.3.0 h1:8zixYquU1Odk+vzAaAQPAdR
github.com/antonfisher/nested-logrus-formatter v1.3.0/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
Expand Down
9 changes: 9 additions & 0 deletions logger/audit_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ type AuditValue interface {

type OperationValue AuditValue

type AuditUUIDValue AuditValue

func Operation(value interface{}) OperationValue {
return AuditElement{
key: "operation",
value: value,
}
}

func AuditUUID(value string) AuditUUIDValue {
return AuditElement{
key: "audit-uuid",
value: value,
}
}

func Generic(key string, value interface{}) AuditValue {
return AuditElement{
key: key,
Expand Down
17 changes: 13 additions & 4 deletions logger/audit_logwrapper.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package logger

import "github.com/google/uuid"

type AuditLogger struct {
sl StandardLogger
operation OperationValue
uuid AuditUUIDValue
}

func newAuditLogger(l *StandardLogger, operation string) *AuditLogger {
var standardLogger = &AuditLogger{sl: *l, operation: Operation(operation)}
return standardLogger
u, err := uuid.NewUUID()
if err == nil {
var standardLogger = &AuditLogger{sl: *l, operation: Operation(operation), uuid: AuditUUID(u.String())}
return standardLogger
} else {
var standardLogger = &AuditLogger{sl: *l, operation: Operation(operation), uuid: AuditUUID("error")}
return standardLogger
}
}

func (l *AuditLogger) Audit(message string) {
l.sl.AuditWithOperation(message, l.operation)
l.sl.AuditWithOperationAndUUIDAndFields(message, l.operation, l.uuid)
}

func (l *AuditLogger) Start(message string, vs ...AuditValue) {
Expand All @@ -25,5 +34,5 @@ func (l *AuditLogger) End(message string, vs ...AuditValue) {
}

func (l *AuditLogger) AuditWithFields(message string, vs ...AuditValue) {
l.sl.AuditWithOperationAndFields(message, l.operation, vs...)
l.sl.AuditWithOperationAndUUIDAndFields(message, l.operation, l.uuid, vs...)
}
8 changes: 8 additions & 0 deletions logger/logwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ func (l *StandardLogger) AuditWithOperation(message string, operation OperationV
}

func (l *StandardLogger) AuditWithOperationAndFields(message string, operation OperationValue, vs ...AuditValue) {
l.AuditWithOperationAndUUIDAndFields(message, operation, nil, vs...)
}

func (l *StandardLogger) AuditWithOperationAndUUIDAndFields(message string, operation OperationValue, u AuditUUIDValue, vs ...AuditValue) {
fields := map[string]interface{}{}
fields["audit"] = true
opKey, opValue := operation.Get()
fields[opKey] = opValue
if u != nil {
uKey, uValue := u.Get()
fields[uKey] = uValue
}

// log level will actually be exchanged in the audit hook
for _, a := range vs {
Expand Down

0 comments on commit a8b438c

Please sign in to comment.