How to make imported modules respect logging formats?
jseiser opened this issue · 1 comments
jseiser commented
Im basically using the logging configuration from the README. Its working fine, except for logs/exceptions that are bubbling up from a module i am using https://github.com/jasonrbriggs/stomp.py
These just log as plain text to the console.
I have a logger.py
from pythonjsonlogger import jsonlogger
import logging
from datetime import datetime
class CustomJsonFormatter(jsonlogger.JsonFormatter):
def add_fields(self, log_record, record, message_dict):
super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
if not log_record.get("timestamp"):
now = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
log_record["timestamp"] = now
if log_record.get("level"):
log_record["level"] = log_record["level"].upper()
else:
log_record["level"] = record.levelname
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logHandler = logging.StreamHandler()
fileHandler = logging.FileHandler("messagebus.log")
formatter = CustomJsonFormatter("%(timestamp)s %(level)s %(name)s %(message)s")
logHandler.setFormatter(formatter)
fileHandler.setFormatter(formatter)
logger.addHandler(logHandler)
logger.addHandler(fileHandler)
then i import it in my app by doing
import stomp
from logger import logger
Logs I create via logger.info etc all show as JSON, logs coming from stomp, just come as plain text.
Any help would be great.
Thanks,
jseiser commented
this line was my problem logger = logging.getLogger(__name__)