Guest User

Untitled

a guest
Dec 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import logging
  2. import sys
  3. import traceback
  4.  
  5.  
  6. class Logger(object):
  7. FMT = '[%(asctime)s - %(name)s] %(message)s'
  8. DEFAULT_LEVEL = logging.INFO
  9. AUTHN_FMT = "{username} (from group:{group}) authenticated {source} at {location}: {success}"
  10. AUTHZ_FMT = "{principal} (using role {role}) performed {action} on {object} at {location}: {success}"
  11. ACTION_FMT = "{principal} performed {action} (details: {details}) at {location}"
  12. EXC_FMT = "exception {exc} (details: {details}) at {location}"
  13.  
  14. def __init__(self, name, level=DEFAULT_LEVEL, fmt=FMT):
  15. self.name = name
  16. self.logger = logging.getLogger(name)
  17. self.logger.setLevel(level)
  18. ch = logging.StreamHandler(sys.stdout)
  19. ch.setLevel(level)
  20. formatter = logging.Formatter(fmt)
  21. ch.setFormatter(formatter)
  22. self.logger.addHandler(ch)
  23.  
  24. def info(self, msg):
  25. self.logger.info(msg)
  26.  
  27. def debug(self, msg):
  28. self.logger.debug(msg)
  29.  
  30. def error(self, msg):
  31. self.logger.error(msg)
  32.  
  33. def critical(self, msg):
  34. self.logger.critical(msg)
  35.  
  36. def authenticate_user(self, location, username, success=False, source='', group=None):
  37. msg = self.AUTHN_FMT.format(**{
  38. 'username': username,
  39. 'location': location,
  40. 'success': success,
  41. 'source': source,
  42. 'group': group,})
  43. return self.info(msg)
  44.  
  45.  
  46. def authenticate_token(self, location, token_name, success=False, source='', group=None):
  47. msg = self.AUTHN_FMT.format(**{
  48. 'username':token_name,
  49. 'location':location,
  50. 'success':success,
  51. 'source':source,
  52. 'group':group,
  53. })
  54. return self.info(msg)
  55.  
  56. def authorize_user(self, location, principal, action, object_, success=False, role=None):
  57. msg = self.AUTHZ_FMT.format(**{
  58. 'principal':principal,
  59. 'action':action,
  60. 'success':success,
  61. 'object':object_,
  62. 'location':location,
  63. 'role':role,
  64. })
  65. return self.info(msg)
  66.  
  67. def action(self, location, principal, action, details=None):
  68. msg = self.ACTION_FMT.format(**{
  69. 'principal':principal,
  70. 'action':action,
  71. 'details':details,
  72. 'location':location,
  73. })
  74. return self.info(msg)
  75.  
  76. def exception(self, location, details):
  77. msg = self.EXC_FMT.format(**{
  78. 'location':location,
  79. 'details':details,
  80. 'exc':traceback.format_exc()})
  81. return self.error(msg)
Add Comment
Please, Sign In to add comment