Python 3.5 streaming output to the same log file that I write to - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python 3.5 streaming output to the same log file that I write to (/thread-17776.html) |
Python 3.5 streaming output to the same log file that I write to - cbj0517 - Apr-23-2019 I am new to python, and am trying to work out a logging issue. I am trying to stream stdoout, as well as stderr, to the log file that I also write entries to when using log... Also, I am getting duplicate entries on the console, but only one entry in the actual log file when I log entries. import os import datetime from subprocess import call import sys import logging currDir = os.path.dirname(os.path.abspath(__file__)) today_key = datetime.datetime.now().strftime("%Y%m%d%H%M") log_file = os.path.join(currDir, "logs", 'stream_'+today_key)+".log" log_formatter = logging.Formatter('%(asctime)s — %(name)s — %(levelname)s — %(funcName)s:%(lineno)d — %(message)s') pStart = datetime.datetime.now() def get_stdout_handler(): stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(log_formatter) return stdout_handler def get_stderr_handler(): stderr_handler = logging.StreamHandler(sys.stderr) stderr_handler.setFormatter(log_formatter) return stderr_handler def get_file_handler(): file_handler = logging.FileHandler(log_file, 'a') file_handler.setFormatter(log_formatter) return file_handler def get_logger(logger_name): log = logging.getLogger(logger_name) log.setLevel(logging.DEBUG) log.addHandler(get_stdout_handler()) log.addHandler(get_stderr_handler()) log.addHandler(get_file_handler()) log.propagate = False log.hasHandlers = True return log def restore_db(): call(["ping", "google.com"]) logger = get_logger(__name__) if __name__ == '__main__': logger.info('-----------------------------------') logger.info('process start') restore_db() logger.info('process end') logger.info('total processing time (%s)',(datetime.datetime.now() - pStart)) logger.info('-----------------------------------')The stream from the ping doesn't appear at all in the log file. Any help would be greatly appreciated. RE: Python 3.5 streaming output to the same log file that I write to - snippsat - Apr-23-2019 You most use a subprocess method that capture stdout or stderr call() dos not that.Also have logging boilerplate code separate,then it look much cleaner. import subprocess import my_log out = subprocess.run(['ping', 'google.com'], capture_output=True) my_log.logger.info('Start') output = out.stdout.decode() my_log.logger.debug(output) my_log.logger.info('Finish')Now in file logg.log
import my_log code:RE: Python 3.5 streaming output to the same log file that I write to - cbj0517 - Apr-23-2019 Thank you snippsat |