qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          用python做測試實現(xiàn)高性能測試工具(5)—多進程寫log

          在上一篇中解決了系統(tǒng)的性能問題,但寫log又引入了問題,多進程寫log會引起混亂。
            查詢了多進程寫log 的方案, 主要有2種:
            利用多進程的Queue,把log放到統(tǒng)一的有個log queue里面,一個單獨的線程寫log
            起一個單獨的socket server,由 這個server來接受log,并負責(zé)寫log
            我覺得這2重方案都太重了,很多寫log的地方就需要改動了,希望找到一個方案能直接不改動老代碼寫log的方式,開始考慮的是每個進程單獨寫一個log,但這樣統(tǒng)計數(shù)據(jù)有點小不方便。 繼續(xù)探索到,有個開源的項目(https://launchpad.net/python-concurrent-log-handler),已經(jīng)實現(xiàn)了多進程寫log,但目前只是實現(xiàn)了按文件大小RotatingFileHandler, 按時間rotate 的功能還沒實現(xiàn)。不過這個已經(jīng)足夠用了。
          try:
          from cloghandler import ConcurrentRotatingFileHandler as RFHandler
          except ImportError:
          from warnings import warn
          warn("ConcurrentLogHandler package not installed.  Using builtin log handler")
          from logging.handlers import RotatingFileHandler as RFHandler
          rotateHandler = RFHandler("sim.log", "a", 10*1024*1024, 5)
          formatter = logging.Formatter('%(asctime)s [%(processName)s %(threadName)s %(levelname)s %(module)s:%(lineno)d] %(message)s')
          rotateHandler.setFormatter(formatter)
          log = logging.getLogger()
          log.addHandler(rotateHandler)
          log.setLevel(20)
          rotateHandler = RFHandler("sim.log", "a", 10*1024*1024, 5)
            log文件名為sim.log,  文件到10M就會rotate, 最多保留5個文件
            formatter = logging.Formatter('%(asctime)s [%(processName)s %(threadName)s %(levelname)s %(module)s:%(lineno)d] %(message)s')   設(shè)置log輸出的格式, 包括時間,進程名,線程名,模塊名字,代碼行數(shù)
            log.setLevel(20) 設(shè)置什么級別的log輸出,   CRITICAL 50; ERROR 40; WARNING 30; INFO 20; DEBUG 10, NOSET 0;
          import logging
          import time
          import multiprocessing
          class Customer(multiprocessing.Process):
          def __init__(self,mp_name):
          multiprocessing.Process.__init__(self,name=mp_name)
          def run(self):
          while 1:
          logging.debug(" I am here")
          time.sleep(1)
          for i in xrange(2):
          mp=Customer("customer"+str(i))
          mp.start()
            最后輸出log的例子是:
          2013-12-05 21:42:10,961 [customer0 MainThread DEBUG testqueue_old:115]  I am here
          2013-12-05 21:42:15,361 [customer1 MainThread DEBUG testqueue_old:115]  I am here
          相關(guān)文章
          用python做測試實現(xiàn)高性能測試工具(4)—系統(tǒng)架構(gòu)

          posted on 2014-01-08 10:43 順其自然EVO 閱讀(2135) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 闽清县| 晋中市| 上饶县| 三亚市| 德清县| 岐山县| 竹山县| 金阳县| 通辽市| 东海县| 黄浦区| 庄浪县| 鲜城| 安新县| 邵武市| 乌拉特中旗| 甘孜县| 苍梧县| 建始县| 日照市| 怀宁县| 襄垣县| 渭源县| 湘潭县| 安平县| 改则县| 嵊州市| 武宣县| 辰溪县| 文水县| 广安市| 藁城市| 兴国县| 梓潼县| 尖扎县| 巴里| 土默特右旗| 双牌县| 云阳县| 宁河县| 黑龙江省|