pypxe / PyPXE

Pure Python PXE (DHCP-(Proxy)/TFTP/HTTP/NBD) Server
MIT License
539 stars 125 forks source link

Logger.getChild() incompatible with Python 2.6 #136

Closed icb- closed 7 years ago

icb- commented 8 years ago

Logger.getChild() doesn't exist until Python 2.7. PyPXE is supposed to support Python 2.6, which is the latest normally available on CentOS 6. Can we see about implementing a workaround so we can still support older versions of Python?

psychomario commented 8 years ago

So we use .getChild() in 4 places at the moment. Three are at the root level (PyPXE.[thing]) and one is at the level below. I propose we create our own getChild implementation similar to the following:

def getChild(logger, name):
    return logging.getLogger("{0}.{1}".format(logger.name, name))

A full example follows:

import logging

def getChild(logger, name):
    return logging.getLogger("{0}.{1}".format(logger.name, name))

sys_logger = logging.getLogger('PyPXE')
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(name)s %(message)s')
handler.setFormatter(formatter)
sys_logger.addHandler(handler)
sys_logger.setLevel(logging.INFO)

sys_logger.info("foo")
getChild(sys_logger, "bar").info("baz")
bash-3.2$ python2.6 test.py
2016-06-03 09:09:19,411 [INFO] PyPXE foo
2016-06-03 09:09:19,411 [INFO] PyPXE.bar baz

It would be easy to subclass the Logger object, but may add uneccessary complexity.

Opinions?

icb- commented 8 years ago

That looks reasonable to me, and works as expected in Python 2.6. Thanks!

psychomario commented 8 years ago

If you've got this working locally would you be able to submit a PR? I can't get to my dev box for a while at the moment.

icb- commented 8 years ago

Sure. I should be able to get to it this weekend.