log.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """
  2. Cement core log module.
  3. """
  4. from ..core import exc, interface, handler
  5. def log_validator(klass, obj):
  6. """Validates an handler implementation against the ILog interface."""
  7. members = [
  8. '_setup',
  9. 'set_level',
  10. 'get_level',
  11. 'info',
  12. 'warn',
  13. 'error',
  14. 'fatal',
  15. 'debug',
  16. ]
  17. interface.validate(ILog, obj, members)
  18. class ILog(interface.Interface):
  19. """
  20. This class defines the Log Handler Interface. Classes that
  21. implement this handler must provide the methods and attributes defined
  22. below.
  23. Implementations do *not* subclass from interfaces.
  24. Usage:
  25. .. code-block:: python
  26. from cement.core import log
  27. class MyLogHandler(object):
  28. class Meta:
  29. interface = log.ILog
  30. label = 'my_log_handler'
  31. ...
  32. """
  33. # pylint: disable=W0232, C0111, R0903
  34. class IMeta:
  35. """Interface meta-data."""
  36. label = 'log'
  37. """The string identifier of the interface."""
  38. validator = log_validator
  39. """The interface validator function."""
  40. # Must be provided by the implementation
  41. Meta = interface.Attribute('Handler Meta-data')
  42. def _setup(app_obj):
  43. """
  44. The _setup function is called during application initialization and
  45. must 'setup' the handler object making it ready for the framework
  46. or the application to make further calls to it.
  47. :param app_obj: The application object.
  48. """
  49. def set_level():
  50. """
  51. Set the log level. Must except atleast one of:
  52. ``['INFO', 'WARN', 'ERROR', 'DEBUG', or 'FATAL']``.
  53. """
  54. def get_level():
  55. """Return a string representation of the log level."""
  56. def info(msg):
  57. """
  58. Log to the 'INFO' facility.
  59. :param msg: The message to log.
  60. """
  61. def warn(self, msg):
  62. """
  63. Log to the 'WARN' facility.
  64. :param msg: The message to log.
  65. """
  66. def error(self, msg):
  67. """
  68. Log to the 'ERROR' facility.
  69. :param msg: The message to log.
  70. """
  71. def fatal(self, msg):
  72. """
  73. Log to the 'FATAL' facility.
  74. :param msg: The message to log.
  75. """
  76. def debug(self, msg):
  77. """
  78. Log to the 'DEBUG' facility.
  79. :param msg: The message to log.
  80. """
  81. class CementLogHandler(handler.CementBaseHandler):
  82. """
  83. Base class that all Log Handlers should sub-class from.
  84. """
  85. class Meta:
  86. """
  87. Handler meta-data (can be passed as keyword arguments to the parent
  88. class).
  89. """
  90. label = None
  91. """The string identifier of this handler."""
  92. interface = ILog
  93. """The interface that this class implements."""
  94. def __init__(self, *args, **kw):
  95. super(CementLogHandler, self).__init__(*args, **kw)