Browse Source

Fix formatter not being used.

Allow custom logzio fields to top level structure
opensearch
Marek Kudlacz 8 years ago
parent
commit
d51b4bc9a9
1 changed files with 17 additions and 7 deletions
  1. +17
    -7
      logzio/handler.py

+ 17
- 7
logzio/handler.py View File

@ -102,14 +102,16 @@ class LogzioHandler(logging.Handler):
self.logs_counter_condition.release() self.logs_counter_condition.release()
def handle_exceptions(self, message): def handle_exceptions(self, message):
if message.exc_info:
return '\n'.join(traceback.format_exception(*message.exc_info))
else:
return message.getMessage()
message = '\n'.join(traceback.format_exception(*message.exc_info))
def format_message(self, message):
def format(self, record):
message = super(LogzioHandler, self).format(record)
try:
return json.loads(message)
except TypeError:
return message
message_field = self.handle_exceptions(message)
def format_message(self, message):
now = datetime.datetime.utcnow() now = datetime.datetime.utcnow()
timestamp = now.strftime("%Y-%m-%dT%H:%M:%S") + ".%03d" % (now.microsecond / 1000) + "Z" timestamp = now.strftime("%Y-%m-%dT%H:%M:%S") + ".%03d" % (now.microsecond / 1000) + "Z"
@ -118,11 +120,19 @@ class LogzioHandler(logging.Handler):
"line_number": message.lineno, "line_number": message.lineno,
"path_name": message.pathname, "path_name": message.pathname,
"log_level": message.levelname, "log_level": message.levelname,
"message": message_field,
"type": self.logzio_type, "type": self.logzio_type,
"@timestamp": timestamp "@timestamp": timestamp
} }
if message.exc_info:
return_json["message"] = self.handle_exceptions(message)
else:
formatted_message = self.format(message)
if isinstance(formatted_message, dict):
return_json.update(formatted_message)
else:
return_json["message"] = formatted_message
return return_json return return_json
def backup_logs(self, logs): def backup_logs(self, logs):


Loading…
Cancel
Save