Slide 1

Slide 1 text

Software Design Patterns In the riveting context of Logging

Slide 2

Slide 2 text

No matter what anyone says... Everyone is a logger

Slide 3

Slide 3 text

The Lynch Me Pattern How a lot of us log

Slide 4

Slide 4 text

The Lynch Me Pattern Write this line at your own peril Doom FORCED logging How a lot of us log Dependency Injection

Slide 5

Slide 5 text

Never force a log! Logging should ALWAYS be optional

Slide 6

Slide 6 text

Design Pattern #1 The Decorator Pattern

Slide 7

Slide 7 text

Design Pattern #1 The Decorator Pattern 1 Remove the Logger

Slide 8

Slide 8 text

Design Pattern #1 The Decorator Pattern 1 Remove the Logger

Slide 9

Slide 9 text

Design Pattern #1 The Decorator Pattern 1 Remove the Logger 2 Place our http client as a property of a 'Decorator' object

Slide 10

Slide 10 text

2 Place our http client as a property of a 'Decorator' object Design Pattern #1 The Decorator Pattern The HttpClientService object

Slide 11

Slide 11 text

Design Pattern #1 The Decorator Pattern 1 Remove the Logger 2 Place our http client as a property of a 'Decorator' object 3 Forward all method calls to the decorated object with __call()

Slide 12

Slide 12 text

3 Forward all method calls to the decorated object with __call() Design Pattern #1 The Decorator Pattern

Slide 13

Slide 13 text

Design Pattern #1 The Decorator Pattern 1 Remove the Logger 2 Place our http client as a property of a 'Decorator' object 3 Forward all method calls to the decorated object with __call() 4 Put the logging in the decorator instead

Slide 14

Slide 14 text

4 Put the logging in the decorator instead Design Pattern #1 The Decorator Pattern

Slide 15

Slide 15 text

Design Pattern #1 The Decorator Pattern 1 Remove the Logger 2 Place our http client as a property of a 'Decorator' object 3 Forward all method calls to the decorated object with __call() 4 Put the logging in the decorator instead

Slide 16

Slide 16 text

Design Pattern #1 The Decorator Pattern • Object can be tested without mocking out a logger For the testing fanboys • Can be removed/modified without touching core code • You're not forcing anyone to log if they don't want to For normal developers For everyone • You use an industry standard pattern • You don't get hunted down and lynched

Slide 17

Slide 17 text

Design Pattern #1 The Decorator Pattern BUT, we're only logging what goes in and what comes out (Request) (Response) What about anything that happens within the __call() forwarding (Business Logic) We're not actually logging anything happening inside here

Slide 18

Slide 18 text

Design Pattern #2 The Observer Pattern

Slide 19

Slide 19 text

Design Pattern #2 The Observer Pattern 1 Add an observers class property to the object to be observed

Slide 20

Slide 20 text

Design Pattern #2 The Observer Pattern 1 Add an observers class property to the object to be observed Two Loggers with differing implementations Response will randomly be either 'success' or 'error' Add the observers array

Slide 21

Slide 21 text

Design Pattern #1 The Observer Pattern 1 Add an observers class property to the object to be observed 2 Implement \SplObserver, \SplSubject and required methods

Slide 22

Slide 22 text

Design Pattern #1 The Observer Pattern 2 Implement \SplObserver, \SplSubject and required methods \SplSubject (the client) \SplObserver (the logger)

Slide 23

Slide 23 text

Design Pattern #1 The Observer Pattern 1 Add an observers class property to the object to be observed 2 Implement \SplObserver, \SplSubject and required methods 3 Place notify() calls wherever you like!

Slide 24

Slide 24 text

Design Pattern #2 The Observer Pattern 3 Place notify() calls wherever you like! The observer will need to call getters to get the data it needs Loops around observers, runs "update" method on each one

Slide 25

Slide 25 text

Design Pattern #2 The Observer Pattern • Add / remove observer loggers, nothing changes • You're not forcing anyone to log if they don't want to For normal developers For everyone • You use an industry standard pattern • You don't get hunted down and lynched

Slide 26

Slide 26 text

Logging Don't force it, anywhere! Be a good logger Record inputs and outputs only ---> Decorator Record business logic results ---> Observer

Slide 27

Slide 27 text

Follow and/or stalk @J7mbo