Skip to content

When an ordinary character string is received. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions fluent/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import msgpack
import socket
import threading
import traceback

try:
import json
Expand Down Expand Up @@ -31,17 +32,37 @@ def format(self, record):
# if 'sys_exc_info' in data and data['sys_exc_info']:
# data['sys_exc_info'] = self.formatException(data['sys_exc_info'])

self._structuring(data, record.msg)
self._structuring(data, record)
return data

def _structuring(self, data, msg):
if isinstance(msg, dict):
self._add_dic(data, msg)
elif isinstance(msg, str):
def _structuring(self, data, record):
log_data = self._get_log_data(record)

traceback = self._get_traceback(record)
if traceback:
log_data['traceback'] = traceback

self._add_dic(data, log_data)

def _get_log_data(self, record):
if isinstance(record.msg, dict):
data = record.msg
else:
message = record.getMessage()
try:
self._add_dic(data, json.loads(str(msg)))
except:
pass
parsed_value = json.loads(message)
except ValueError, e:
data = {'message': message}
else:
if not isinstance(parsed_value, dict):
data = {'message': parsed_value}
return data

def _get_traceback(self, record):
if not record.exc_info:
return None
tb = traceback.format_exception(*record.exc_info)
return "".join(tb)

def _add_dic(self, data, dic):
for k, v in dic.items():
Expand Down