0

I have looked solutions for my query but couldn't find related to my answer.

Below is my code:

#test.py

def log_setup():
    stream = io.StringIO()
    #logging.handlers.BufferingHandler(capacity=float("inf"))
    logging.basicConfig(stream=stream,
                    level=logging.NOTSET, 
                    format='%(asctime)s.%(msecs)03d %(levelname)-8s [%(module)s] - %(message)s')


    logging.warning('This is serious')



    print("-------------->>>>", stream.getvalue())

    #output = -------------->>>> 2022-11-25 17:44:54,014.014 WARNING  [test] - This is serious 

above code works fine !

So, what I want?

Im using this log_setup() function in another files as below:

#newfile.py

from test import log_setup

log_setup()

def file1_function():
    logging.error("heloo")
    a = 5
    b = 0

    try:
        c = a / b
    except Exception as e:
        logging.error("Exception occurred", exc_info=True)


 file1_function()

I also want to add the logg error of file1_function to stream.getvalue()

Expected output:

    -------------->>>> 2022-11-25 17:44:54,014.014 WARNING  [test] - This is serious 
    -------------->>>> exception error from file1_function()
vvvvv
  • 25,404
  • 19
  • 49
  • 81
VJ P
  • 45
  • 5

0 Answers0