4 ms·
> `import logging ; logging.info("hello world")` I don't think that this is doing what you think it is doing (assuming that 'import logging' is importing the P
by pyre 11y ago
> `import logging ; logging.info("hello world")`
I don't think that this is doing what you think it is doing (assuming that 'import logging' is importing the Python logging package, and not relying on broken Python 2 package name resolution quirks to import a local module).
The `logging.{LEVEL}` functions always log in the root logger. The loggers that you initialize have a tree structure. There is a nice package for inspecting that here:
https://pypi.python.org/pypi/logging_tree https://pypi.python.org/pypi/logging_tree
Several different 3rd party libraries that you might be using will all have their own loggers that are initialized at their level. By separating things out like this, you can set different log levels for different loggers. For example, you might not want Component A logging at DEBUG level, while you want Component B logging at DEBUG level. This is why one would initialize things per package like this:
import logging
logger = logging.getLogger(__name__)
That logger's log level inherits from it's parent, and the structure mirrors the structure of your package/module hierarchy, so you can set the log level for an entire package, or just an individual module. Granted, you may not require such granularity, but doing this as a standard practice does give you the flexibility.
- m_mueller 11y agoWell yes, but with basic_config I'm telling the root logger in a central fashion what it's supposed to do - at least that's how I understand it. So far I've never had the urge to initialize separate child loggers for every component. Do people do that mainly so you can have fine grained control over log levels?
- mixmastamyk 11y agoYes, otherwise you can get messages from noisy libraries bubbling up to the user that may not be relevant in the app context. If the logging is just going to a file to look at later it matters less, still sometimes it is nice to shut them up.
- m_mueller 11y agoGood points. I'll probably switch to the modular way in the next project.
- pyre 11y agoExamples of libraries that can be noisy: * Flask-CORS: When I first started using this it's default logging level was really noisy. * factory (aka Factory Boy): You can turn in DEBUG level to get a deluge of information about what is happening with your test data. * SQLAlchemy: The 'echo' option to create_engine isn't the only way that you can control logging. E.g.: import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) Using logging_tree will give you a good idea of the loggers that have been initialized on the libraries that you're using.
- zenogais 11y agoOriginal post author here. This 100 times, few python programmers realize this, but this is exactly where that bullet point advice comes from.