Skip to content

Commit

Permalink
Merge pull request ansible#6569 from ryanpetrello/log-decimal
Browse files Browse the repository at this point in the history
properly serialize external logs that contain decimal.Decimal objects

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
  • Loading branch information
2 parents 362fdae + 2cefba6 commit 397c009
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions awx/main/utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from copy import copy
import json
import time
import logging
import traceback
import socket
from datetime import datetime


from dateutil.tz import tzutc
from django.core.serializers.json import DjangoJSONEncoder
from django.conf import settings


Expand Down Expand Up @@ -91,18 +91,13 @@ def get_debug_fields(self, record):
'processName': record.processName,
}

@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):
return bytes(json.dumps(message), 'utf-8')
return bytes(json.dumps(message, cls=DjangoJSONEncoder), 'utf-8')


class LogstashFormatter(LogstashFormatterBase):
Expand Down Expand Up @@ -157,9 +152,6 @@ def reformat_data_for_log(self, raw_data, kind=None):

try:
data_for_log[key] = getattr(job_event, fd)
if fd in ['created', 'modified'] and data_for_log[key] is not None:
time_float = time.mktime(data_for_log[key].timetuple())
data_for_log[key] = self.format_timestamp(time_float)
except Exception as e:
data_for_log[key] = 'Exception `{}` producing field'.format(e)

Expand Down Expand Up @@ -231,10 +223,12 @@ def get_extra_fields(self, record):
return fields

def format(self, record):
stamp = datetime.utcfromtimestamp(record.created)
stamp = stamp.replace(tzinfo=tzutc())
message = {
# Field not included, but exist in related logs
# 'path': record.pathname
'@timestamp': self.format_timestamp(record.created),
'@timestamp': stamp,
'message': record.getMessage(),
'host': self.host,

Expand Down

0 comments on commit 397c009

Please sign in to comment.