I have a Rabbitmq consumer that gets messages from another service with a unique_id. I need to set some environment variables which prints in every log within that process message block.
import logging
import os
LOG_FORMAT = '%(asctime)s - %(levelname)-7s %(message)s : unique_id : os.getenv("unique_id","1")'
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
also updating os var before message process block:
os.environ["unique_id"] = new_unique_id
however, with the above code log format is not taking the latest value of unique_id. It always prints 1. Seems it only takes value while initialization.
I only want to append unique_id for each log message in that particular block with updated unique_id. As it is a very big project so I don't want to append this to each log manually. I want to use a log format here.
New to the python, so not able to understand what I am doing wrong.
LOG_FORMATwill be fixed to the time when logger is configured. If you just want to prefix the logs, why not add it to your log message instead?logging.info(os.getenv("unique_id", "1") + "your message here")or create a function to wrap around logging calls? or check this answer