python的日誌logging模組效能以及多程序

2021-07-16 01:33:15 字數 2066 閱讀 4482

寫在前面:

日誌是記錄操作的一種好方式。但是日誌,基本都是基於檔案的,也就是要寫到磁碟上的。這時候,磁碟將會成為乙個效能瓶頸。對於普通的伺服器硬碟(機械磁碟,非固態硬碟),python日誌的效能瓶頸是多少呢?今天我們就來測一下。

測試**如下:

#! /usr/bin/env python

#coding=utf-8

# *************************===

# describe : 給平台提供的日誌

# d&p author by: 常成功

# create date: 2016/08/01

# modify date: 2016/08/01

# *************************===

import time

import os

import logging

print "start test ...."

s_tm = time.time()

test_time = 10.0 # 測試時間10秒

e_tm = s_tm + 10

j = 0

pid = str(os.getpid())

while 1:

now_time = time.time()

j += 1

if now_time > e_tm:

break

# 生成資料夾

lujing = "d:\\test_log"

if not os.path.exists(lujing):

os.mkdir(lujing)

fm2 = '%y%m%d'

ymd = time.strftime(fm2, time.localtime(now_time))

filename = 'recharge_' + ymd + '.log'

log_file = os.path.join(lujing, filename)

t = "\t"

log_msg = str(j) +t+ str(now_time) +t+ pid

the_logger = logging.getlogger('recharge_log')

f_handler = logging.filehandler(log_file)

the_logger.addhandler(f_handler)

the_logger.setlevel(logging.info)

# to pass exception information, use the keyword argument exc_info with a true value

the_logger.info(log_msg, exc_info=false)

the_logger.removehandler(f_handler)

rps = j/test_time

print rps, "rows per second"

結果為:start test ....

7200轉的機械磁碟,測了幾次,每秒的能寫入日誌的行數(每行就是一條日誌),數量基本在

2800-3000

之間。此時,磁碟io基本已經跑滿。(在3.3ghz的cpu上,cpu占用大約40%)。

python 的 logging模組,是執行緒安全的。但對於多程序的程式來說,怎麼去寫日誌檔案呢?我的解決辦法是,每個程序的pid,寫乙個單獨的日誌檔案。再用演算法把所有程序的日誌合併起來,生成新的日誌。

詳解Python中的日誌模組logging

許多應用程式中都會有日誌模組,用於記錄系統在執行過程中的一些關鍵資訊,以便於對系統的執行狀況進行跟蹤。在.net平台中,有非常著名的第三方開源日誌元件log4net,c 中,有人們熟悉的log4cpp,而在python中,我們不需要第三方的日誌元件,因為它已經為我們提供了簡單易用 且功能強大的日誌模...

python日誌 python日誌處理

一 日誌概念 日誌是一種可以追蹤某些軟體執行時所發生事件的方法。軟體開發人員可以向他們的 中 呼叫日誌記錄相關的方法來表明發生了某些事情。乙個事件可以用乙個可包含可選變數資料 的訊息來描述。此外,事件也有重要性的概念,這個重要性也可以被稱為嚴重性級別 level python自身也提供了乙個用於記錄...

python日誌等級 python 日誌輸出級別

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 ...