Print timestamp for logging in Python

Before the first time you log anything do this:

logging.basicConfig(
    format="%(asctime)s %(levelname)-8s %(message)s",
    level=logging.INFO,
    datefmt="%Y-%m-%d %H:%M:%S")

Example on the REPL:

>>> import logging
>>> logging.basicConfig(
...         format="%(asctime)s %(levelname)-8s %(message)s",
...         level=logging.INFO,
...         datefmt="%Y-%m-%d %H:%M:%S")
>>> 
>>> logging.info('an info messge')
2017-05-25 00:58:28 INFO     an info messge
>>> logging.debug('a debug messag is not shown')
>>> 

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)