Instantly share code, notes, and snippets.

Embed
What would you like to do?
Configuring multiple loggers in a file
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)
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