0

The goal here is to catch error messages or any output generated in the console. Python error logging module grabs logs which is specified specifically using logging level. example :DEBUG,INFO or WARNING etc. I cannot use try except because the errors can happen anywhere inside the program. Not in one particular location.

I want to capture the console output. Is there a function or something which can do this.

So far I am able to log specific exceptions that I detail using try except method. I want to move away from logging specific methods.

import logging 
logging.basicConfig(filename='log.txt',level=logging.WARNING,format='%(asctime)s:%(levelname)s:%(message)s')

a = input('Enter first number') 
b = input('Enter second number')

try:
    a= int(a)
    b=int(b)
    print('Addtion value',a+b) 
except:
    logging.warning("Addition caused an error")   

Scenario 1: If we enter a character 'A' as an input the error we get in the console is


ValueError Traceback (most recent call last) in () ----> 1 int('a')

ValueError: invalid literal for int() with base 10: 'a'

The goal is to capture this error as the output into the log file. This specific error is value error but it could be any error and those errors should be captured.

Scenario 2: If the digits are entered correctly for A and B then the log file should contain the sum of a and b.

Thanks in advance for the help.

Krish Kk
  • 21
  • 6

0 Answers0