Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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)
Add Comment
Please, Sign In to add comment