Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 6792f58

Browse files
committed
Fixes and improvements for json logging
There are still some places where data/adresses were not hex'ed. My approach here is to use 'lazy' encoding for all trace-/debug-only log messages.
1 parent 2d203a8 commit 6792f58

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

ethereum/blocks.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@
3434
Log = processblock.Log
3535

3636

37+
class lazy_encode(object):
38+
def __init__(self, data):
39+
self.data = data
40+
41+
def __repr__(self):
42+
return repr([[k, encode_hex(a), v if k != 'code' else encode_hex(v)] for k, a, v in self.data])
43+
44+
def __str__(self):
45+
return str(repr(self))
46+
3747

3848
# Difficulty adjustment algo
3949
def calc_difficulty(parent, timestamp):
@@ -1019,7 +1029,7 @@ def commit_state(self):
10191029
t.delete(enckey)
10201030
acct.storage = t.root_hash
10211031
self.state.update(addr, rlp.encode(acct))
1022-
log_state.trace('delta', changes=changes)
1032+
log_state.trace('delta', changes=lazy_encode(changes))
10231033
self.reset_cache()
10241034
self.db.put_temporarily(b'validated:' + self.hash, '1')
10251035

ethereum/processblock.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ def __init__(self, data):
125125

126126
def __str__(self):
127127
if not isinstance(self.data, (str, unicode)):
128-
return self.data
128+
return repr(self.data)
129129
else:
130130
return encode_hex(self.data)
131131

132+
def __repr__(self):
133+
return str(self)
134+
132135

133136
def apply_transaction(block, tx):
134137
validate_transaction(block, tx)
@@ -276,7 +279,7 @@ def _apply_msg(ext, msg, code):
276279
# assert utils.is_numeric(gas)
277280
if trace_msg:
278281
log_msg.debug('MSG APPLIED', gas_remained=gas,
279-
sender=msg.sender, to=msg.to, data=dat)
282+
sender=encode_hex(msg.sender), to=encode_hex(msg.to), data=dat)
280283
if log_state.is_active('trace'):
281284
log_state.trace('MSG POST STATE SENDER', account=msg.sender.encode('hex'),
282285
bal=ext.get_balance(msg.sender),

ethereum/slogging.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,21 @@ def is_active(self, level_name='trace'):
130130

131131
def format_message(self, msg, kwargs, highlight, level):
132132
if getattr(self, 'log_json', False):
133-
message = {
134-
k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v)
135-
for k, v in kwargs.items()
136-
}
137-
133+
message = dict()
138134
message['event'] = '{}.{}'.format(self.name, msg.lower().replace(' ', '_'))
139135
message['level'] = logging.getLevelName(level)
140-
msg = json.dumps(message)
136+
try:
137+
message.update({
138+
k: v if isnumeric(v) or isinstance(v, (float, complex, list, str, dict)) else repr(v)
139+
for k, v in kwargs.items()
140+
})
141+
msg = json.dumps(message)
142+
except UnicodeDecodeError:
143+
message.update({
144+
k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v)
145+
for k, v in kwargs.items()
146+
})
147+
msg = json.dumps(message)
141148
else:
142149
msg = "{}{} {}{}".format(
143150
bcolors.WARNING if highlight else "",

0 commit comments

Comments
 (0)