Created
June 23, 2018 01:40
-
-
Save meteorfox/a26d569986f7d70581dce8e8fe2b1635 to your computer and use it in GitHub Desktop.
Usage:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import subprocess | |
import tempfile | |
import sys | |
# Set-up logging to both console and a file log. | |
logger = logging.getLogger() | |
logger.handlers = [] | |
logger.setLevel(logging.DEBUG) | |
# Console logging will be only INFO or higher | |
# This could be configured for CLI invocations | |
console_handler = logging.StreamHandler() | |
console_handler.setLevel(logging.INFO) | |
console_handler.setFormatter( | |
logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') | |
) | |
logger.addHandler(console_handler) | |
# Adds log file handler. Everything DEBUG or higher is logged here | |
# You could potentially have 1 log per benchpress invocation | |
file_handler = logging.FileHandler(filename='benchpress.log') | |
file_handler.setLevel(logging.DEBUG) | |
file_handler.setFormatter( | |
logging.Formatter(('%(asctime)s ' | |
'%(filename)s:%(lineno)d %(levelname)-8s %(message)s')) | |
) | |
logger.addHandler(file_handler) | |
# If you want to keep per-command log files, instead | |
# of using temp files, you could give them a proper name and path. | |
with tempfile.TemporaryFile() as temp_stdout, \ | |
tempfile.TemporaryFile() as temp_stderr: | |
cmd = sys.argv[1:] # bad practice, only use for cmd | |
process = subprocess.Popen(cmd, | |
stdin=subprocess.PIPE, | |
stdout=temp_stdout, | |
stderr=temp_stderr) | |
process.wait() | |
temp_stdout.seek(0) | |
stdout = temp_stdout.read().decode('utf8', 'ignore') | |
temp_stderr.seek(0) | |
stderr = temp_stderr.read().decode('utf8', 'ignore') | |
status_code = process.returncode | |
output = '\nstdout:\n{}\nstderr:\n{}\nstatus_code: {}' | |
# Imaging this is the parser call | |
msg = output.format(stdout, stderr, status_code) | |
# Here we log to log file only | |
logging.debug(msg) | |
# Here we log to console and log file | |
logging.info('cmd: {}\n'.format(cmd) + msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment