Slide 1

Slide 1 text

Logging: Your new best friend Michael Heap (@mheap) Developer at DataSift Presented at php|tek, May 2016

Slide 2

Slide 2 text

Me! I’m Michael I’m @mheap Developer at DataSift

Slide 3

Slide 3 text

@mheap https://joind.in/17042

Slide 4

Slide 4 text

Logging

Slide 5

Slide 5 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 6

Slide 6 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 7

Slide 7 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 8

Slide 8 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 9

Slide 9 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 10

Slide 10 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 11

Slide 11 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 12

Slide 12 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 13

Slide 13 text

Sound good?

Slide 14

Slide 14 text

Good!

Slide 15

Slide 15 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 16

Slide 16 text

Why log?

Slide 17

Slide 17 text

What went wrong? (Error log)

Slide 18

Slide 18 text

Who visited us? (Access log)

Slide 19

Slide 19 text

Who enabled ? (Audit log)

Slide 20

Slide 20 text

Runtime documentation (Application log)

Slide 21

Slide 21 text

I’m sold!

Slide 22

Slide 22 text

Can I have it for free?

Slide 23

Slide 23 text

Actually, yes!

Slide 24

Slide 24 text

(And more)

Slide 25

Slide 25 text

But that doesn’t help my application

Slide 26

Slide 26 text

Two types of log

Slide 27

Slide 27 text

Human readable

Slide 28

Slide 28 text

Machine readable

Slide 29

Slide 29 text

We should log both

Slide 30

Slide 30 text

What is an application log?

Slide 31

Slide 31 text

Debug information

Slide 32

Slide 32 text

Narrative information

Slide 33

Slide 33 text

Business information

Slide 34

Slide 34 text

“An application log signposts every twist and turn through the code”

Slide 35

Slide 35 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 36

Slide 36 text

Four W’s

Slide 37

Slide 37 text

When?

Slide 38

Slide 38 text

Who?

Slide 39

Slide 39 text

Where?

Slide 40

Slide 40 text

Why?

Slide 41

Slide 41 text

Getting started

Slide 42

Slide 42 text

error_log()

Slide 43

Slide 43 text

Slide 44

Slide 44 text

Slide 45

Slide 45 text

Slide 46

Slide 46 text

Pros ✴ It’s built in

Slide 47

Slide 47 text

Cons ✴ Is it semantically correct? ✴ Errors mixed with informational logs ✴ It’s not very powerful

Slide 48

Slide 48 text

Logging frameworks

Slide 49

Slide 49 text

1) Monolog 2) Everything else

Slide 50

Slide 50 text

pushHandler(new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG)); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 51

Slide 51 text

pushHandler(new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG)); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 52

Slide 52 text

pushHandler(new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG)); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 53

Slide 53 text

pushHandler(new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG)); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 54

Slide 54 text

pushHandler(new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG)); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 55

Slide 55 text

[2016-05-25 03:56:01] casino-app.INFO: Consonants in Michael: 4 [] []

Slide 56

Slide 56 text

FingersCrossedHandler

Slide 57

Slide 57 text

$log = new Monolog\Logger('casino-app'); $streamHandler = new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG); $fcHandler = new Monolog\Handler\FingersCrossedHandler($streamHandler, Monolog \Logger::ERROR); $log->pushHandler($fcHandler); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 58

Slide 58 text

$log = new Monolog\Logger('casino-app'); $streamHandler = new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG); $fcHandler = new Monolog\Handler\FingersCrossedHandler($streamHandler, Monolog \Logger::ERROR); $log->pushHandler($fcHandler); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 59

Slide 59 text

$log = new Monolog\Logger('casino-app'); $streamHandler = new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG); $fcHandler = new Monolog\Handler\FingersCrossedHandler($streamHandler, Monolog \Logger::ERROR); $log->pushHandler($fcHandler); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); return $c; } echo countConsonants("Michael", $log);

Slide 60

Slide 60 text

$log = new Monolog\Logger('casino-app'); $streamHandler = new Monolog\Handler\StreamHandler('/tmp/app.log', Monolog \Logger::DEBUG); $fcHandler = new Monolog\Handler\FingersCrossedHandler($streamHandler, Monolog \Logger::ERROR); $log->pushHandler($fcHandler); function countConsonants($str, $log){ $c = strlen(str_replace(['a','e','i','o','u'],'', $str)); $log->info("Consonants in {$str}: {$c}"); $log->error("Something bad happened"); return $c; } echo countConsonants("Michael", $log);

Slide 61

Slide 61 text

Pros ✴ It’s an object! Dependency injection FTW ✴ Supports multiple log writers ✴ Log level support

Slide 62

Slide 62 text

Cons ✴ Instantiating an instance can be complicated

Slide 63

Slide 63 text

Error Levels

Slide 64

Slide 64 text

0. Emergency System is unusable 1. Alert Should be corrected immediately 2. Critical Critical conditions 3. Error Error conditions 4. Warning May indicate that an error will occur if action is not taken. 5. Notice Events that are unusual, but not error conditions. 6. Informational Normal operational messages that require no action. 7. Debug Information useful to developers for debugging the application. Syslog (RFC 5424)

Slide 65

Slide 65 text

0. Emergency System is unusable 1. Alert Should be corrected immediately 2. Critical Critical conditions 3. Error Error conditions 4. Warning May indicate that an error will occur if action is not taken. 5. Notice Events that are unusual, but not error conditions. 6. Informational Normal operational messages that require no action. 7. Debug Information useful to developers for debugging the application. PSR3

Slide 66

Slide 66 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 67

Slide 67 text

Everything is on fire

Slide 68

Slide 68 text

The ELK Stack

Slide 69

Slide 69 text

Elasticsearch Logstash Kibana

Slide 70

Slide 70 text

Logstash Elasticsearch Kibana

Slide 71

Slide 71 text

Logstash

Slide 72

Slide 72 text

Beats CouchDB_Changes Drupal_DBLog Elasticsearch Exec Event log File Ganglia Gelf Generator Graphite Github Heartbeat Heroku HTTP HTTP_Poller IRC IMAP JDBC JMX
 Kafka Log4J Lumberjack Meetup Pipe Puppet_Facter Relp RSS Backspace RabbitMQ Redis Salesforce SNMPTrap Stdin sqlite S3 SQS Stomp Syslog TCP Twitter Unix UDP Varnishlog WMI Web socket XMPP Zenoss ZeroMQ Inputs

Slide 73

Slide 73 text

Filters

Slide 74

Slide 74 text

filter { json { source => "message" add_field => [ “my_field", "tek_%{host}" ] } }

Slide 75

Slide 75 text

filter { kv { default_keys => [ "from", "[email protected]", "to", "[email protected]" ] } }

Slide 76

Slide 76 text

Accepted publickey for root from 172.14.183.11 port 22 ssh2

Slide 77

Slide 77 text

Accepted publickey for root from 172.14.183.11 port 22 ssh2

Slide 78

Slide 78 text

filter { grok { match => { "message" => "Accepted %{WORD:auth_method} for %{USER:username} from %{IP:src_ip} port %{INT:src_port} ssh2" } } } Accepted publickey for root from 172.14.183.11 port 22 ssh2

Slide 79

Slide 79 text

filter { grok { match => { "message" => "Accepted %{WORD:auth_method} for %{USER:username} from %{IP:src_ip} port %{INT:src_port} ssh2" } } } Accepted publickey for root from 172.14.183.11 port 22 ssh2

Slide 80

Slide 80 text

http://grokdebug.herokuapp.com/

Slide 81

Slide 81 text

Boundary Circus CSV Cloud watch Datadog Datadog_Metrics Email Elastic search Exec File Google BigQuery Google Cloud Storage Ganglia Gelf Graphtastic Graphite Hipchat HTTP IRC InfluxDB Juggernaut Jira Kafka Lumberjack Librato Loggly MongoDB MetricCatcher Nagios Null OpenTSDB Pagerduty Pipe Riemann Redmine Rackspace RabbitMQ Redis Riak S3 SQS Stomp StatsD Solr SNS Syslog Stdout TCP UDP WebHDFS Websocket XMPP Outputs Zabbix ZeroMQ

Slide 82

Slide 82 text

Input -> Filter -> Output

Slide 83

Slide 83 text

Logstash is slow(ish)

Slide 84

Slide 84 text

Elasticsearch

Slide 85

Slide 85 text

Kibana

Slide 86

Slide 86 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 92

Slide 92 text

Asimov’s Law

Slide 93

Slide 93 text

“A robot may not injure a human being or, through inaction, allow a human being to come to harm.”

Slide 94

Slide 94 text

@mheap’s Law

Slide 95

Slide 95 text

“An application log may not injure a an application’s performance or readability”

Slide 96

Slide 96 text

Plan for bursts of data

Slide 97

Slide 97 text

Disk space

Slide 98

Slide 98 text

Index management

Slide 99

Slide 99 text

Ship what’s relevant

Slide 100

Slide 100 text

Devs create dashboards

Slide 101

Slide 101 text

Unique request IDs

Slide 102

Slide 102 text

Normalise timezones

Slide 103

Slide 103 text

No really. Normalise timezones

Slide 104

Slide 104 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 105

Slide 105 text

Beats

Slide 106

Slide 106 text

Graphite

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

Pagerduty

Slide 110

Slide 110 text

Elastalert

Slide 111

Slide 111 text

1. Logging 2. Getting started 3. The ELK stack 4. Logs and dashboards 5. Log management 6. Supporting services 7. Conclusion

Slide 112

Slide 112 text

Logging is required

Slide 113

Slide 113 text

Developers are empowered

Slide 114

Slide 114 text

Use PSR-3

Slide 115

Slide 115 text

Logging isn’t free

Slide 116

Slide 116 text

“Would you rather fly slowly or fly blind?”

Slide 117

Slide 117 text

Thanks! I’ve been @mheap, you’ve been awesome. Please leave feedback on Joind.in https://joind.in/17042