current position:Home>Python logging log error and exception exception callback method
Python logging log error and exception exception callback method
2022-02-01 03:58:25 【ilyq69】
demand
- stay Python Handle the error level log output or program exceptions , Such as : Log alert , Abnormal notice
Realization
logging record
- By rewriting logging.Logger.callHandlers This method can do some other processing when printing the log
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
old_callHandlers = logging.Logger.callHandlers # Copy the original callHandlers
def callHandlers(self, record):
try:
return old_callHandlers(self, record)
finally:
# Custom business logic can be added here , Each time printing log Will come in
if record.levelno >= logging.ERROR: # Greater than or equal to ERROR Levels of log Just output
logger.info("callHandlers: %s", record.message)
# Replace the original callHandlers Method
logging.Logger.callHandlers = callHandlers
logger.info("info log")
logger.error("error log")
logger.error("error log001")
# Output
INFO:__main__:info log
ERROR:__main__:error log
INFO:__main__:callHandlers: error log
ERROR:__main__:error log001
INFO:__main__:callHandlers: error log001
Copy code
exception handling
- Realization sys.excepthook Method , If it is a thread, you need to implement threading.excepthook
import sys
import logging
def callback_func(exc_type, exc_value, exc_traceback):
logging.error("callback_func>>>>>>>>>>>>>>", exc_info=(
exc_type, exc_value, exc_traceback))
sys.excepthook = callback_func
1 / 0
# Output
ERROR:root:callback_func>>>>>>>>>>>>>>
Traceback (most recent call last):
File "C:/Users/dong/PycharmProjects/err-callback-py/main.py", line 14, in <module>
1 / 0
ZeroDivisionError: division by zero
Copy code
Last
- Recommend an encapsulated Library err-callback-py, In that way, you can write your own business logic directly
copyright notice
author[ilyq69],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010358236523.html
The sidebar is recommended
- Python avatar animation, come and generate your own animation avatar
- leetcode 1884. Egg Drop With 2 Eggs and N Floors(python)
- leetcode 1910. Remove All Occurrences of a Substring(python)
- Python and binary
- First acquaintance with Python class
- [Python data collection] scrapy book acquisition and coding analysis
- Python crawler from introduction to mastery (IV) extracting information from web pages
- Python crawler from entry to mastery (III) implementation of simple crawler
- The apscheduler module in Python implements scheduled tasks
- 1379. Find the same node in the cloned binary tree (Java / C + + / Python)
guess what you like
-
Python connects redis, singleton and thread pool, and resolves problems encountered
-
Python from 0 to 1 (day 11) - Python data application 1
-
Python bisect module
-
Python + OpenGL realizes real-time interactive writing on blocks with B-spline curves
-
Use the properties of Python VTK implicit functions to select and cut data
-
Learn these 10000 passages and become a humorous person in the IT workplace. Python crawler lessons 8-9
-
leetcode 986. Interval List Intersections(python)
-
leetcode 1860. Incremental Memory Leak(python)
-
How to teach yourself Python? How long will it take?
-
Python Matplotlib drawing pie chart
Random recommended
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
- Daily python, Chapter 18, class
- "I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
- Django view
- Python implements filtering emoticons in text
- When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
- Python crawler - get fund change information
- Highlight actor using Python VTK
- Python crawler actual combat: crawling southern weekend news articles
- leetcode 406. Queue Reconstruction by Height(python)
- leetcode 1043. Partition Array for Maximum Sum (python)
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
- Python series tutorial 127 -- Reference vs copy
- Control flow in Python: break and continue
- Teach you how to extract tables in PDF with Python
- leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(python)
- leetcode 1338. Reduce Array Size to The Half(python)
- Object oriented and exception handling in Python
- How to configure load balancing for Django service
- How to embed Python in go
- Python Matplotlib drawing graphics
- Python object-oriented programming 05: concluding summary of classes and objects