Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

base_module.py

By: jsf80238 on Feb 2nd, 2012  |  syntax: Python  |  size: 1.58 KB  |  views: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Version 3.2.1
  2. import logging
  3. import logging.handlers
  4.  
  5. def memoize(f):
  6.     cache = {}
  7.     def memf(*x):
  8.         if x not in cache:
  9.             cache[x] = f(*x)
  10.         return cache[x]
  11.     return memf
  12.  
  13. @memoize
  14. def get_logger():
  15.     """
  16.    HTTP logging is always verbose (DEBUG).
  17.    Console logging is INFO by default.
  18.    """
  19.     # create logger
  20.     logger = logging.getLogger('informatica')
  21.     logger.setLevel(logging.DEBUG)
  22.     # create console handler
  23.     console_handler = logging.StreamHandler()
  24.     # create formatter
  25.     console_formatter = logging.Formatter('%(asctime)s [%(levelname)8s] %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
  26.     # add formatter to console_handler
  27.     console_handler.setFormatter(console_formatter)
  28.     console_handler.setLevel(logging.INFO)
  29.     # http handler
  30.     host = "localhost:9090"
  31.     url = ""
  32.     class MyHTTPHandler(logging.handlers.HTTPHandler):
  33.         wanted = ["lineno", "module", "pathname", "funcName", "process", "levelname", "levelno", "msg"]
  34.         def mapLogRecord(self, record):
  35.             return {name: getattr(record, name, "#missing") for name in self.wanted}
  36.     http_handler = MyHTTPHandler(host, url, method='GET')
  37.     # add console_handler to logger
  38.     logger.addHandler(console_handler)
  39.     http_formatter = logging.Formatter('%(name)s - %(levelname)8s - %(message)s')
  40.     http_handler.setFormatter(http_formatter)
  41.     http_handler.setLevel(logging.DEBUG)
  42.     logger.addHandler(http_handler)
  43.     return logger
  44.  
  45. def record_usage():
  46.     get_logger().debug("used")
  47.  
  48. if __name__ == "__main__":
  49.     record_usage()
clone this paste RAW Paste Data