當前位置:首頁 » 編程語言 » logpython

logpython

發布時間: 2022-02-10 03:24:17

python 將運行結果保存到log文件中遇到的問題

這個簡單啊。通過管道輸出到另一個程序里去,另外一個程序同時列印並輸出到out.log

這是我常用的辦法。

還有一個笨辦法,用自己的myprint代替所有的print語句。這樣你可以同時輸出到屏幕與文件了。

第三個辦法是通過logging。這個模塊支持多個listener,可以同時輸出到屏幕與文件以及網路。

Ⅱ python下比較好使的log模塊有哪些

Python只宜用其讀取和寫入EXCEL數據,並不宜用它去控制Excel Excel本身功能強大,也用不著用Python去幫忙 只有數據量特別大小,用python才有優勢

Ⅲ python的log可以像log4j一樣發郵件嗎

可以,需要自己寫發郵件的代碼,用smtplib就行。

Ⅳ 有幾千個TXT格式的log文件,怎麼用Python批量提取每個log文件固定位置的具體數值,輸出到一個TXT文件

1.將這些TXT文件先合並
2.找到這些具體數值的特徵
3.用正則表達式過濾提取
這陣子正學習python,不介意可以發給我,練著玩

Ⅳ 如何用python提取log文件中的特定字元串和數字

一般用正則表達式提取

Ⅵ python里怎模對復數取對數log,對矩陣實現log以3為底,怎麼做

#coding=utf-8
importmath
importnumpyasnp

#復數
aComplex=4.23+8.5j
#對復數的實數部分取對數
math.log(aComplex.real)
#對復數的虛數部分取對數
math.log(aComplex.imag)

#矩陣
aArray=np.array([1.0,2.0,3.0])
#對矩陣求log以3為底
foriinrange(len(aArray)):
aArray[i]=math.log(aArray[i],3)

Ⅶ 怎麼把python運行結果保存到log

通過管道輸出到另一個程序里去,另外一個程序同時列印並輸出到out.log。

用自己的myprint代替所有的print語句。這樣你可以同時輸出到屏幕與文件了。

通過logging。這個模塊支持多個listener,可以同時輸出到屏幕與文件以及網路。

Ⅷ 怎麼把python運行結果保存到log

python test.py >1.log
將輸出結果記錄到1.log(覆蓋寫入)
python test.py >>1.log
將輸出結果追加到1.log(每次追加)

Ⅸ python中log info 是什麼文件

a. 利用sys.stdout將print行導向到你定義的日誌文件中,例如:

import sys# make a of original stdout routestdout_backup = sys.stdout# define the log file that receives your log infolog_file = open("message.log", "w")# redirect print output to log filesys.stdout = log_fileprint "Now all print info will be written to message.log"# any command line that you will execute...

log_file.close()# restore the output to initial patternsys.stdout = stdout_backupprint "Now this will be presented on screen"

b. 利用logging模塊(規范化日誌輸出,推薦!!)
由於logging模塊的功能比較多,下面就放一些文檔里介紹的簡單的例子,更詳細具體的用法請戳這里

需求

最好的實現方式

故障調查或者狀態監測 logging.info()或logging.debug()(後者常用於針對性檢測診斷的目的)

特定運行事件發出警告 logging.warning()

報告錯誤抑制不出發異常(常見於長時間運行的伺服器進程的錯誤處理程序) logging.error(), logging.exception()或者logging.critical()

而以下是根據事件的嚴重性程度而應採取的logging函數的建議:

程度

使用場景

DEBUG 獲得診斷問題是具體的信息

INFO 確認程序是否按正常工作

WARNING 在程序還正常運行時獲取發生的意外的信息,這可能會在之後引發異常(例如磁碟空間不足)

ERROR 獲取程序某些功能無法正常調用這類嚴重異常的信息

CRITICAL 獲取程序無法繼續運行的這類最嚴重異常信息

默認的等級是WARNING,也就是說logging函數在沒有特別配置的前提下只追蹤比WARNING程度更嚴重的異常。

下面就用一些例子來具體說明如何用logging函數記錄日誌信息:

# this is a simple exampleimport logging# define the log file, file mode and logging levellogging.basicConfig(filename='example.log', filemode="w", level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

查看example.log文件可以看到以下信息:

DEBUG:root:This message should go to the log fileINFO:root:So should thisWARNING:root:And this, too

從多個文件記錄日誌

# myapp.pyimport loggingimport mylibdef main():
logging.basicConfig(filename='myapp.log', level=logging.INFO)
logging.info('Started')
mylib.do_something()
logging.info('Finished')if __name__ == '__main__':
main()
# mylib.pyimport loggingdef do_something():
logging.info('Doing something')

輸出的信息為

INFO:root:StartedINFO:root:Doing somethingINFO:root:Finished

改變默認輸出信息的格式

import logging# output format: output time - logging level - log messageslogging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
logging.warning('This message will appear in python console.')

在python console中直接列印以下輸出:

2016-8-2 2:59:11, 510 - WARNING - This message will appear in python console

logging高級用法
可以通過構建logger或者讀取logging config文件對logging函數進行任意配置。

  • 構建logger法

  • import logging# create loggerlogger = logging.getLogger('simple_example')

  • logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler()

  • ch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')

  • logger.info('info message')

  • logger.warn('warn message')

  • logger.error('error message')

  • logger.critical('critical message')

  • 以上程序結果會輸出到python console中:

  • $ python simple_logging_mole.py2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message2005-03-19 15:10:26,620 - simple_example - INFO - info message2005-03-19 15:10:26,695 - simple_example - WARNING - warn message2005-03-19 15:10:26,697 - simple_example - ERROR - error message2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message

  • 讀取配置文件法

  • import loggingimport logging.config


  • logging.config.fileConfig('logging.conf')# create loggerlogger = logging.getLogger('simpleExample')# 'application' codelogger.debug('debug message')

  • logger.info('info message')

  • logger.warn('warn message')

  • logger.error('error message')

  • logger.critical('critical message')

  • 其中logging.conf文件格式為:(其實就是將前一種方法的各項配置參數寫到logging.conf文件中)

  • [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=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt= '%m/%d/%Y %I:%M:%S %p'

  • 與前面一樣,上述文件輸出結果為:

  • $ python simple_logging_config.py2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message2005-03-19 15:38:55,979 - simpleExample - INFO - info message2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message2005-03-19 15:38:56,055 - simpleExample - ERROR - error message2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message

Ⅹ python log中文亂碼

Python log出現中文亂碼的解決方法:修改「handle」的「encode」參數為「utf-8」,即在源碼中修改「encoding='utf-8'」。

python log寫入中文亂碼,直接修改handle的encode參數為utf-8
即在源碼中修改encoding='utf-8',因為 logging.basicConfig() 配置時實際上是用到了4大組件,只不過給了默認值,在loging.FileHandler()方法中默認是這樣的。

只需在源碼中修改。
推薦課程:零基礎入門學習Python(小甲魚)

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:333
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:374
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:609
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:29
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:104
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:940
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:736
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:800
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:507
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:369