国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 曲松县| 聂荣县| 六枝特区| 沙雅县| 韶关市| 章丘市| 阆中市| 英吉沙县| 泰宁县| 同仁县| 江源县| 新河县| 榆树市| 化隆| 长宁区| 长乐市| 抚宁县| 乐东| 曲周县| 长寿区| 京山县| 灌南县| 高雄市| 襄汾县| 龙海市| 龙陵县| 青海省| 山阴县| 临西县| 大姚县| 杂多县| 新丰县| 嘉兴市| 威信县| 濮阳市| 合作市| 繁昌县| 塘沽区| 双鸭山市| 泸水县| 若尔盖县|