python - 如何使用 python 日志记录捕获命令行输出?
问题描述
我正在编写一个脚本来匿名化元数据,并且我正在设置一个日志系统来记录脚本在每次收到主题时是如何执行的。该脚本从在线服务器推送和提取信息,并使用“curl”shell 命令来执行此操作。当我执行脚本时,我在 shell 中得到一个输出,我想将它记录在日志文件中,但我不知道如何捕获它。
目前我将记录器配置为:
import logging
logging.basicConfig(filename='/path/to/logging_test.txt',filemode='a',format='%(asctime)s - %(levelname)s - %(message)s',level=logging.DEBUG)
logger = logging.getLogger(__name__)
然后我在从服务器中提取元数据后执行命令来修改元数据,
os.system(cmd)
哪个输出到 shell 中,如下所示:
{
"ID" : "dshbjhdoqwdjwebfie",
"Path" : "/subjects/dshbjhdoqwdjwebfie",
"Type" : "Subject"
}
我想在日志文本文件中包含它。我该怎么做呢?
解决方案
推荐阅读
- amazon-web-services - AWS - 将 TTL 删除的数据从 DynamoDB 存档到 S3
- python - 如何使用 Python 检查 SMTP 服务器是否处于活动状态?
- amazon-web-services - 调用 GetKeyPolicy 操作时出现错误消息 ValidationException
- reactjs - useEffect 中的排序方法不会更新我状态下的数组
- html - Bootstrap:如何避免汉堡菜单破坏导航和标志?
- powershell - 使用 Powershell 将 Azure Devops 秘密变量作为纯文本获取
- javascript - 未捕获的类型错误:无法读取 null Javascipt 的属性“classList”
- svg - 如何使用这个 UTF-8 SVG 字符串来获取
- python - 使用 docker + django + nginx 时不提供静态文件
- sql - 通过变量从数据集中计算统计数据