Skip to content

Instantly share code, notes, and snippets.

@meteorfox
Created June 23, 2018 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meteorfox/d655d6595b2065d03fce2e70061645d0 to your computer and use it in GitHub Desktop.
Save meteorfox/d655d6595b2065d03fce2e70061645d0 to your computer and use it in GitHub Desktop.
Usage:
#!/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