2010-12-13 58 views
15

Je suis en train d'installer un format pour l'enregistrement en python:Comment afficher le format de date en utilisant le module de journalisation 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) 

Fondamentalement, les travaux d'exploitation forestière, mais sans le format de date ...

Répondre

25

Vous pouvez ajoutez le paramètre datefmt-basicConfig:

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

Ou, pour définir le format pour le Rotating FileHandler:

fmt = logging.Formatter(FORMAT,datefmt='%Y-%m-%d') 
handler.setFormatter(fmt) 
+0

la deuxième suggestion a fonctionné, merci! –

+0

'datefmt' est passé à' time.strftime() 'en interne: voir la documentation [documentation] (https://docs.python.org/2/library/time.html#time.strftime) pour tout format valide cordes. – sevko