Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log my process id using logging module

How to log my process my module using python. My script will get Pid and process.

I want to log the output

import psutil
[p.info for p in psutil.process_iter(attrs=['pid', 'name']) if 'firefox' in p.info['name']]

Output > [{'pid': 413, 'name': 'firefox'}]

import logging
logging.basicConfig(level=logging.INFO)
logging.info()

1 Answers

I think there is a simpler solution: the logging module can automatically display the Process ID.

FORMAT = '%(process)d %(message)s'
logging.basicConfig(format=FORMAT)

see docs: https://docs.python.org/3/library/logging.html#logrecord-attributes

like image 138
Edward Gaere Avatar answered May 09 '26 20:05

Edward Gaere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!