[英]How to save log file using Python?
I have a powershell script and I save the log using this way我有一个 powershell 脚本,我使用这种方式保存日志
$Log = Start-Transcript -Path $Log_Path -Force
How do I use it in Python?我如何在 Python 中使用它?
You can write/save the log file using the below command.您可以使用以下命令写入/保存日志文件。
logging.basicConfig(filename='path to the log file', level=...)
There is a filemode option to overwrite the file.有一个文件模式选项可以覆盖文件。 To overwrite you can specifie filemode:w
要覆盖,您可以指定 filemode:w
logging.basicConfig(filename='logs.log',
filemode='w',
level=logging.INFO)
filemode: Specifies the mode to open the file, if filename is specified (if filemode is unspecified, it defaults to 'a'). filemode:指定打开文件的模式,如果指定了文件名(如果未指定文件模式,则默认为'a')。
There is a logging module in python. python中有一个日志模块。 You can import an use that.
你可以导入一个使用。
import logging logging.basicConfig(level=logging.DEBUG) logging.debug('This will get logged')
And the output will be:输出将是:
DEBUG:root:This will get logged
Similarly, you can use the other log levels too.同样,您也可以使用其他日志级别。 You can find more about it here
你可以在这里找到更多关于它的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.