It's recommended to have a logger defined in each module like this:
import logging
logger = logging.getLogger(__name__)
Then in your main program, do the following:
import logging.config
logging.config.fileConfig('/path/to/logging.conf')
If you want to use one logger, you can create a variable for the logging module:
import logging
logger = logging
logger.basicConfig()
Then in other files, you will import the logger and use it from now on:
from base_logger import logger
Every logger is a child of the parent's package logger. Meaning that all you need to do is configure the root logger.
-
How To Color Python Logging Output?
If you are new to logging in Python, please feel free to start with our Introduction to Python logging to get started smoothly. Otherwise, here is how to color python logging output: đź” Want to cent...
Questions -
How To Disable Logging From The Python Request Library?
You can change the log level of the logger taking care of these messages. Setting the level to WARNING will remove the request messages and keep warnings and errors: import logging logging.getLogge...
Questions -
How To Log Uncaught Exceptions In Python?
For this, you can use the sys.excepthook that allows us to attach a handler for any unhandled exception: Creating a logger logger = logging.getLogger(name) logging.basicConfig(filename='example.log...
Questions -
How To Write Logs To A File With Python?
If you are new to logging in Python, please feel free to start with our Introduction to Python logging to get started smoothly. Otherwise, here is how to write logs to a file in Python: Using Basic...
Questions
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github