Last active
October 31, 2020 10:14
-
-
Save muya/2dff1cd8c5b42f1dabab to your computer and use it in GitHub Desktop.
Configuring multiple loggers in a file - accompanying blog post: https://blog.muya.co.ke/configuring-multiple-loggers-python/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from logging import FileHandler | |
from logging import Formatter | |
LOG_FORMAT = ( | |
"%(asctime)s [%(levelname)s]: %(message)s in %(pathname)s:%(lineno)d") | |
LOG_LEVEL = logging.INFO | |
# messaging logger | |
MESSAGING_LOG_FILE = "/tmp/wasted_meerkats/messaging.log" | |
messaging_logger = logging.getLogger("wasted_meerkats.messaging") | |
messaging_logger.setLevel(LOG_LEVEL) | |
messaging_logger_file_handler = FileHandler(MESSAGING_LOG_FILE) | |
messaging_logger_file_handler.setLevel(LOG_LEVEL) | |
messaging_logger_file_handler.setFormatter(Formatter(LOG_FORMAT)) | |
messaging_logger.addHandler(messaging_logger_file_handler) | |
# payments logger | |
PAYMENTS_LOG_FILE = "/tmp/wasted_meerkats/payments.log" | |
payments_logger = logging.getLogger("wasted_meerkats.payments") | |
payments_logger.setLevel(LOG_LEVEL) | |
payments_file_handler = FileHandler(PAYMENTS_LOG_FILE) | |
payments_file_handler.setLevel(LOG_LEVEL) | |
payments_file_handler.setFormatter(Formatter(LOG_FORMAT)) | |
payments_logger.addHandler(payments_file_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from logger import messaging_logger | |
from logger import payments_logger | |
messaging_logger.info("The meerkats are drunk!") | |
payments_logger.info("Who knows where they got the money?!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment