How can I capture everything from my console using python and email it?

Viewed 338

I am working on a code that runs a scheduled job and take screenshot on the chromedriver and email it. I am trying to find a way to capture the console output to a file and later email it. Below is a code snippet for the scheduler and the screenshot module.

This is my scheduler,

import schedule
import time
from datetime import datetime
from screenshot import screenshot

def job():
    print("Launching scresnshot")
    screenshot()

My screenshot code,

import time
from Screenshot import Screenshot_Clipping
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from email_it import email_it
from environmental_variables import environmental_variables
from error_alert_email import error_alert_email
from selenium import webdriver

def screenshot():

    ob=Screenshot_Clipping.Screenshot()
    
    chrome_options = Options()
    
    chrome_options.add_argument('--start-maximized')    
    chrome_options.add_argument('--start-fullscreen')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    driver = webdriver.Chrome(executable_path = r"C:\Users\me\Documents\Projects\chromedriver.exe")
    print('Confirming visibility...')
    WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.ID, visibility_element)))
    
    print('taking screenshot...')
    img_url=ob.full_Screenshot(driver, path = path, image_name = label)

    print('closing driver...')    
    driver.close()

screenshot()

MY console output

PS: C:\Users\Documents\Projects\> python .\src\scheduler.py

DevTools listening on ws://124.1.0.1:76532/devtools/browser/785b0a24-7638-4b2d-a2da-ae62a65891f6
Opening url...
[ERROR:device_event_log_impl.cc(214)] [15:07:41.359] USB: usb_device_handle_win.cc:1054 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Logging in...
Confirming visibility...
Taking screenshot...
Sending mail...
Message sent!
Closing driver...
2 Answers

One thing you could do is route all of the console output into a text file, and then email the contents of the text file once the program is done running. I'll add some code below on how to direct all print statements to a text file, and for the email sending part, there are a few good python libraries that make sending emails pretty simple. Here is a good article on the subject.

sys.stdout = open("test.txt", "w")
print("Hello World")
sys.stdout.close()

You can try this one. Its a mix of the above one plus logging!

import logging

stdout_ = sys.stdout
sys.stdout = open("logFiles.txt", "w")
e_type, e_val, e_tb = sys.exc_info()
traceback.print_exception(e_type, e_val, e_tb, file = sys.stdout)

root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)

def job():
    print("Launching scresnshot")
    screenshot()
    sys.stdout = stdout_
    sys.stdout.close()
Related