Skip to content

Instantly share code, notes, and snippets.

@jpablo
Created September 8, 2011 17:49
Show Gist options
  • Save jpablo/1204080 to your computer and use it in GitHub Desktop.
Save jpablo/1204080 to your computer and use it in GitHub Desktop.
Logger Proxy class
from django.db import models
## given a Log container class, for example:
class MethodLog(models.Model):
occurred = models.DateTimeField(auto_now_add=True)
method = models.CharField(max_length=100)
## here's a class which logs all method calls and passes it
## to the contained object
class Proxy(object):
"""
Logs the method and passes the method request
to the inner object
"""
def __init__(self,inner):
self.inner = inner
def __getattr__(self, name):
MethodLog.objects.create(method=name)
return getattr(self.inner,name)
# now, in place of your original object you use the proxy:
# p = Proxy(original)
# p.method() => log entry + original.method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment