mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
properly serialize external logs that contain decimal.Decimal objects
This commit is contained in:
@@ -3,13 +3,13 @@
|
|||||||
|
|
||||||
from copy import copy
|
from copy import copy
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
import socket
|
import socket
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from dateutil.tz import tzutc
|
||||||
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
@@ -91,18 +91,13 @@ class LogstashFormatterBase(logging.Formatter):
|
|||||||
'processName': record.processName,
|
'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
|
@classmethod
|
||||||
def format_exception(cls, exc_info):
|
def format_exception(cls, exc_info):
|
||||||
return ''.join(traceback.format_exception(*exc_info)) if exc_info else ''
|
return ''.join(traceback.format_exception(*exc_info)) if exc_info else ''
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def serialize(cls, message):
|
def serialize(cls, message):
|
||||||
return bytes(json.dumps(message), 'utf-8')
|
return bytes(json.dumps(message, cls=DjangoJSONEncoder), 'utf-8')
|
||||||
|
|
||||||
|
|
||||||
class LogstashFormatter(LogstashFormatterBase):
|
class LogstashFormatter(LogstashFormatterBase):
|
||||||
@@ -157,9 +152,6 @@ class LogstashFormatter(LogstashFormatterBase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
data_for_log[key] = getattr(job_event, fd)
|
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:
|
except Exception as e:
|
||||||
data_for_log[key] = 'Exception `{}` producing field'.format(e)
|
data_for_log[key] = 'Exception `{}` producing field'.format(e)
|
||||||
|
|
||||||
@@ -231,10 +223,12 @@ class LogstashFormatter(LogstashFormatterBase):
|
|||||||
return fields
|
return fields
|
||||||
|
|
||||||
def format(self, record):
|
def format(self, record):
|
||||||
|
stamp = datetime.utcfromtimestamp(record.created)
|
||||||
|
stamp = stamp.replace(tzinfo=tzutc())
|
||||||
message = {
|
message = {
|
||||||
# Field not included, but exist in related logs
|
# Field not included, but exist in related logs
|
||||||
# 'path': record.pathname
|
# 'path': record.pathname
|
||||||
'@timestamp': self.format_timestamp(record.created),
|
'@timestamp': stamp,
|
||||||
'message': record.getMessage(),
|
'message': record.getMessage(),
|
||||||
'host': self.host,
|
'host': self.host,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user