Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建议移除logging.basicConfig #163

Closed
zifeiYv opened this issue Oct 28, 2021 · 3 comments · Fixed by #186
Closed

建议移除logging.basicConfig #163

zifeiYv opened this issue Oct 28, 2021 · 3 comments · Fixed by #186

Comments

@zifeiYv
Copy link

zifeiYv commented Oct 28, 2021

项目在这里调用了logging.basicConfig,我认为这种方式是不友好的,会使用户试图定义自己的logger对象时无法直接生效。

建议移除。

@wey-gu
Copy link
Contributor

wey-gu commented Nov 8, 2021

@Aiee could you kindly review this suggestion? thanks :)

@Aiee
Copy link
Contributor

Aiee commented Nov 8, 2021

Hello @zifeiYv, thanks for your feedback. Would you say a module-specific logger may solve this problem as proposed in #150 (comment)?
Any thoughts are welcome!

@zifeiYv
Copy link
Author

zifeiYv commented Nov 16, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants