Created
November 20, 2022 10:20
-
-
Save h3xagn/8e0f26f8b09937eacc5fe33937f9481a to your computer and use it in GitHub Desktop.
Streaming logs using RabbitMQ: https://h3xagn.com
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 libraries | |
import time | |
import logging | |
from python_logging_rabbitmq import RabbitMQHandler | |
# set app name | |
APP_NAME = "app1" | |
# configure logging | |
FORMAT = "[%(asctime)s.%(msecs)03d] %(levelname)s [%(thread)d] [%(name)s] %(message)s" | |
formatter = logging.Formatter(fmt=FORMAT) | |
rabbit = RabbitMQHandler(formatter=formatter) | |
logger = logging.getLogger(APP_NAME) | |
logger.setLevel(logging.DEBUG) | |
# configure RabbitMQ logger | |
rabbit = RabbitMQHandler( | |
host="localhost", | |
port=5672, | |
exchange="amq.topic", | |
fields={"source": f"{APP_NAME}-producer", "env": "development"}, | |
fields_under_root=True, | |
) | |
logger.addHandler(rabbit) | |
# simulate logging messages | |
for i in range(1000): | |
logger.debug(f"... this is a log message from '{APP_NAME}'.") | |
time.sleep(1) | |
logger.info(f"--- this is a log message from '{APP_NAME}'.") | |
time.sleep(1) | |
logger.warning(f"*-* this is a log message from '{APP_NAME}'.") | |
time.sleep(1) | |
logger.error(f"*** this is a log message from '{APP_NAME}'.") | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment