Sunday, 28 November 2021

How do I prefix my log messages with the current date and time?

I'm using Python 3.9 and Django 3.2. I have logging configured in my settings.py file

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}

When I do logging in one of my classes, I do it like so

import logging
...
class TransactionService:
    def __init__(self):
        self._logger = logging.getLogger(__name__)


    def my_method(self, arg1, arg2):
            ...
        self._logger.info("Doing some logging here.")
      

  

How do I configure my logger such that when the message is printed out, it is prefixed by the current date and time?



from How do I prefix my log messages with the current date and time?

No comments:

Post a Comment