Logging handler to send logs to your OpenSearch cluster with bulk SSL. Forked from https://github.com/logzio/logzio-python-handler
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.

103 lines
3.2 KiB

7 years ago
7 years ago
7 years ago
  1. import sys
  2. import json
  3. import logging
  4. import datetime
  5. import traceback
  6. import logging.handlers
  7. from .sender import LogzioSender
  8. from .exceptions import LogzioException
  9. class LogzioHandler(logging.Handler):
  10. def __init__(self,
  11. token,
  12. logzio_type="python",
  13. logs_drain_timeout=3,
  14. url="https://listener.logz.io:8071",
  15. debug=False):
  16. if not token:
  17. raise LogzioException('Logz.io Token must be provided')
  18. self.logzio_type = logzio_type
  19. self.logzio_sender = LogzioSender(
  20. token=token,
  21. url=url,
  22. logs_drain_timeout=logs_drain_timeout,
  23. debug=debug)
  24. logging.Handler.__init__(self)
  25. def extra_fields(self, message):
  26. not_allowed_keys = (
  27. 'args', 'asctime', 'created', 'exc_info', 'stack_info', 'exc_text',
  28. 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'module',
  29. 'msecs', 'msecs', 'message', 'msg', 'name', 'pathname', 'process',
  30. 'processName', 'relativeCreated', 'thread', 'threadName')
  31. if sys.version_info < (3, 0):
  32. # long and basestring don't exist in py3 so, NOQA
  33. var_type = (basestring, bool, dict, float, # NOQA
  34. int, long, list, type(None)) # NOQA
  35. else:
  36. var_type = (str, bool, dict, float, int, list, type(None))
  37. extra_fields = {}
  38. for key, value in message.__dict__.items():
  39. if key not in not_allowed_keys:
  40. if isinstance(value, var_type):
  41. extra_fields[key] = value
  42. else:
  43. extra_fields[key] = repr(value)
  44. return extra_fields
  45. def flush(self):
  46. self.logzio_sender.flush()
  47. def format(self, record):
  48. message = super(LogzioHandler, self).format(record)
  49. try:
  50. return json.loads(message)
  51. except (TypeError, ValueError):
  52. return message
  53. def format_exception(self, exc_info):
  54. return '\n'.join(traceback.format_exception(*exc_info))
  55. def format_message(self, message):
  56. now = datetime.datetime.utcnow()
  57. timestamp = now.strftime('%Y-%m-%dT%H:%M:%S') + \
  58. '.%03d' % (now.microsecond / 1000) + 'Z'
  59. return_json = {
  60. 'logger': message.name,
  61. 'line_number': message.lineno,
  62. 'path_name': message.pathname,
  63. 'log_level': message.levelname,
  64. 'type': self.logzio_type,
  65. 'message': message.getMessage(),
  66. '@timestamp': timestamp
  67. }
  68. if message.exc_info:
  69. return_json['exception'] = self.format_exception(message.exc_info)
  70. # We want to ignore logging format on exceptions, as we handle those differently
  71. message.exc_info = None
  72. formatted_message = self.format(message)
  73. if isinstance(formatted_message, dict):
  74. return_json.update(formatted_message)
  75. else:
  76. return_json['message'] = formatted_message
  77. return_json.update(self.extra_fields(message))
  78. return return_json
  79. def emit(self, record):
  80. self.logzio_sender.append(self.format_message(record))