fork download
  1. import os, sys
  2. import logging, inspect
  3. from inspect import( isclass, ismethod, isfunction )
  4. from pprint import pprint
  5.  
  6. logger = logging.getLogger("LogHelper")
  7. logger.setLevel(logging.DEBUG)
  8.  
  9. handler = logging.StreamHandler()
  10. handler.setLevel(logging.DEBUG)
  11.  
  12. def log_decorator(instance=None, *arg, **kwarg):
  13.  
  14. qualname = lambda obj: obj.__qualname__
  15. eventlog = logging.getLogger('LogHelper')
  16. self = None
  17.  
  18. try:
  19. self = instance()
  20. except TypeError:
  21. self = instance
  22.  
  23. if isfunction(instance):
  24. try:
  25. channel = kwarg['channel']
  26. except KeyError as ex:
  27. if isfunction(instance):
  28. raise TypeError('''Missing required 'channel' argument.''')
  29. else:
  30. pass
  31.  
  32. def log(message, level = logging.INFO, *arg_list, **kwarg_list):
  33.  
  34. if isclass(instance):
  35. #caller_id = "%s::%s" %( qualname(instance.__class__ ), attr )
  36. caller_id = "WIP"
  37. else:
  38. caller_id = qualname(instance)
  39.  
  40. eventlog.log( level, message, extra={ 'caller': caller_id, 'log_channel': channel })
  41.  
  42. def debug(message, *arg_list, **kwarg_list):
  43. log( message, logging.DEBUG, *arg_list, **kwarg_list)
  44.  
  45. def info(message, *arg_list, **kwarg_list):
  46. log( message, logging.INFO, *arg_list, **kwarg_list)
  47.  
  48. def warn(message, *arg_list, **kwarg_list):
  49. log( message, logging.WARNING, *arg_list, **kwarg_list)
  50.  
  51. def error(message, *arg_list, **kwarg_list):
  52. log( message, logging.ERROR, *arg_list, **kwarg_list)
  53.  
  54. def critical(message, *arg_list, **kwarg_list):
  55. log( message, logging.CRITICAL, *arg_list, **kwarg_list)
  56.  
  57. def wrapper(self, *args, **kw_arg):
  58. return self(*args, **kw_arg)
  59.  
  60. globals = [
  61. ( 'log', log ),
  62. ( 'debug', debug ),
  63. ( 'info', info ),
  64. ( 'warn', warn ),
  65. ( 'error', error ),
  66. ( 'critical', critical )
  67. ]
  68.  
  69. if self is not None:
  70. print(self, isinstance(self, self.__class__))
  71.  
  72. if isinstance(self, self.__class__):
  73. ## I'll see about this.
  74. code, scope = None, {}
  75. else:
  76. print("HI.")
  77. code, scope = self.__code__, self.__globals__
  78.  
  79. for i, (name, handler) in enumerate(globals):
  80. scope[name] = handler
  81. else:
  82. # We didn't receive the function on this call, so the return value
  83. # of this call will receive it, and we're getting the options now.
  84. def partial_inner(arg0=instance):
  85. return log_decorator(arg0, *arg, **kwarg)
  86.  
  87. return partial_inner
  88.  
  89. return wrapper
  90.  
  91. @log_decorator(channel='FooChannel')
  92. def foo():
  93. pass #log("Hi, world.")
  94.  
  95. foo()
  96.  
  97. @log_decorator
  98. class FooClass(object):
  99. channel = 'FooClassObj'
  100.  
  101. def bar(self):
  102. pass #log('Method logged.')
  103.  
  104. #f = FooClass()
  105. #f.bar()
  106.  
Success #stdin #stdout 0.04s 10540KB
stdin
Standard input is empty
stdout
<__main__.FooClass object at 0x15241e813438> True