国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python logging模塊學(xué)習(xí)筆記

2020-02-23 05:25:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

模塊級(jí)函數(shù)

logging.getLogger([name]):返回一個(gè)logger對(duì)象,如果沒有指定名字將返回root logger
logging.debug()、logging.info()、logging.warning()、logging.error()、logging.critical():設(shè)定root logger的日志級(jí)別
logging.basicConfig():用默認(rèn)Formatter為日志系統(tǒng)建立一個(gè)StreamHandler,設(shè)置基礎(chǔ)配置并加到root logger中

示例:logging_level_example.py
代碼如下:
import logging
import sys

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

if len(sys.argv) > 1:
    level_name = sys.argv[1]
    level = LEVELS.get(level_name, logging.NOTSET)
    logging.basicConfig(level=level)

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical error message')

輸出:
代碼如下:
$ python logging_level_example.py debug
DEBUG:root:This is a debug message
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical error message

$ python logging_level_example.py info
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical error message

Loggers

Logger.setLevel(lel):指定最低的日志級(jí)別,低于lel的級(jí)別將被忽略。debug是最低的內(nèi)置級(jí)別,critical為最高
Logger.addFilter(filt)、Logger.removeFilter(filt):添加或刪除指定的filter
Logger.addHandler(hdlr)、Logger.removeHandler(hdlr):增加或刪除指定的handler
Logger.debug()、Logger.info()、Logger.warning()、Logger.error()、Logger.critical():可以設(shè)置的日志級(jí)別

示例:simple_logging_module.py
代碼如下:
import logging

# create logger
logger = logging.getLogger("simple_example")
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")

輸出:
代碼如下:
$ python simple_logging_module.py
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎左旗| 阿拉善右旗| 通渭县| 涞水县| 北碚区| 淳化县| 全椒县| 阜康市| 舒兰市| 舞钢市| 水城县| 嘉定区| 永顺县| 新龙县| 张家界市| 灵寿县| 盐城市| 郎溪县| 尖扎县| 荔波县| 太和县| 秦安县| 韶关市| 奈曼旗| 韶山市| 高台县| 若羌县| 灌云县| 阳新县| 马关县| 宽城| 贵德县| 礼泉县| 沐川县| 友谊县| 和林格尔县| 明星| 乐昌市| 永善县| 广南县| 美姑县|