PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 5, 2022

[FIXED] How to log all exceptions in python 3

 August 05, 2022     error-logging, exception, exception-handling, logging, python-3.x     No comments   

Issue

There are a lot of exception I don't want to catch, I want them to stop processing, but, I still want the stack trace in the log file. Is there a way to setup a logger so that it automatically logs all exceptions?

Right now I am writing things like this a lot:

try:
  blah
except:
  logging.exception('doing blah')
  raise

which take up a lot of time and space + it'll only log the exceptions for which I have a try clause for, not the other ones.

Thanks.


Solution

You could use decorators to wrap individual functions and methods with code that logs any uncaught exception.

This means one additional line of code in front of any function definition that should do this logging. Advantage is: Can be quickly added and removed.

The decorator also could get arguments. For example you could specify the base class of those exceptions that you want to log.

Here's a basic question & concise answer on how to write decorators.



Answered By - cfi
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing