Python Forum
Python 3.5 streaming output to the same log file that I write to
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.5 streaming output to the same log file that I write to
#1
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.
Reply
#2
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
Output:
2019-04-23 16:13:50,242 - my_log - INFO - Start 2019-04-23 16:13:50,243 - my_log - DEBUG - Pinging google.com [216.58.211.142] with 32 bytes of data: Reply from 216.58.211.142: bytes=32 time=30ms TTL=54 Reply from 216.58.211.142: bytes=32 time=39ms TTL=54 Reply from 216.58.211.142: bytes=32 time=37ms TTL=54 Reply from 216.58.211.142: bytes=32 time=46ms TTL=54 Ping statistics for 216.58.211.142: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 30ms, Maximum = 46ms, Average = 38ms 2019-04-23 16:13:50,244 - my_log - INFO - Finish
import my_log code:
Reply
#3
Thank you snippsat
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,885 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 876 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Raspberry PI - PyAudio - Streaming to Web Page ultimatecodewarrior 2 862 Dec-25-2024, 10:54 AM
Last Post: pintailscratchy
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 875 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,489 Oct-17-2024, 01:15 AM
Last Post: Winfried
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,306 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 1,534 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 4,869 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,755 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 24,407 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020