Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This worked for me (adapted from thorndeux's answer):

import logging.config

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'prepend_date': {
            'format': '{asctime} {levelname}: {message}',
            'style': '{',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'prepend_date',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}

logging.config.dictConfig(LOGGING)
logging.info('foo')
logging.warning('bar')

prints

2021-11-28 16:05:13,469 INFO: foo
2021-11-28 16:05:13,469 WARNING: bar
over 4 years ago · Santiago Trujillo Report

0

Add a formatter to your logger. For example:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'prepend_date': {
            'format': '{asctime} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'prepend_date',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO',
    },
}
over 4 years ago · Santiago Trujillo Report

0

The answer to your question can be found in the official documentation, please read the full article.

import logging

FORMAT = '%(asctime)s  %(message)s'
logging.basicConfig(format=FORMAT)

logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.info("Doing some logging here.")

This would print:

2021-11-29 14:06:59,825  Doing some logging here.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!