Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display date format using Python logging module

Tags:

I am trying to setup a format for logging in python:

import logging,logging.handlers FORMAT = "%(asctime)-15s %(message)s" logging.basicConfig(format=FORMAT,level=logging.INFO) logger = logging.getLogger("twitter") handler = logging.handlers.RotatingFileHandler('/var/log/twitter_search/message.log', maxBytes=1024000, backupCount=5) logger.addHandler(handler) 

Basically, logging works, but without the date format...

like image 293
James Lin Avatar asked Dec 13 '10 20:12

James Lin


1 Answers

You can add the datefmt parameter to basicConfig:

logging.basicConfig(format=FORMAT,level=logging.INFO,datefmt='%Y-%m-%d %H:%M:%S') 

Or, to set the format for the Rotating FileHandler:

fmt = logging.Formatter(FORMAT,datefmt='%Y-%m-%d') handler.setFormatter(fmt) 
like image 164
unutbu Avatar answered Oct 17 '22 04:10

unutbu