I have some Python code that runs in an infinite loop, in which I do some logging, and depending on a certain condition, run a shell command using the subprocess
module, and more logging is done afterwards. It looks approximately like this:
#!/usr/bin/env python
import logging
import subprocess
import time
TIME_FORMAT = '%a %b %-d %Y %-I:%M:%S %p'
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logformat = '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
formatter = logging.Formatter(fmt=logformat, datefmt=TIME_FORMAT)
file_handler = logging.FileHandler('/path/to/logfile.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logger.addHandler(console)
def main():
if some_condition:
return_code = subprocess.call(['./some_other_process'])
if return_code == 0:
logger.info('Just ran some_other_process')
else:
logger.debug('Sleeping for 60 seconds.')
time.sleep(60)
if __name__ == '__main__':
main()
When I first start executing this process, I notice that the output is correct; ie the log lines should look like this:
Fri Aug 22 2014 3:35:11 PM [DEBUG] __main__: Sleeping for 60 seconds.
But once the subprocess.call
is made, I noticed that all the log lines (just the ones in the console; the output to the logfile is fine) revert to the default:
DEBUG:__main__:Sleeping for 60 seconds.
Not to mention that I shouldn't be seeing debug-level logging in the console, only info and up.
Why is this happening?
I'm not sure why this is happening, but I solved a similar issue with this procedure. Apparently, the getLogger and StreamHandler routines create two different StreamHandlers.
Try with this code:
#!/usr/bin/env python
import logging
import subprocess
import time
TIME_FORMAT = '%a %b %-d %Y %-I:%M:%S %p'
logformat = '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
formatter = logging.Formatter(fmt=logformat, datefmt=TIME_FORMAT)
file_handler = logging.FileHandler('/path/to/logfile.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logging.getLogger('').addHandler(file_handler)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
def main():
if some_condition:
return_code = subprocess.call(['./some_other_process'])
if return_code == 0:
logger.info('Just ran some_other_process')
else:
logger.debug('Sleeping for 60 seconds.')
time.sleep(60)
if __name__ == '__main__':
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.