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.
 
 
 

86 lines
1.9 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. def setLevel(cls, level):
  18. cls.level = level
  19. setLevel = classmethod(setLevel)
  20. _LoggerClass = ILogger
  21. class BasicLogger(ILogger):
  22. def __init__(self, msg, out=sys.stdout):
  23. self.msg, self.out = msg, out
  24. def warning(self, msg, *args):
  25. if self.level < 1: return
  26. print >>self, self.WARN, self.msg,
  27. print >>self, msg %args
  28. WARN = 'WARN'
  29. def debug(self, msg, *args):
  30. if self.level < 2: return
  31. print >>self, self.DEBUG, self.msg,
  32. print >>self, msg %args
  33. DEBUG = 'DEBUG'
  34. def error(self, msg, *args):
  35. print >>self, self.ERROR, self.msg,
  36. print >>self, msg %args
  37. ERROR = 'ERROR'
  38. def write(self, *args):
  39. '''Write convenience function; writes strings.
  40. '''
  41. for s in args: self.out.write(s)
  42. def setBasicLogger():
  43. '''Use Basic Logger.
  44. '''
  45. setLoggerClass(BasicLogger)
  46. BasicLogger.setLevel(0)
  47. def setBasicLoggerWARN():
  48. '''Use Basic Logger.
  49. '''
  50. setLoggerClass(BasicLogger)
  51. BasicLogger.setLevel(1)
  52. def setBasicLoggerDEBUG():
  53. '''Use Basic Logger.
  54. '''
  55. setLoggerClass(BasicLogger)
  56. BasicLogger.setLevel(2)
  57. def setLoggerClass(loggingClass):
  58. '''Set Logging Class.
  59. '''
  60. assert issubclass(loggingClass, ILogger), 'loggingClass must subclass ILogger'
  61. global _LoggerClass
  62. _LoggerClass = loggingClass
  63. def setLevel(level=0):
  64. '''Set Global Logging Level.
  65. '''
  66. ILogger.level = level
  67. def getLogger(msg):
  68. '''Return instance of Logging class.
  69. '''
  70. return _LoggerClass(msg)