Created
June 22, 2018 12:56
-
-
Save yashk/1bd9f9317346239b171f3ca53781c07b to your computer and use it in GitHub Desktop.
python subprocess logging
This file contains 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
import sys | |
import subprocess | |
import logging | |
import shlex | |
from logging.handlers import RotatingFileHandler | |
def log_handler(log_file): | |
log_handler = RotatingFileHandler(filename=log_file,maxBytes=200000, | |
backupCount=5) | |
formatter = logging.Formatter('%(asctime)s %(message)s','%m/%d/%Y %I:%M:%S %p') | |
log_handler.setFormatter(formatter) | |
log_handler.setLevel(logging.INFO) | |
logger.addHandler(log_handler) | |
logger.setLevel(logging.INFO) | |
log_file="/tmp/py.log" | |
logger = logging.getLogger("logger") | |
log_handler(log_file) | |
print("INFO Log file path is: " + log_file) | |
hive_command="hive -e 'show databases;'" | |
proc=subprocess.Popen(shlex.split(hive_command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
for line in proc.stdout: | |
logger.info(line) | |
proc.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment