Skip to content

Commit 5ab82fa

Browse files
committed
Updated logrus to 1.0, simplified log messages
1 parent 63167c8 commit 5ab82fa

File tree

9 files changed

+62
-45
lines changed

9 files changed

+62
-45
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ go:
1111

1212
install:
1313
- go get -v -d
14-
- go get github.com/onsi/ginkgo/ginkgo
15-
- go get github.com/onsi/gomega
16-
14+
- go get -u github.com/onsi/ginkgo/ginkgo
15+
- go get -u github.com/onsi/gomega
16+
1717
script: ginkgo -r

aggregate_repository.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ package goengine
22

33
import (
44
"fmt"
5-
log "github.com/Sirupsen/logrus"
5+
6+
log "github.com/sirupsen/logrus"
67
)
78

89
type AggregateRepository interface {
@@ -21,7 +22,7 @@ func NewPublisherRepository(eventStore EventStore, eventBus VersionedEventPublis
2122
}
2223

2324
func (r *PublisherRepository) Load(id string, streamName StreamName) (*EventStream, error) {
24-
log.Debugf("Loading events from %s stream for aggregate %s", streamName, id)
25+
log.WithFields(log.Fields{"stream": streamName, "id": id}).Debug("Loading events from stream for aggregate")
2526
stream, err := r.EventStore.GetEventsFor(streamName, id)
2627
if nil != err {
2728
return nil, err
@@ -33,14 +34,14 @@ func (r *PublisherRepository) Load(id string, streamName StreamName) (*EventStre
3334
func (r *PublisherRepository) Save(aggregateRoot AggregateRoot, streamName StreamName) error {
3435
events := aggregateRoot.GetUncommittedEvents()
3536
eventStream := NewEventStream(streamName, events)
36-
log.Debugf("Saving %d events to %s stream", len(events), streamName)
37+
log.WithFields(log.Fields{"count": len(events), "stream": streamName}).Debug("Saving events to stream")
3738
err := r.EventStore.Append(eventStream)
3839
if nil != err {
3940
return err
4041
}
4142

4243
if nil == r.EventBus {
43-
log.Debug("Event bus not detected, skiping publishing events")
44+
log.Debug("Event bus not detected, skipping publishing events")
4445
return nil
4546
}
4647

@@ -52,7 +53,7 @@ func (r *PublisherRepository) Save(aggregateRoot AggregateRoot, streamName Strea
5253
}
5354

5455
func (r *PublisherRepository) Reconstitute(id string, source AggregateRoot, streamName StreamName) error {
55-
log.Debugf("Reconstituting aggregate %s from %s stream", id, streamName)
56+
log.WithFields(log.Fields{"stream": streamName, "id": id}).Debug("Reconstituting aggregate from stream")
5657
stream, err := r.Load(id, streamName)
5758
if nil != err {
5859
return err
@@ -68,6 +69,6 @@ func (r *PublisherRepository) Reconstitute(id string, source AggregateRoot, stre
6869
}
6970

7071
source.SetVersion(events[len(events)-1].Version)
71-
log.Debugf("Aggregate %s reconstituted", id)
72+
log.WithField("id", id).Debug("Aggregate reconstituted")
7273
return nil
7374
}

aggregate_root.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package goengine
33
import (
44
"fmt"
55

6-
log "github.com/Sirupsen/logrus"
76
"github.com/hellofresh/goengine/reflection"
87
"github.com/pborman/uuid"
8+
log "github.com/sirupsen/logrus"
99
)
1010

1111
type AggregateRoot interface {
@@ -17,10 +17,10 @@ type AggregateRoot interface {
1717
}
1818

1919
type AggregateRootBased struct {
20-
ID string
21-
version int
22-
source interface{}
23-
uncommitedEvents []*DomainMessage
20+
ID string
21+
version int
22+
source interface{}
23+
uncommittedEvents []*DomainMessage
2424
}
2525

2626
// NewAggregateRootBased constructor
@@ -46,18 +46,21 @@ func (r *AggregateRootBased) SetVersion(version int) {
4646
}
4747

4848
func (r *AggregateRootBased) GetUncommittedEvents() []*DomainMessage {
49-
stream := r.uncommitedEvents
50-
r.uncommitedEvents = nil
51-
log.Debugf("%d Uncommited events cleaned", len(stream))
49+
stream := r.uncommittedEvents
50+
r.uncommittedEvents = nil
51+
log.WithField("count", len(stream)).Debug("Uncommitted events cleaned")
5252

5353
return stream
5454
}
5555

5656
func (r *AggregateRootBased) Apply(event DomainEvent) {
5757
t := reflection.TypeOf(event)
58-
log.Debugf("source: %+v; MethodName: %+v; event: %+v", r.source, fmt.Sprintf("When%s", t.Name()), event)
59-
reflection.CallMethod(r.source, fmt.Sprintf("When%s", t.Name()), event)
60-
log.Debugf("Event %s applied", t.Name())
58+
methodName := fmt.Sprintf("When%s", t.Name())
59+
60+
entry := log.WithFields(log.Fields{"source": fmt.Sprintf("%+v", r.source), "method": methodName, "event": fmt.Sprintf("%+v", event)})
61+
entry.Debug("Applying event")
62+
reflection.CallMethod(r.source, methodName, event)
63+
entry.Debug("Event applied")
6164
}
6265

6366
func (r *AggregateRootBased) RecordThat(event DomainEvent) {
@@ -68,6 +71,6 @@ func (r *AggregateRootBased) RecordThat(event DomainEvent) {
6871

6972
func (r *AggregateRootBased) Record(event DomainEvent) {
7073
message := RecordNow(r.ID, r.version, event)
71-
r.uncommitedEvents = append(r.uncommitedEvents, message)
74+
r.uncommittedEvents = append(r.uncommittedEvents, message)
7275
log.Debug("Event recorded")
7376
}

cmd/goengine/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package main
33
import (
44
"os"
55

6-
log "github.com/Sirupsen/logrus"
76
"github.com/hellofresh/goengine"
87
"github.com/hellofresh/goengine/mongodb"
98
"github.com/hellofresh/goengine/rabbit"
10-
9+
log "github.com/sirupsen/logrus"
1110
"gopkg.in/mgo.v2"
1211
)
1312

@@ -16,7 +15,7 @@ func main() {
1615
var streamName goengine.StreamName = "test"
1716

1817
mongoDSN := os.Getenv("STORAGE_DSN")
19-
log.Infof("Connecting to the database %s", mongoDSN)
18+
log.WithField("dsn", mongoDSN).Debug("Connecting to the database")
2019
session, err := mgo.Dial(mongoDSN)
2120
if err != nil {
2221
log.Panic(err)

domain_message.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package goengine
22

3-
import "time"
3+
import (
4+
"fmt"
5+
"time"
6+
)
47

58
type DomainEvent interface {
69
OccurredOn() time.Time
@@ -13,6 +16,10 @@ type DomainMessage struct {
1316
RecordedOn time.Time `json:"recorded_on"`
1417
}
1518

19+
func (dm *DomainMessage) String() string {
20+
return fmt.Sprintf("DomainMessage{ ID: %s, Version: %d }", dm.ID, dm.Version)
21+
}
22+
1623
func NewDomainMessage(id string, version int, payload DomainEvent, recordedOn time.Time) *DomainMessage {
1724
return &DomainMessage{id, version, payload, recordedOn}
1825
}

event_dispatcher_manager.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package goengine
22

33
import (
4-
log "github.com/Sirupsen/logrus"
4+
log "github.com/sirupsen/logrus"
55
)
66

77
// VersionedEventDispatchManager is responsible for coordinating receiving messages
@@ -52,22 +52,25 @@ func (m *VersionedEventDispatchManager) Listen(stop <-chan bool, exclusive bool)
5252
// signifying successful processing of the message.
5353
// This should eventually call an event handler. See NewVersionedEventDispatcher()
5454
case event := <-receiveEventChannel:
55-
log.Debugf("EventDispatchManager.DispatchEvent: %s", event.Event)
55+
entry := log.WithField("event", event.Event)
56+
entry.Debugf("EventDispatchManager.DispatchEvent")
5657
if err := m.versionedEventDispatcher.DispatchEvent(event.Event); err != nil {
57-
log.Println("Error dispatching event: ", err)
58+
entry.WithError(err).Error("Error dispatching event")
5859
}
5960

6061
event.ProcessedSuccessfully <- true
61-
log.Debug("EventDispatchManager.DispatchSuccessful")
62+
entry.Debug("EventDispatchManager.DispatchSuccessful")
63+
6264
case <-stop:
6365
log.Debug("EventDispatchManager.Stopping")
6466
closeSignal := make(chan error)
6567
closeChannel <- closeSignal
66-
defer log.Debug("EventDispatchManager.Stopped")
68+
log.Debug("EventDispatchManager.Stopped")
6769
return <-closeSignal
68-
// Receiving on this channel signifys an error has occured worker processor side
70+
71+
// Receiving on this channel signifys an error has occurred worker processor side
6972
case err := <-errorChannel:
70-
log.Debugf("EventDispatchManager.ErrorReceived: %s", err)
73+
log.WithError(err).Debugf("EventDispatchManager.ErrorReceived")
7174
return err
7275
}
7376
}

rabbit/eventbus.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"fmt"
77
"time"
88

9-
log "github.com/Sirupsen/logrus"
109
"github.com/hellofresh/goengine"
1110
"github.com/hellofresh/goengine/reflection"
11+
log "github.com/sirupsen/logrus"
1212
"github.com/streadway/amqp"
1313
)
1414

@@ -136,7 +136,7 @@ func (bus *EventBus) ReceiveEvents(options goengine.VersionedEventReceiverOption
136136
conn, c, events, err = connR, cR, eventsR, errR
137137
}
138138

139-
log.Debug(err)
139+
log.WithError(err).Debug("Failed to reconnect to RabbitMQ")
140140

141141
return errR
142142
}, 5)

rabbit/retry.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,29 @@ import (
44
"math"
55
"time"
66

7-
log "github.com/Sirupsen/logrus"
7+
log "github.com/sirupsen/logrus"
88
)
99

1010
type function func() error
1111

1212
func exponential(operation function, maxRetries int) error {
1313
var err error
14-
var sleepTime int
14+
var sleepMs int
1515
for i := 0; i < maxRetries; i++ {
1616
err = operation()
1717
if err == nil {
1818
return nil
1919
}
20+
2021
if i == 0 {
21-
sleepTime = 1
22+
sleepMs = 1
2223
} else {
23-
sleepTime = int(math.Exp2(float64(i)) * 100)
24+
sleepMs = int(math.Exp2(float64(i)) * 100)
2425
}
25-
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
26-
log.Debugf("Retry exponential: Attempt %d, sleep %d", i, sleepTime)
26+
27+
sleepTime := time.Duration(sleepMs) * time.Millisecond
28+
time.Sleep(sleepTime)
29+
log.WithFields(log.Fields{"attempt": i, "sleep": sleepTime.String()}).Debug("Retry exponential")
2730
}
2831

2932
return err

type_registry.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package goengine
33
import (
44
"reflect"
55

6-
log "github.com/Sirupsen/logrus"
76
"github.com/hellofresh/goengine/errors"
87
"github.com/hellofresh/goengine/reflection"
8+
log "github.com/sirupsen/logrus"
99
)
1010

1111
// TypeRegistry is a registry for go types
@@ -35,15 +35,16 @@ func NewInMemmoryTypeRegistry() *InMemoryTypeRegistry {
3535
func (r *InMemoryTypeRegistry) RegisterType(i interface{}) {
3636
rawType := reflection.TypeOf(i)
3737
r.types[rawType.String()] = rawType
38-
log.Debugf("Type %s was registered", rawType.String())
38+
log.WithField("type", rawType.String()).Debug("Type was registered")
3939
}
4040

4141
func (r *InMemoryTypeRegistry) RegisterAggregate(aggregate AggregateRoot, events ...interface{}) {
4242
r.RegisterType(aggregate)
43-
log.Debugf("Aggregate %s was registered", aggregate.GetID())
43+
entry := log.WithField("aggregate", aggregate.GetID())
44+
entry.Debug("Aggregate was registered")
4445

4546
r.RegisterEvents(events)
46-
log.Debugf("%s events were registered for aggregate %s", len(events), aggregate.GetID())
47+
entry.WithField("count", len(events)).Debug("Events were registered for aggregate")
4748
}
4849

4950
func (r *InMemoryTypeRegistry) RegisterEvents(events ...interface{}) {
@@ -66,6 +67,6 @@ func (r *InMemoryTypeRegistry) Get(name string) (interface{}, error) {
6667
return reflect.New(typ).Interface(), nil
6768
}
6869

69-
log.Debugf("Type %s not found", name)
70+
log.WithField("type", name).Debug("Type not found")
7071
return nil, errors.ErrorTypeNotFound
7172
}

0 commit comments

Comments
 (0)