Closed zifeiYv closed 2 years ago
@Aiee could you kindly review this suggestion? thanks :)
Hello @zifeiYv, thanks for your feedback. Would you say a module-specific logger may solve this problem as proposed in https://github.com/vesoft-inc/nebula-python/issues/150#issuecomment-928969206? Any thoughts are welcome!
If there is no handlers in root logger, call basicConfig
will initialize the logging system in this current process, which means users cannot customize their logger performance anymore unless call their basicConfig
earlier than import nebula2
, or remove root handlers manually.
For example, if you code like this:
from nebula2.gclient.net import ConnectionPool
import logging
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] %(levelname)-8s [%(filename)s:%(lineno)d]:%(message)s')
logger = logging.getLogger(__name__)
logger.debug('debug msg')
will not print 'debug msg' in console because the logger level was already set to be logging.INFO
when you run from nebula2.gclient.net import ConnectionPool
.
To avoid this, there are two choices:
# option 1, call basicConfig earlier than import nebula2
import logging
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] %(levelname)-8s [%(filename)s:%(lineno)d]:%(message)s')
from nebula2.gclient.net import ConnectionPool
logger = logging.getLogger(__name__)
logger.debug('debug msg')
or
# option 2, remove all handlers in root logger and reset basicConfig
from nebula2.gclient.net import ConnectionPool
import logging
for handler in logging.root.handlers:
logging.root.removeHandler(handler)
logging.basicConfig(level=logging.DEBUG, format='[%(asctime)s] %(levelname)-8s [%(filename)s:%(lineno)d]:%(message)s')
logger = logging.getLogger(__name__)
logger.debug('debug msg')
It's troublesome, right? I don't know clearly if there is great demand for you developers to use basicConfig
in your code and I think the problem in #150 is similar to the situation here.
项目在这里调用了
logging.basicConfig
,我认为这种方式是不友好的,会使用户试图定义自己的logger对象时无法直接生效。建议移除。