Home > Software design >  How to log every error in file you get in cmd
How to log every error in file you get in cmd

Time:12-09

I tried try catch block in my python script but my team lead wants to log every error which can occure i cannot write try catch every line do anyone know better way to do this? currently im using try catch and logger module for logging certain comparisions she says she wants to log everytime error happens

only getting log of try catch block

CodePudding user response:

If all you need to do is log when an error happens, then I'd just wrap the entire script (or at least the part that is going to produce the error) in a single try-catch block that simply writes to a file whenever an error occurs.

try:
    # all your code here
except:
    with open("log.txt", "a") as fp:
        fp.write("error")
        fp.write(data)
  • Related