Introduction to logging in django

69
Introduction to Logging in Django Sivasubramaniam Arunachalam July 20, 2013 @sivaa_in http://www.meetup.com/BangPypers/events/125755952/

Transcript of Introduction to logging in django

Page 1: Introduction to logging in django

Introduction to Logging in Django

Sivasubramaniam Arunachalam

July 20, 2013

@sivaa_in

http://www.meetup.com/BangPypers/events/125755952/

Page 2: Introduction to logging in django

Agenda

• Introduction to Logging

• Python/Django Logging

• Few Best Practices

• Demo

Page 3: Introduction to logging in django
Page 4: Introduction to logging in django

Your Application misbehaved

Next, what the user will do?

Page 5: Introduction to logging in django

https://twitter.com/naitikvyas/statuses/221096865574293504

Page 6: Introduction to logging in django

https://twitter.com/vijaypadiyar/status/352439670732369921

Page 7: Introduction to logging in django

https://twitter.com/PaulAppleyard/status/42294624353656832

Page 8: Introduction to logging in django

The Users

• End Users

• System Administrators

• Developers

• Support Team

Page 9: Introduction to logging in django

The Questions

• What

• When

• How Much

• How to Control

• How to Correlate

to Log

Page 10: Introduction to logging in django

What to Log?It’s an Art

Page 11: Introduction to logging in django

Exceptions

• No Managed Exceptions

• Global Exception Handler

Page 12: Introduction to logging in django

Events

• Security Incidents

• Life Cycle

• Resource usage

Page 13: Introduction to logging in django

States

• Retry

• Time-Outs

Page 14: Introduction to logging in django

Exceptions/Events alone will not help much

Page 15: Introduction to logging in django

Debug Traces

• User Input

• System/API Calls

• Method Invocation

Page 16: Introduction to logging in django

And much more…

• HTTP Requests

• DB Calls

• Threads

Page 17: Introduction to logging in django

Logging in Django

Page 18: Introduction to logging in django

Logging in DjangoPython• Standard Module

• Multiple Destinations

• Custom Filtering Options

Page 19: Introduction to logging in django

import logging

• Loggers

• Handlers

• Filters

• Formatters

Page 20: Introduction to logging in django

Loggers

• Entry Point

• Unique Name

• Log Level

Page 21: Introduction to logging in django

Log Level

• DEBUG

• INFO

• WARNING

• ERROR

• CRITICAL

Page 22: Introduction to logging in django

Log Record• Log Level

• Logger Name

• Message– Error Code

– Stack Trace

• Others

– Path, Line #, args, func

Page 23: Introduction to logging in django

Handlers

• Output Mechanism – File

– Screen/Console

– HTTP/Mail

– Queue

• Log Level

Page 24: Introduction to logging in django

Default Handlers

• StreamHandler

• SocketHandler

• DatagramHandler

• SysLogHandler

• NTEventLogHandler

• MemoryHandler

• FileHandler– Default

– Rotating

– TimedRotating

– Watched

• NullHandler

• SMTPHandler

• HTTPHandler

Page 25: Introduction to logging in django

Handler 02Logger ‘X’

Handler N

Handler 01

Page 26: Introduction to logging in django

Handler E-MailPayment Logger

Handler File

Handler SMS

Page 27: Introduction to logging in django

Handler E-MailPayment Logger

Handler File

Handler SMS

ERROR

Page 28: Introduction to logging in django

Filters

• Conditional Filtering

• Changing Log Levels

• Chaining

Page 29: Introduction to logging in django

Logger 02Filter ‘X’

Handler 01

Logger 01

Page 30: Introduction to logging in django

Log Record will be Dropped, If

• Log Level is lower than– Logger’s Log Level

– Handler’s Log Level

• Filter Chain returns a False

Page 31: Introduction to logging in django

Formatters

• The output text format

• Python Formatting String

Page 32: Introduction to logging in django

http://docs.python.org/2/library/logging.html#logrecord-attributes

Page 33: Introduction to logging in django

logging.method()• logger.log()

• logger.critical()

• logger.error()

• logger.warning()

• logger.info()

• logger.debug()

• logger.exception()

Page 34: Introduction to logging in django

Django Extensions - Loggers

1. django

2. django.request

3. django.db.backends

Page 35: Introduction to logging in django

django.request

• 4xx -> WARNING

• 5xx -> ERROR

• Contexts– HTTP Status Code (Response)

– Entire Request Object

Page 36: Introduction to logging in django

django.db.backends

• SQLs -> DEBUG

• Only when DEBUG = True

• Contexts– SQL Statement

– Duration

– Parameters

Page 37: Introduction to logging in django

Django Extensions – Handlers

• AdminEmailHandler

Page 38: Introduction to logging in django

AdminEmailHandler

• E-Mails -> Site Admins

• Contexts– Request Object

– Stack Trace

• include_html = True/False– Full Trace/Local Vars/Settings

Page 39: Introduction to logging in django

Django Extensions – Filters

• CallBackFilter

• RequireDebugFalse

• RequireDebugTrue

Page 40: Introduction to logging in django

CallBackFilter

• Call Back Functions

• Called with Log Record

• From 1.4

Page 41: Introduction to logging in django

RequireDebugFalse

• DEBUG = False

• From 1.4

Page 42: Introduction to logging in django

RequireDebugTrue

• DEBUG = True

• From 1.5

Page 43: Introduction to logging in django

settings.LOGGING

Page 44: Introduction to logging in django

disable_existing_loggers

• False - Merge

• True - Ignore

• From 1.5

Page 45: Introduction to logging in django
Page 46: Introduction to logging in django
Page 47: Introduction to logging in django

SENTRY

Page 48: Introduction to logging in django

Best Practices

Page 49: Introduction to logging in django

self.logger = logging.getLogger(type(self).__name__)

Page 50: Introduction to logging in django

No Single Logger For All Classes

Page 51: Introduction to logging in django

Project Level Formatters

Page 52: Introduction to logging in django

Avoid “print” in development. Use “stdout” with a “StreamHandler”

Page 53: Introduction to logging in django

Don’t Catch -> Log -> Re-raiseLet the caller do that

Page 54: Introduction to logging in django

Something terribly wrong? Don’t use logging alone. Raise a Exception to the caller.

. Logging is NOT a way to deal with exceptions

Page 55: Introduction to logging in django

Different Log File for each Environment

LOG_ROOT = '/var/log/<project>/<env>/'

Page 56: Introduction to logging in django

Un-handled Exceptions should be stored separately

Page 57: Introduction to logging in django

Sensitive information’s in Logs

Page 58: Introduction to logging in django

Size of the Log Files

• Text Editor Problems

• Archive via Scripts

• Compress to Save Space

Page 59: Introduction to logging in django

Only Integers1300604499194,4,192168001002,20600,1001,2,500000

Page 60: Introduction to logging in django

Be aware of cluster nodesserver<id>_node<id>_<logname>.log

Page 61: Introduction to logging in django

is_debug()

Page 62: Introduction to logging in django

I/O Performance

Page 63: Introduction to logging in django

Batch Logging

Page 64: Introduction to logging in django

No Repeated Logs & Stack Traces

Page 65: Introduction to logging in django

Infrastructure Failures

Page 66: Introduction to logging in django

End Users -> No Stack Trace Please!

Page 67: Introduction to logging in django

Centralized Logging

Page 68: Introduction to logging in django

Thank [email protected]

bit.ly/sivasubramaniambit.ly/sivaa_in

Page 69: Introduction to logging in django

References

http://css.dzone.com/articles/best-practices-python-logginghttp://architects.dzone.com/articles/deferred-logging-file-handlerhttp://architects.dzone.com/articles/high-performance-and-smarterhttp://java.dzone.com/articles/dark-art-logginghttp://architects.dzone.com/articles/think-ahead-think-logginghttp://www.shutupandship.com/2012/02/how-python-logging-module-works.htmlhttp://java.dzone.com/news/application-logging-what-when