logging.warning('%s before you %s', 'Look', 'leap!')
更改消息显示的格式
1 2 3 4
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) logging.debug('This message should appear on the console') logging.info('So should this') logging.warning('And this, too')
1 2 3
DEBUG:This message should appear on the console INFO:So should this WARNING:And this, too
在消息中显示日期和时间
1
logging.basicConfig(format='%(asctime)s %(message)s') logging.warning('is when this event was logged.')
1
2024-04-17 23:01:48,365 is when this event was logged.
1 2
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') logging.warning('is when this event was logged.')
datefmt 参数的格式与 time.strftime() 支持的格式相同
1
04/17/2024 11:03:12 PM is when this event was logged.