Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Correct slogging code for many times using configure
  • Loading branch information
inzem77 committed Aug 25, 2015
commit d136e0ae02d28c54118b59e54951bf49d35f00a3
17 changes: 12 additions & 5 deletions ethereum/slogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,23 @@ def configure_loglevels(config_string, format=PRINT_FORMAT):
config_string = ':debug,p2p:info,vm.op:trace'
"""
assert ':' in config_string
conf_dict = {}
for name_levels in config_string.split(','):
name, level = name_levels.split(':')
conf_dict[name] = level

root_logger = getLogger()
root_logger.setLevel(logging._checkLevel(conf_dict[""].upper()))

for i in root_logger.manager.loggerDict:
if isinstance(root_logger.manager.loggerDict[i], EthLogger):
root_logger.manager.loggerDict[i].setLevel(root_logger.level) # set all logers level as root logger. Protection from many execute configure

for name in conf_dict:
level = conf_dict[name]
assert not isinstance(level, int)
logger = getLogger(name)
logger.setLevel(getattr(logging, level.upper()))
if not len(logger.handlers):
ch = EthStreamHandler()
formatter = logging.Formatter(format)
ch.setFormatter(formatter)
logger.addHandler(ch)

def configure(config_string='', log_json=False):
if log_json:
Expand Down
23 changes: 19 additions & 4 deletions ethereum/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ def test_baseconfig2():
assert th.does_log(eth_chain_tx.info)
assert not th.does_log(eth_chain_tx.debug)

#---------------- configure again ------------------
th = setup_logging(':error')
assert th.does_log(root.error)
assert not th.does_log(root.info)
p2p_discovery = slogging.get_logger('p2p.discovery')
assert th.does_log(p2p_discovery.error)
assert not th.does_log(p2p_discovery.debug)
p2p_peer = slogging.get_logger('p2p.peer')
assert th.does_log(p2p_peer.error)
assert not th.does_log(p2p_peer.info)
p2p = slogging.get_logger('p2p')
assert th.does_log(p2p.error)
assert not th.does_log(p2p.warn)


def test_is_active2():
setup_logging(':info')
tester = slogging.get_logger('tester')
Expand Down Expand Up @@ -510,16 +525,16 @@ def test_count_logging_handlers():
# check named logger
eth_logger = slogging.getLogger('eth')
slogging.configure(config_string1)
assert len(eth_logger.handlers) == 1
assert len(eth_logger.handlers) == 0
slogging.configure(config_string1)
assert len(eth_logger.handlers) == 1
assert len(eth_logger.handlers) == 0

# check child of named logger
eth_vm_logger = slogging.getLogger('eth.vm')
slogging.configure(config_string2)
assert len(eth_vm_logger.handlers) == 1
assert len(eth_vm_logger.handlers) == 0
slogging.configure(config_string2)
assert len(eth_vm_logger.handlers) == 1
assert len(eth_vm_logger.handlers) == 0

if __name__ == '__main__':
pass