From 017d367749f69e13a334e378eff3446eeea850ae Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Mon, 19 Nov 2018 07:56:27 -0500 Subject: [PATCH] Remove dependency and insert class --- awx/main/utils/formatters.py | 82 ++++++++++++++++++++++++++++++++++- requirements/requirements.in | 1 - requirements/requirements.txt | 2 +- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/awx/main/utils/formatters.py b/awx/main/utils/formatters.py index c7beee6b0c..7b8f06d535 100644 --- a/awx/main/utils/formatters.py +++ b/awx/main/utils/formatters.py @@ -1,11 +1,14 @@ # Copyright (c) 2017 Ansible Tower by Red Hat # All Rights Reserved. -from logstash.formatter import LogstashFormatterVersion1 from copy import copy import json import time import logging +import traceback +import socket +import sys +from datetime import datetime from django.conf import settings @@ -20,7 +23,82 @@ class TimeFormatter(logging.Formatter): return logging.Formatter.format(self, record) -class LogstashFormatter(LogstashFormatterVersion1): +class LogstashFormatterBase(logging.Formatter): + + def __init__(self, message_type='Logstash', tags=None, fqdn=False): + self.message_type = message_type + self.tags = tags if tags is not None else [] + + if fqdn: + self.host = socket.getfqdn() + else: + self.host = socket.gethostname() + + def get_extra_fields(self, record): + # The list contains all the attributes listed in + # http://docs.python.org/library/logging.html#logrecord-attributes + skip_list = ( + 'args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', + 'funcName', 'id', 'levelname', 'levelno', 'lineno', 'module', + 'msecs', 'msecs', 'message', 'msg', 'name', 'pathname', 'process', + 'processName', 'relativeCreated', 'thread', 'threadName', 'extra') + + if sys.version_info < (3, 0): + easy_types = (basestring, bool, dict, float, int, long, list, type(None)) + else: + easy_types = (str, bool, dict, float, int, list, type(None)) + + fields = {} + + for key, value in record.__dict__.items(): + if key not in skip_list: + if isinstance(value, easy_types): + fields[key] = value + else: + fields[key] = repr(value) + + return fields + + def get_debug_fields(self, record): + fields = { + 'stack_trace': self.format_exception(record.exc_info), + 'lineno': record.lineno, + 'process': record.process, + 'thread_name': record.threadName, + } + + # funcName was added in 2.5 + if not getattr(record, 'funcName', None): + fields['funcName'] = record.funcName + + # processName was added in 2.6 + if not getattr(record, 'processName', None): + fields['processName'] = record.processName + + return fields + + @classmethod + def format_source(cls, message_type, host, path): + return "%s://%s/%s" % (message_type, host, path) + + @classmethod + def format_timestamp(cls, time): + tstamp = datetime.utcfromtimestamp(time) + return tstamp.strftime("%Y-%m-%dT%H:%M:%S") + ".%03d" % (tstamp.microsecond / 1000) + "Z" + + @classmethod + def format_exception(cls, exc_info): + return ''.join(traceback.format_exception(*exc_info)) if exc_info else '' + + @classmethod + def serialize(cls, message): + if sys.version_info < (3, 0): + return json.dumps(message) + else: + return bytes(json.dumps(message), 'utf-8') + + +class LogstashFormatter(LogstashFormatterBase): def reformat_data_for_log(self, raw_data, kind=None): ''' diff --git a/requirements/requirements.in b/requirements/requirements.in index 04b68a511c..eb1731ea9b 100644 --- a/requirements/requirements.in +++ b/requirements/requirements.in @@ -32,7 +32,6 @@ psycopg2==2.7.3.2 # problems with Segmentation faults / wheels on upgrade pygerduty==0.37.0 pyparsing==2.2.0 python-dateutil==2.7.2 # contains support for TZINFO= parsing -python-logstash==0.4.6 python-memcached==1.59 python-radius==1.0 python3-saml==1.4.0 diff --git a/requirements/requirements.txt b/requirements/requirements.txt index eedd26c369..12472b93af 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -80,7 +80,7 @@ pyrad==2.1 # via django-radius pysocks==1.6.8 # via twilio python-dateutil==2.7.2 python-ldap==3.1.0 # via django-auth-ldap -python-logstash==0.4.6 +django-cors-headers==2.4.0 python-memcached==1.59 python-radius==1.0 python3-openid==3.1.0 # via social-auth-core