這篇文章主要講解了“如何使用Python3實(shí)時(shí)操作處理日志文件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何使用Python3實(shí)時(shí)操作處理日志文件”吧!
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到雄縣網(wǎng)站設(shè)計(jì)與雄縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋雄縣地區(qū)。
假設(shè)我們要實(shí)時(shí)讀取的日志的路徑為: /data/MongoDB/shard1/log/pg.csv
那么我們可以在python文件中使用shell腳本命令tail -F 進(jìn)行實(shí)時(shí)讀取并操作
代碼如下:
import re import codecs import subprocess def pg_data_to_elk(): p = subprocess.Popen('tail -F /data/mongodb/shard1/log/pg.csv', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,) #起一個(gè)進(jìn)程,執(zhí)行shell命令 while True: line = p.stdout.readline() #實(shí)時(shí)獲取行 if line: #如果行存在的話 xxxxxxxxxxxx your operation
簡(jiǎn)單解釋一下subprocess模塊:
subprocess允許你生成新的進(jìn)程,連接到它們的 input/output/error 管道,并獲取它們的返回(狀態(tài))碼。
subprocess.Popen介紹
該類用于在一個(gè)新的進(jìn)程中執(zhí)行一個(gè)子程序。
subprocess.Popen的構(gòu)造函數(shù)
class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startup_info=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())
參數(shù)說(shuō)明:
args: 要執(zhí)行的shell命令,可以是字符串,也可以是命令各個(gè)參數(shù)組成的序列。當(dāng)該參數(shù)的值是一個(gè)字符串時(shí),該命令的解釋過(guò)程是與平臺(tái)相關(guān)的,因此通常建議將args參數(shù)作為一個(gè)序列傳遞。
stdin, stdout, stderr: 分別表示程序標(biāo)準(zhǔn)輸入、輸出、錯(cuò)誤句柄。
shell: 該參數(shù)用于標(biāo)識(shí)是否使用shell作為要執(zhí)行的程序,如果shell值為T(mén)rue,則建議將args參數(shù)作為一個(gè)字符串傳遞而不要作為一個(gè)序列傳遞。
如果日志會(huì)在滿足一定條件下產(chǎn)生新的日志文件,比如log1.csv已經(jīng)到了20M,那么則會(huì)寫(xiě)入log2.csv,這樣一天下來(lái)大概有1000多個(gè)文件,且不斷產(chǎn)生新的,那么如何進(jìn)行實(shí)時(shí)獲取呢?
思路如下:
在實(shí)時(shí)監(jiān)聽(tīng)(tail -F)中加入當(dāng)前文件的大小判定,如果當(dāng)前文件大小大于20M,那么跳出實(shí)時(shí)監(jiān)聽(tīng),獲取新的日志文件。(如果有其他判定條件也是這個(gè)思路,只不過(guò)把當(dāng)前文件大小的判定換成你所需要的判定)
代碼如下:
import re import os import time import codecs import subprocess from datetime import datetime path = '/home/liao/python/csv' time_now_day = datetime.now.strftime('%Y-%m-%d') def get_file_size(new_file): fsize = os.path.getsize(new_file) fsize = fsize/float(1024*1024) return fsize def get_the_new_file(): files = os.listdir(path) files_list = list(filter(lambda x:x[-4:]=='.csv' and x[11:21]==time_now_day, files)) files_list.sort(key=lambda fn:os.path.getmtime(path + '/' + fn) if not os.path.isdir(path + '/' + fn) else 0) new_file = os.path.join(path, files_list[-1]) return new_file def pg_data_to_elk(): while True: new_file = get_the_new_file() p = subprocess.Popen('tail -F {0}'.format(new_file), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,) #起一個(gè)進(jìn)程,執(zhí)行shell命令 while True: line = p.stdout.readline() #實(shí)時(shí)獲取行 if line: #如果行存在的話 if get_file_size(new_file) > 20: #如果大于20M,則跳出循環(huán) break xxxxxxxxxxxx your operation time.sleep(3)
感謝各位的閱讀,以上就是“如何使用Python3實(shí)時(shí)操作處理日志文件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何使用Python3實(shí)時(shí)操作處理日志文件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
標(biāo)題名稱:如何使用Python3實(shí)時(shí)操作處理日志文件
文章來(lái)源:http://aaarwkj.com/article32/iighpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、電子商務(wù)、網(wǎng)站收錄、ChatGPT、小程序開(kāi)發(fā)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)