我正在尝试设置用于登录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)
基本上,日志记录有效,但没有日期格式......
答案 0 :(得分:30)
您可以将datefmt
参数添加到basicConfig
:
logging.basicConfig(format=FORMAT,level=logging.INFO,datefmt='%Y-%m-%d %H:%M:%S')
或者,设置Rotating FileHandler的格式:
fmt = logging.Formatter(FORMAT,datefmt='%Y-%m-%d')
handler.setFormatter(fmt)
答案 1 :(得分:0)
基本示例:
import logging
logging.basicConfig(
format='%(asctime)s %(levelname)s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S'
)
logging.info('Just a random string...')
# 2030-01-01 00:00:00 INFO Just a random string...
如果您想调整levelname 和message 之间的行距,请像示例一样更改%(levelname)s:
... %(levelname)-10s ...
# 2030-01-01 00:00:00 INFO Just a random string...