logging.warning('Watch out!') # will print a message to the console logging.info('I told you so') # will not print anything
WARNING:root:Watch out!
打印日志到文件
1 2 3 4 5 6 7 8 9 10
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too')
DEBUG:root:This message should go to the log file INFO:root:So should this WARNING:root:And this, too
import logging 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')
DEBUG:This message should appear on the console INFO:So should this WARNING:And this, too
日期
1 2 3 4 5
import logging logging.basicConfig(format='%(asctime)s %(message)s') logging.warning('is when this event was logged.')
2019-02-07 22:21:20,993is when this event was logged.