Python web service call logger

I wanted to log every access to my web services (using soaplib): Get the client IP, what method is it calling, what parameters, and what class. Rather than write the code to log the access in every method I've written a decorator (logWebServiceCall) so you only need to add it on the top of your web method, like this:

  1.  
  2. class MyService(SimpleWSGISoapApp )
  3. @logWebServiceCall
  4. @soapmethod(String, String, _returns=Array(String))
  5. def myMethod(self, param1, param2):
  6. ...code here...
  7.  

Decorator code:

  1.  
  2. from soaplib.wsgi_soap import request
  3.  
  4. def logWebServiceCall(func):
  5. def wrapped(*args, **kwargs):
  6. if hasattr(request, "environ"):
  7. if not kwargs.has_key('_soap_descriptor'):
  8. message = request.environ['SCRIPT_NAME']
  9. message += " - " + request.environ['HTTP_SOAPACTION']
  10. message += " - " + request.environ['REMOTE_ADDR']
  11. message += " - " + str(list(args)[1:])
  12. print message
  13. result = func(*args, **kwargs)
  14. return result
  15. wrapped.__doc__ = func.__doc__
  16. wrapped.func_name = func.func_name
  17. wrapped._is_soap_method = True
  18. return wrapped
  19.  

Update:
Just finished a decorator for validation. Same usage than the previous one.

  1.  
  2. def validateUserFirst(func):
  3. def wrapped(*args, **kwargs):
  4. if hasattr(request, "environ"):
  5. if not kwargs.has_key('_soap_descriptor'):
  6. argumentsList = list(args)[1:]
  7. if len(argumentsList) < 2:
  8. raise Exception('This method requires username and password at least')
  9.  
  10. username = argumentsList[0]
  11. password = argumentsList[1]
  12.  
  13. if not validate(username, password): # define the validation method as you want
  14. raise Exception('Invalid username or password')
  15.  
  16. result = func(*args, **kwargs)
  17. return result
  18. wrapped.__doc__ = func.__doc__
  19. wrapped.func_name = func.func_name
  20. wrapped._is_soap_method = True
  21. return wrapped
  22.  
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
  • http://jadarios.wordpress.com esauro

    Using decorators for such purposes is a great idea.

    I think you should take a look into logging module [1]. It provides differents log levels (debug, warn, error, etc) as well as a single way to log into a lot of medium (stream, file, socket, smtp, etc). You can log to all those places just modifiying a parameter.

    [1] http://docs.python.org/lib/module-logging.html

  • http://carlosble.com Carlos Ble

    Thanks Esau 🙂
    This technic of using decorators is part of the Aspect Oriented Programming paradigm.
    Thanks for the link, I’ll definitively use that module to log.

  • http://www.thebigbangtheoryepisodes.info/season-1/season-1-episode-17-the-tangerine-factor/ big bang theory season 1

    A colleague of mine asked me to check out your website three weeks ago, since us two enjoy the same stuff (yeah we’re close buddies, clone heads, been 16 years) and I seriously got to say that I am greatly impressed. Me and my friend were actually considering to start a similar site just like yours, but we have had some struggles. First, neither of us is a technology individual, 2ndly, we do not really know where to start. I know alot of people managed to get their Dot Coms on the net just recently (due to cheap prices, etc) but both of us are still having struggles. I would really appreciate it if you recommend us a place to start from, because we don’t have a clue LOL. Yes it sucks being 48 year old internet literates… or should I say dotcom literates?

  • http://manishlamrodblog.wordpress.com/ Manish Lamrod

    Magnificent beat ! I would like to apprentice at the same time as you amend your site, how could i subscribe for a blog web site? The account helped me a appropriate deal. I had been a little bit familiar of this your broadcast offered bright clear concept

  • http://www.articlesfreehub.com/fb-optin-app-review-killer-discount/ FB Optin App Review

    Hi-Ya! our family just loves your terrific editorial thanx and please stick to it

  • http://teamatomicmilkshake.com NorClorse

    Nuariusbart http://teamatomicmilkshake.com – cheap tom s shoes

  • http://teamatomicmilkshake.com NorClorse

    Nuariusbart http://teamatomicmilkshake.com – toms sale

  • Lester