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.mzph.cn/news/390558.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!