import logging logging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘, datefmt=‘%a, %d %b %Y %H:%M:%S‘, filename=‘/tmp/test.log‘, filemode=‘w‘) logging.debug(‘debug message‘) logging.info(‘info message‘) logging.warning(‘warning message‘) logging.error(‘error message‘) logging.critical(‘critical message‘)
import logging # 设置root logger r = logging.getLogger() ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter(‘%(asctime)s - %(levelname)s - %(message)s‘) ch.setFormatter(formatter) r.addHandler(ch) # 创建一个logger作为父亲 p = logging.getLogger(‘foo‘) p.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter(‘%(asctime)s - %(message)s‘) ch.setFormatter(formatter) p.addHandler(ch) # 创建一个孩子logger c = logging.getLogger(‘foo.bar‘) c.debug(‘foo‘)
时间: 2024-10-10 11:14:02