You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
1.5 KiB

  1. #! /usr/bin/env python
  2. """Logging"""
  3. import sys
  4. class ILogger:
  5. '''Logger interface, by default this class
  6. will be used and logging calls are no-ops.
  7. '''
  8. level = 0
  9. def __init__(self, msg):
  10. return
  11. def warning(self, *args):
  12. return
  13. def debug(self, *args):
  14. return
  15. def error(self, *args):
  16. return
  17. _LoggerClass = ILogger
  18. class BasicLogger(ILogger):
  19. def __init__(self, msg, out=sys.stdout):
  20. self.msg, self.out = msg, out
  21. def warning(self, msg, *args):
  22. print >>self, self.WARN, self.msg,
  23. print >>self, msg %args
  24. WARN = 'WARN'
  25. def debug(self, msg, *args):
  26. print >>self, self.DEBUG, self.msg,
  27. print >>self, msg %args
  28. DEBUG = 'DEBUG'
  29. def error(self, msg, *args):
  30. print >>self, self.ERROR, self.msg,
  31. print >>self, msg %args
  32. ERROR = 'ERROR'
  33. def write(self, *args):
  34. '''Write convenience function; writes strings.
  35. '''
  36. for s in args: self.out.write(s)
  37. def setBasicLogger():
  38. '''Use Basic Logger.
  39. '''
  40. setLoggerClass(BasicLogger)
  41. def setLoggerClass(loggingClass):
  42. '''Set Logging Class.
  43. '''
  44. assert issubclass(loggingClass, ILogger), 'loggingClass must subclass ILogger'
  45. global _LoggerClass
  46. _LoggerClass = loggingClass
  47. def setLevel(level=0):
  48. '''Set Global Logging Level.
  49. '''
  50. ILogger.level = level
  51. def getLogger(msg):
  52. '''Return instance of Logging class.
  53. '''
  54. return _LoggerClass(msg)