Python Forum
How to capture an error message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to capture an error message
#5
(Feb-15-2020, 02:54 AM)TheHacker707 Wrote: so that I can save it to a log or something. Is there a way to do that?
Can do it with logging module,look at this post.
In that post i use logging in standard library,you see i have to write boiler plate code and i import it with import my_log.

There is a easier way if using eg Loguru.
No boiler plate code,if i use same function as in post.
from loguru import logger

logger.add("output.log", backtrace=True, diagnose=True)

def add(x, y):
   try:
       return(x + y)
   except Exception as error:
       logger.exception('msg')

if __name__ == '__main__':
    add(5, '50')
Output:
λ cat output.log 2020-02-15 15:42:45.104 | ERROR | __main__:add:8 - msg Traceback (most recent call last): File "add_guru.py", line 11, in <module> add(5, '50') -> <function add at 0x033308A0> > File "add_guru.py", line 6, in add return(x + y) | -> '50' -> 5 TypeError: unsupported operand type(s) for +: 'int' and 'str'
Loguru can also catch error even if not using try:except,bye using a decorator.
from loguru import logger

logger.add("file_1.log", rotation="5 MB")

@logger.catch
def add(x, y):
    return(x + y)

if __name__ == '__main__':
    add(5, '99')
Output:
λ cat file_1.log 2020-02-15 15:59:35.736 | ERROR | __main__:<module>:12 - An error has been caught in function '<module>', process 'MainProcess' (13600), thread 'MainThread' (10060): Traceback (most recent call last): > File "catch_guru.py", line 12, in <module> add(5, '99') -> <function add at 0x0320AF60> File "catch_guru.py", line 9, in add return(x + y) | -> '99' -> 5 TypeError: unsupported operand type(s) for +: 'int' and 'str'
Reply


Messages In This Thread
How to capture an error message - by TheHacker707 - Feb-14-2020, 01:00 AM
RE: How to capture an error message - by Larz60+ - Feb-14-2020, 02:39 AM
RE: How to capture an error message - by ibreeden - Feb-15-2020, 11:26 AM
RE: How to capture an error message - by snippsat - Feb-15-2020, 03:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 1,043 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Another Error message. the_jl_zone 2 995 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,177 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Screen capture opencv - grab error Kalman15 1 1,627 Jan-27-2022, 12:22 PM
Last Post: buran
  understanding error message krlosbatist 1 1,924 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 43,202 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 3,373 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,383 Feb-18-2021, 07:31 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 3,012 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  Don't understand error message Milfredo 2 2,065 Aug-24-2020, 05:00 PM
Last Post: Milfredo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020