最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python logging類庫使用例子

 更新時間:2014年11月22日 14:21:21   投稿:junjie  
這篇文章主要介紹了python logging類庫使用例子,本文講解了簡單使用、logging的level、Handlers、FileHandler + StreamHandler等內(nèi)容,需要的朋友可以參考下

一、簡單使用

復(fù)制代碼 代碼如下:

def TestLogBasic():
    import logging
    logging.basicConfig(filename = 'log.txt', filemode = 'a', level = logging.NOTSET, format = '%(asctime)s - %(levelname)s: %(message)s')
    logging.debug('this is a message')
    logging.info("this is a info")
    logging.disable(30)#logging.WARNING
    logging.warning("this is a warnning")
    logging.critical("this is a critical issue")
    logging.error("this is a error")
    logging.addLevelName(88,"MyCustomError")
    logging.log(88,"this is an my custom error")
    try:
      raise Exception('this is a exception')
    except:
      logging.exception( 'exception')
    logging.shutdown()

TestLogBasic()

說明:(此實例為最簡單的用法,用來將log記錄到log文件中)

1)logging.basicConfig()中定義默認(rèn)的log到log.txt,log文件為append模式,處理所有的level大于logging.NOTSET的logging,log的格式定義為'%(asctime)s - %(levelname)s: %(message)s';

2)使用logging.debug()...等來log相應(yīng)level的log;

3)使用logging.disable()來disable某個logging level;

4)使用logging.addLevelName增加自定義的logging level;

5)使用logging.log來log自定義的logging level的log;

輸出的text的log如下:

復(fù)制代碼 代碼如下:

2011-01-18 10:02:45,415 - DEBUG: this is a message
2011-01-18 10:02:45,463 - INFO: this is a info
2011-01-18 10:02:45,463 - CRITICAL: this is a critical issue
2011-01-18 10:02:45,463 - ERROR: this is a error
2011-01-18 10:02:45,463 - MyCustomError: this is an my custom error
2011-01-18 10:02:45,463 - ERROR: exception
Traceback (most recent call last):
  File "testlog.py", line 15, in TestLogBasic
    raise Exception('this is a exception')
Exception: this is a exception

二、logging的level

復(fù)制代碼 代碼如下:

#logging level
#logging.NOTSET 0
#logging.DEBUG 10
#logging.INFO 20
#logging.WARNING 30
#logging.ERROR 40
#logging.CRITICAL 50

logging的level對應(yīng)于一個int,例如10,20...用戶可以自定義logging的level。

可以使用logging.setLevel()來指定要處理的logger級別,例如my_logger.setLevel(logging.DEBUG)表示只處理logging的level大于10的logging。
 

三、Handlers

Handler定義了log的存儲和顯示方式。

NullHandler不做任何事情。

StreamHandler實例發(fā)送錯誤到流(類似文件的對象)。
FileHandler實例發(fā)送錯誤到磁盤文件。
BaseRotatingHandler是所有輪徇日志的基類,不能直接使用。但是可以使用RotatingFileHandler和TimeRotatingFileHandler。
RotatingFileHandler實例發(fā)送信息到磁盤文件,并且限制最大的日志文件大小,并適時輪徇。
TimeRotatingFileHandler實例發(fā)送錯誤信息到磁盤,并在適當(dāng)?shù)氖录g隔進行輪徇。
SocketHandler實例發(fā)送日志到TCP/IP socket。
DatagramHandler實例發(fā)送錯誤信息通過UDP協(xié)議。
SMTPHandler實例發(fā)送錯誤信息到特定的email地址。
SysLogHandler實例發(fā)送日志到UNIX syslog服務(wù),并支持遠(yuǎn)程syslog服務(wù)。
NTEventLogHandler實例發(fā)送日志到WindowsNT/2000/XP事件日志。
MemoryHandler實例發(fā)送日志到內(nèi)存中的緩沖區(qū),并在達到特定條件時清空。
HTTPHandler實例發(fā)送錯誤信息到HTTP服務(wù)器,通過GET或POST方法。
NullHandler,StreamHandler和FileHandler類都是在核心logging模塊中定義的。其他handler定義在各個子模塊中,叫做logging.handlers。

當(dāng)然還有一個logging.config模塊提供了配置功能。

四、FileHandler + StreamHandler

復(fù)制代碼 代碼如下:

def TestHanderAndFormat():
    import logging
    logger = logging.getLogger("simple")
    logger.setLevel(logging.DEBUG)
   
    # create file handler which logs even debug messages
    fh = logging.FileHandler("simple.log")
    fh.setLevel(logging.DEBUG)
   
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.ERROR)
   
    # create formatter and add it to the handlers
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)
   
    # add the handlers to logger
    logger.addHandler(ch)
    logger.addHandler(fh)

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

TestHanderAndFormat()

說明:(此實例同時使用FileHandler和StreamHandler來實現(xiàn)同時將log寫到文件和console)

1)使用logging.getLogger()來新建命名logger;

2)使用logging.FileHandler()來生成FileHandler來將log寫入log文件,使用logger.addHandler()將handler與logger綁定;

3)使用logging.StreamHandler()來生成StreamHandler來將log寫到console,使用logger.addHandler()將handler與logger綁定;

4)使用logging.Formatter()來構(gòu)造log格式的實例,使用handler.setFormatter()來將formatter與handler綁定;

 運行結(jié)果

simple.txt

復(fù)制代碼 代碼如下:

2011-01-18 11:25:57,026 - simple - DEBUG - debug message
2011-01-18 11:25:57,072 - simple - INFO - info message
2011-01-18 11:25:57,072 - simple - WARNING - warn message
2011-01-18 11:25:57,072 - simple - ERROR - error message
2011-01-18 11:25:57,072 - simple - CRITICAL - critical message

console

復(fù)制代碼 代碼如下:

2011-01-18 11:25:57,072 - simple - ERROR - error message
2011-01-18 11:25:57,072 - simple - CRITICAL - critical message

五、RotatingFileHandler

復(fù)制代碼 代碼如下:

def TestRotating():
    import glob
    import logging
    import logging.handlers
   
    LOG_FILENAME = 'logging_rotatingfile_example.out'

    # Set up a specific logger with our desired output level
    my_logger = logging.getLogger('MyLogger')
    my_logger.setLevel(logging.DEBUG)

    # Add the log message handler to the logger
    handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, backupCount=5)

    my_logger.addHandler(handler)

    # Log some messages
    for i in range(20):
        my_logger.debug('i = %d' % i)

    # See what files are created
    logfiles = glob.glob('%s*' % LOG_FILENAME)

    for filename in logfiles:
        print(filename)
       
TestRotating()

說明:

RotatingFileHandler指定了單個log文件的size的最大值和log文件的數(shù)量的最大值,如果文件大于最大值,將分割為多個文件,如果log文件的數(shù)量多于最多個數(shù),最老的log文件將被刪除。例如此例中最新的log總是在logging_rotatingfile_example.out,logging_rotatingfile_example.out.5中包含了最老的log。

運行結(jié)果:

復(fù)制代碼 代碼如下:

logging_rotatingfile_example.out
logging_rotatingfile_example.out.1
logging_rotatingfile_example.out.2
logging_rotatingfile_example.out.3
logging_rotatingfile_example.out.4
logging_rotatingfile_example.out.5

六、使用fileConfig來使用logger

復(fù)制代碼 代碼如下:

import logging
import logging.config

logging.config.fileConfig("logging.conf")

# create logger
logger = logging.getLogger("simpleExample")

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

logging.conf文件如下:

復(fù)制代碼 代碼如下:

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

運行結(jié)果:

復(fù)制代碼 代碼如下:

2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message
2005-03-19 15:38:55,979 - simpleExample - INFO - info message
2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message
2005-03-19 15:38:56,055 - simpleExample - ERROR - error message
2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message

相關(guān)文章

  • 解決一個pandas執(zhí)行模糊查詢sql的坑

    解決一個pandas執(zhí)行模糊查詢sql的坑

    這篇文章主要介紹了解決一個pandas執(zhí)行模糊查詢sql的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python中深拷貝與淺拷貝的區(qū)別介紹

    Python中深拷貝與淺拷貝的區(qū)別介紹

    這篇文章介紹了Python中深拷貝與淺拷貝的區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Python自定義指標(biāo)聚類實例代碼

    Python自定義指標(biāo)聚類實例代碼

    K-means算法是最為經(jīng)典的基于劃分的聚類方法,是十大經(jīng)典數(shù)據(jù)挖掘算法之一,下面這篇文章主要給大家介紹了關(guān)于Python自定義指標(biāo)聚類的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • python模擬點擊玩游戲的實例講解

    python模擬點擊玩游戲的實例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python模擬點擊玩游戲的實例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-11-11
  • Python爬蟲設(shè)置代理IP的方法(爬蟲技巧)

    Python爬蟲設(shè)置代理IP的方法(爬蟲技巧)

    這篇文章主要介紹了Python爬蟲設(shè)置代理IP的方法(爬蟲技巧),需要的朋友可以參考下
    2018-03-03
  • python 處理dataframe中的時間字段方法

    python 處理dataframe中的時間字段方法

    下面小編就為大家分享一篇python 處理dataframe中的時間字段方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Selenium(Python web測試工具)基本用法詳解

    Selenium(Python web測試工具)基本用法詳解

    這篇文章主要介紹了Selenium(Python web測試工具)基本用法,結(jié)合實例形式分析了Selenium的基本安裝、簡單使用方法及相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • Python pip 常用命令匯總

    Python pip 常用命令匯總

    這篇文章主要介紹了Python pip 常用命令匯總,幫助大家更好的理解和使用pip命令,感興趣的朋友可以了解下
    2020-10-10
  • python對raw格式照片進行降噪處理的方法詳解

    python對raw格式照片進行降噪處理的方法詳解

    要對RAW格式的照片進行降噪,我們可以使用rawpy庫來讀取RAW圖像,并使用imageio庫將處理后的圖像保存為其他格式,如PNG或JPEG,本文將詳細(xì)給大家介紹python如何對raw格式照片進行降噪處理,文中有詳細(xì)的代碼流程,需要的朋友可以參考下
    2023-05-05
  • pandas的排序、分組groupby及cumsum累計求和方式

    pandas的排序、分組groupby及cumsum累計求和方式

    這篇文章主要介紹了pandas的排序、分組groupby及cumsum累計求和方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論

张家川| 房产| 泗水县| 巢湖市| 子长县| 连山| 公主岭市| 安康市| 晋江市| 石门县| 寻甸| 通榆县| 宁乡县| 黔西| 准格尔旗| 罗定市| 米脂县| 沙田区| 诸暨市| 安吉县| 安庆市| 霍山县| 阳曲县| 吉安县| 彭阳县| 星座| 贡山| 长顺县| 杭锦旗| 柳州市| 山东省| 临澧县| 张北县| 肥城市| 古浪县| 荣昌县| 贺兰县| 昭平县| 镇康县| 石首市| 维西|