Git Product home page Git Product logo

django-timelog's Introduction

A simple django middleware that logs request times using the Django 1.3 logging support, and a management command to analyze the resulting data. Once installed and configured you can run a command line these:

python manage.py analyze_timelog
python manage.py analyze_timelog --noreverse

And generate useful tabular data like this:

+--------------------------+--------+--------+-------+---------+---------+-------+-----------------+
| view                     | method | status | count | minimum | maximum | mean  | stdev           |
+--------------------------+--------+--------+-------+---------+---------+-------+-----------------+
| boxes.viewsBoxDetailView | GET    | 200    | 9430  | 0.14    | 0.28    | 0.21  | 0.0700037118541 |
+--------------------------+--------+--------+-------+---------+---------+-------+-----------------+
| boxes.viewsBoxListView   | GET    | 200    | 66010 | 0.17    | 0.28    | 0.232 | 0.0455415351076 |
+--------------------------+--------+--------+-------+---------+---------+-------+-----------------+
| django.views.staticserve | GET    | 200    | 61295 | 0.00    | 0.02    | 0.007 | 0.0060574669888 |
+--------------------------+--------+--------+-------+---------+---------+-------+-----------------+

This project was heavily influenced by the Rails Request log analyzer.

Installation

pip install django-timelog

Once installed you need to do a little configuration to get things working. First add the middleware to your MIDDLEWARE_CLASSES in your settings file.

MIDDLEWARE_CLASSES = (
  'timelog.middleware.TimeLogMiddleware',

Next add timelog to your INSTALLED_APPS list. This is purely for the management command discovery.

INSTALLED_APPS = (
  'timelog',

Then configure the logger you want to use. This really depends on what you want to do, the django 1.3 logging setup is pretty powerful. Here’s how I’ve got logging setup as an example:

TIMELOG_LOG = '/path/to/logs/timelog.log'

LOGGING = {
  'version': 1,
  'formatters': {
    'plain': {
      'format': '%(asctime)s %(message)s'},
    },
  'handlers': {
    'timelog': {
      'level': 'DEBUG',
      'class': 'logging.handlers.RotatingFileHandler',
      'filename': TIMELOG_LOG,
      'maxBytes': 1024 * 1024 * 5,  # 5 MB
      'backupCount': 5,
      'formatter': 'plain',
    },
  },
  'loggers': {
    'timelog.middleware': {
      'handlers': ['timelog'],
      'level': 'DEBUG',
      'propogate': False,
     }
  }
}

Lastly, if you have particular URIs you wish to ignore you can define them using basic regular expressions in the TIMELOG_IGNORE_URIS list in settings.py:

TIMELOG_IGNORE_URIS = (
    '^/admin/',         # Ignores all URIs beginning with '/admin/'
    '^/other_page/$',   # Ignores the URI '/other_page/' only, but not '/other_page/more/'.
    '.jpg$',            # Ignores all URIs ending in .jpg
)

django-timelog's People

Contributors

garethr avatar jernejcic avatar kmike avatar rassie avatar rory-geoghegan-ecometrica avatar rossp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-timelog's Issues

use time.clock() instead of time.time()

time.time() will return the server's best guess at the current wall clock time. Wall clock time can be affected by NTP updates, result in artificially high or low (even negative) runtimes for requests.

time.clock() uses the processor's internal counters to measure time since last reboot. It will never "jump" like time.time() will, and so is the preferred method for profiling runtimes.

Incompatibility with django-piston

When running "analyze_timelog" on a project that has piston Resources:

File ".../virtualenv/lib/python2.7/site-packages/timelog/lib.py", line 38, in view_name_from
    view =  "%s.%s" % (resolve(path).func.__module__, resolve(path).func.__name__)
AttributeError: 'Resource' object has no attribute '__name__'

adding --noreverse solves the problem.
The problem with piston is that

type(resolve(path).func) == Resource #and not function

Mistake in description

"you can run a command line these:" in description page. I guess it sould be "like this".

ValueError: 'format' in __slots__ conflicts with class variable

Unable to install with python 3 environment, getting this error while installing
`Collecting django-timelog
Cache entry deserialization failed, entry ignored
Using cached https://files.pythonhosted.org/packages/f8/81/08b354c2fb0eb132a855ad53a43a9ab740a40d63825bb98c3d8a094b8186/django-timelog-0.4.tar.gz
Collecting texttable (from django-timelog)
Cache entry deserialization failed, entry ignored
Using cached https://files.pythonhosted.org/packages/ba/16/ac997890d27d6a78e4bcb379ecd9ce105a0d923f4fefff2eb0c489697da0/texttable-1.2.1.tar.gz
Collecting progressbar (from django-timelog)
Cache entry deserialization failed, entry ignored
Using cached https://files.pythonhosted.org/packages/0f/e1/f30b72ecabba259c7c38dd0eb944a173effced3fd7de2c9c2902bd6f649a/progressbar-2.3.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/private/var/folders/_9/2532sz1d6p1_916m2gccg4w00000gn/T/pip-install-q_lcmelj/progressbar/setup.py", line 5, in
import progressbar
File "/private/var/folders/_9/2532sz1d6p1_916m2gccg4w00000gn/T/pip-install-q_lcmelj/progressbar/progressbar/init.py", line 59, in
from progressbar.widgets import *
File "/private/var/folders/_9/2532sz1d6p1_916m2gccg4w00000gn/T/pip-install-q_lcmelj/progressbar/progressbar/widgets.py", line 121, in
class FileTransferSpeed(Widget):
File "/Users/sasuke/.virtualenvs/search-api/bin/../lib/python3.6/abc.py", line 133, in new
cls = super().new(mcls, name, bases, namespace, **kwargs)
ValueError: 'format' in slots conflicts with class variable

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/_9/2532sz1d6p1_916m2gccg4w00000gn/T/pip-install-q_lcmelj/progressbar/`

error

File "manage.py", line 14, in
execute_manager(settings)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 438, in execute_manager
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/init.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(_args, *_options.dict)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 220, in execute
output = self.handle(_args, *_options)
File "/usr/local/lib/python2.7/dist-packages/timelog/management/commands/analyze_timelog.py", line 34, in handle
data = analyze_log_file(LOGFILE, PATTERN, reverse_paths=options.get('reverse'))
File "/usr/local/lib/python2.7/dist-packages/timelog/lib.py", line 75, in analyze_log_file
parsed = compiled_pattern.findall(line)[0]
IndexError: list index out of range

analyze_logfile command cannot read from stdin

We use rsyslog and rotate our timelogs, so we concatenate multiple ones together to analyze. It would be great if the analyze_logfile command took a filename as a parameter, and read from stdin if it is not present.

Also, we do not use TIMELOG_LOG, so could it be possible to not require having that in the settings.py file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.