Skip to content

Commit

Permalink
handle if the message is not a str
Browse files Browse the repository at this point in the history
  • Loading branch information
csm10495 committed Jul 31, 2020
1 parent 010e00e commit 3053693
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion csmlog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from csmlog.udp_handler import UdpHandler
from csmlog.udp_handler_receiver import UdpHandlerReceiver

__version__ = '0.21.0'
__version__ = '0.21.1'


class CSMLogger(object):
Expand Down
9 changes: 5 additions & 4 deletions csmlog/google_sheets_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,14 @@ def emit(self, record):
Called when a log record is to be sent. This just queues the events to be written to Sheets.
Will also break up msgs that are longer than the max cell size allowed by Google.
'''
rows = [(record.asctime, record.levelname, record.pathname, record.funcName, record.lineno, record.msg),]
full_record_msg_str = str(record.msg)
rows = [(record.asctime, record.levelname, record.pathname, record.funcName, record.lineno, full_record_msg_str),]

if len(record.msg) > GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH:
if len(full_record_msg_str) > GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH:
rows = []
# split row into multiple
for i in range(0, len(record.msg), GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH):
rows.append((record.asctime, record.levelname, record.pathname, record.funcName, record.lineno, record.msg[i:i+GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH]),)
for i in range(0, len(full_record_msg_str), GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH):
rows.append((record.asctime, record.levelname, record.pathname, record.funcName, record.lineno, full_record_msg_str[i:i+GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH]),)

with self._pending_rows_mutex:
for row in rows:
Expand Down
4 changes: 4 additions & 0 deletions csmlog/tests/test_google_sheets_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ def get_record(msg):
gsheets_handler.emit(get_record('hello'))
assert gsheets_handler._pending_rows[-1][-1] == 'hello'

# send to pending rows (non-str)
gsheets_handler.emit(get_record(123))
assert gsheets_handler._pending_rows[-1][-1] == '123'

# send to pending rows (too long, so should split)
gsheets_handler.emit(get_record(('A' * google_sheets_handler.GOOGLE_SHEETS_MAX_CELL_CHAR_LENGTH) + 'B'))
B = gsheets_handler._pending_rows[-1]
Expand Down

0 comments on commit 3053693

Please sign in to comment.