如何获取PID并将其导出到日志文件

时间:2019-01-06 18:36:04

标签: python-2.7 logging pid

im尝试记录使用此代码运行的进程。 我可以打印出来,但不确定如何将其输入到日志文件中

iv查看了示例代码,并获得了一些要记录的代码,但其中包含一些静态变量。 如果我删除logging.debug并打印出来,它将

import logging
import win32com.client
logging.basicConfig(
    filename="test1.log",
    level=logging.DEBUG,
    format="%(asctime)s:%(levelname)s:%(message)s"
    )
wmi=win32com.client.GetObject('winmgmts:')
for p in wmi.InstancesOf('win32_process'):
    logging.debug ("p.Name", p.Properties_('ProcessId')), \
        int(p.Properties_('UserModeTime').Value)+int(p.Properties_('KernelModeTime').Value)
    children=wmi.ExecQuery('Select * from win32_process where     ParentProcessId=%s' %p.Properties_('ProcessId'))

在所有帮助下预先感谢您 我希望为PID加上时间戳

1 个答案:

答案 0 :(得分:0)

您应该看看官方的docs

如果您在执行.info,.debug等之前定义了日志记录配置

logging.basicConfig(filename='example.log', level=logging.DEBUG)

它将记录到文件。

记录变量应如下所示:

logging.warning('%s before you %s', 'Look', 'leap!')

在您的情况下,对于字符串使用%s,对于整数使用%d