ip登錄打印機怎么打印
Often on Python, especially as a beginner, you might print( ) a variable in order to see what is happening in your program. It is possible if you rely on too many print statements throughout your program you will face the nightmare of having to comment them all out towards the end. A much more pythonic way to see what the program is doing is logging. You can then limit your prints to command line outputs that the end-user wants to see.
通常在Python上(尤其是作為初學者),您可以將變量print()以便查看程序中正在發生的事情。 如果您在整個程序中依賴過多的打印語句,則可能會面臨噩夢,不得不在最后將所有注釋都注釋掉。 查看程序正在執行的另一種Python方法是日志記錄。 然后,您可以將打印內容限制為最終用戶希望查看的命令行輸出。
為什么要登錄? (Why log?)
Logging is a comfortable tool to see the status of the program in the development phase. It is ideal if:
日志記錄是在開發階段查看程序狀態的便捷工具。 理想的情況是:
- You want to differentiate between debug output and program output 您要區分調試輸出和程序輸出
- You don’t want irrelevant stdout in your final product 您不想在最終產品中使用無關緊要的標準配置
- You would like to disable all stdout after development and test 您想在開發和測試后禁用所有stdout
- You want to save your program execution history to a file with meta information like time and more detailed debug information. 您想要將程序執行歷史記錄保存到包含元信息(例如時間和更詳細的調試信息)的文件中。
如何登錄? (How to log?)
You log in python using the logging module.
您可以使用日志記錄模塊登錄python。
import logging
The logging module provides some default states of logging various status messages in your program. The default levels are DEBUG
, INFO
, WARNING
, ERROR
, and CRITICAL
.
日志記錄模塊提供了一些默認狀態,用于記錄程序中的各種狀態消息。 默認級別為DEBUG
, INFO
, WARNING
, ERROR
和CRITICAL
。
If you execute the following program:
如果執行以下程序:
import logging
import os
savepath = ‘path/to/save/at/’if not os.path.exists(savepath):
logging.warning(‘Warning! The savepath provided doesn\’t exist!'
'Saving at current directory ‘)
savepath = os.getcwd()
logging.info(‘savepath reset at %s’,str(savepath))else:
logging.info('Savepath provided is correct,'
'saving at %s’,str(savepath))
The path in the example Doesn't exist. You will see the following output:
示例中的路徑不存在。 您將看到以下輸出:
WARNING:root:warning! The savepath provided doesn’t exist!Saving at current directory
The new current save path information, that is logging.info is not displayed.
不顯示新的當前保存路徑信息,即logging.info。
This is because the default level of severity of output is “warning”. The order of severity is as follows:
這是因為默認的輸出嚴重性級別是“警告”。 嚴重性順序如下:
DEBUG
DEBUG
INFO
INFO
WARNING
(This is default)
WARNING
(這是默認設置)
ERROR
ERROR
CRITICAL
CRITICAL
If you want to see outputs of a lower severity level, you have to explicitly set them in the logging configuration. Start a new interpreter after this setting, else it won’t work.
如果要查看較低嚴重性級別的輸出,則必須在日志記錄配置中顯式設置它們。 進行此設置后,請啟動新的解釋器,否則將無法使用。
logging.basicConfig(level = logging.DEBUG)
Now, everything will be printed out, starting from level debug.
現在,從級別調試開始,所有內容都將被打印出來。
output:
輸出:
WARNING:root:warning! The savepath provided doesn’t exist! Saving at current directory INFO:root:savepath reset at path/to/current/directory
That is awesome! But for a decent log, we perhaps want to see more information and make it available in a separate file. This can be set using the format and filename in the configuration. Add the desired configuration at the beginning of your file:
太棒了! 但是對于一個像樣的日志,我們可能希望查看更多信息,并在單獨的文件中提供它。 可以使用配置中的格式和文件名進行設置。 在文件開頭添加所需的配置:
logging.basicConfig(filename=’logfilename.log’,level =
logging.DEBUG,format=’%(asctime)s %
(message)s’,
datefmt=’%d/%m/%Y %I:%M:%S %p’)
This logs your information to logfilename.log along with timestamps.
這會將您的信息和時間戳一起記錄到logfilename.log中。
13/06/2020 05:14:47 PM warning! The savepath provided doesn’t exist!
Saving at current directory
13/06/2020 05:14:47 PM savepath reset at current/working/directory
The next time you run the file, logs get appended to the textfile, so that you have a historic record of your logs in one place. You can also change this and create a new text file each time by adding this to your logging.basicConfig
下次運行該文件時,日志將追加到文本文件中,這樣一來,您就可以在歷史記錄中記錄日志。 您還可以通過將其添加到logging.basicConfig中來更改此設置并每次創建一個新的文本文件。
filemode='w'
You can disable all logging centrally by adding the following to your script, without having to comment each of the messages out:
您可以通過在腳本中添加以下內容來集中禁用所有日志記錄,而不必注釋掉每條消息:
logger = logging.getLogger()
logger.disabled = True
Alternatively, you can choose to change the level of output needed to be written to the log file to only enable warning and critical error messages to the end user.
或者,您可以選擇更改需要寫入日志文件的輸出級別,以僅向最終用戶啟用警告和嚴重錯誤消息。
That was a primer to logging on python. Logging offers much more flexibility than described above (Handlers, filters, changing the default log levels, etc) But this should be enough to start using basic logging and do away with print statements.
那是登錄python的入門。 日志記錄提供了比上述更多的靈活性(處理程序,過濾器,更改默認日志級別等),但這足以開始使用基本日志記錄并消除打印語句。
翻譯自: https://towardsdatascience.com/dont-print-log-85df4c153abb
ip登錄打印機怎么打印
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/390558.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/390558.shtml 英文地址,請注明出處:http://en.pswp.cn/news/390558.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!