0

I have tried to implement the logging function. I have successfully used datetime in my logging function but I cannot implement the function name and the line number in my code. Here is my code-

def error_log(error="Log file", filename="logs.csv"):
    now = datetime.now()
    logging.basicConfig(format='[%(filename)s:%(lineno)d]',
                        datefmt='%Y-%m-%d:%H:%M:%S',
                        level=logging.DEBUG)
    with open(filename, 'a+') as f:
        f.write(now.strftime("%m/%d/%Y, %H:%M:%S") + " ----> " + error + '\n')

Also I have attached my log file output for better understanding.

05/25/2022, 10:20:56 ----> not enough values to unpack (expected 2, got 1)
05/25/2022, 10:20:56 ----> not enough values to unpack (expected 2, got 1)

I want to implement the logging file like this--

05/25/2022, 10:20:56, function name , line number ----> not enough values to unpack (expected 2, got 1)
05/25/2022, 10:20:56, function name , line number ----> not enough values to unpack (expected 2, got 1)
ewokx
  • 2,204
  • 3
  • 14
  • 27
  • Welcome to Stack Overflow. You could take a look at the ```traceback``` module. i.e. https://docs.python.org/3/library/traceback.html – ewokx May 25 '22 at 05:11
  • Does this question "[When I catch an exception, how do I get the type, file, and line number?](https://stackoverflow.com/questions/1278705/when-i-catch-an-exception-how-do-i-get-the-type-file-and-line-number)" help? – acw1668 May 25 '22 at 05:23
  • Thank you for you response. Kind of similar but not the same format, which I want to implement. – Shafayat Mugdha May 25 '22 at 05:35

0 Answers0