Created
September 8, 2011 17:49
-
-
Save jpablo/1204080 to your computer and use it in GitHub Desktop.
Logger Proxy class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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