Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Handling
#1
Hey Everyone,

Does anyone have any good ideas for Error Logging?

Obviously, Try-Except will be used. but I am curious how you guys handle the actual logging.

Right now, I have all the logging stored in a file named after the date. then the error is appended to it with the time.

Here is an example:
File Name: Thu-Oct-15-2020.txt
20:15:43: 
CCS811 Sensor Needs to be recalibrated

20:15:59: 
CCS811 Sensor Needs to be recalibrated

20:16:15: 
CCS811 Sensor Needs to be recalibratedp
I am running into two problems:

one: as you can see, if an error occurs, but isn't "program ending", it will inform me of the error every time it happens. which means I end up with the same being logged, multiple times. I'd like a way for the program to look up if its happened before, and simply ignore it if it's already logged. Or at least, just append the time. The only way I can think about doing this is with Mysql, or another DB. Anyone have any ideas?

two: When the error details show up in the terminal, it gives me all the information. Including what function was called when the error occurred, and in what file. When I transfer it to the file, it's just the error. the code format I use is:

Try:
  code to be done
except Exceptions as e:
  appendtotextfilefunction(e)
I am assuming there is methods within e that need to be called to do it?
Reply
#2
Are you using the logging module? Calling logging.exception("message") from within the except block should generate the traceback information in the log for you.
Reply
#3
(Oct-16-2020, 11:33 PM)bowlofred Wrote: Are you using the logging module? Calling logging.exception("message") from within the except block should generate the traceback information in the log for you.

I'm new to Python. I didn't know about the logging module. is it standard, or do I have to install it?
Reply
#4
So I looked up the logging module. It looks help full, but is there an advantage of using this over Try-Except"?

Also, can this be set to ignore multiples of the same error?
Reply
#5
(Oct-16-2020, 11:26 PM)JarredAwesome Wrote: one: as you can see, if an error occurs, but isn't "program ending", it will inform me of the error every time it happens. which means I end up with the same being logged, multiple times. I'd like a way for the program to look up if its happened before, and simply ignore it if it's already logged. Or at least, just append the time. The only way I can think about doing this is with Mysql, or another DB. Anyone have any ideas?

two: When the error details show up in the terminal, it gives me all the information. Including what function was called when the error occurred, and in what file. When I transfer it to the file, it's just the error.

1)
if ERRORSTRING not in ERRORFILE:
	my_log_function(ERRORSTRING)
2) IF you are referring to the traceback you can get the traceback and add it to the error log.
https://pymotw.com/3/traceback/index.html

But again the logging module exists to not have to reinvent the wheel like you are trying to do
Recommended Tutorials:
Reply
#6
(Oct-17-2020, 12:37 AM)metulburr Wrote:
(Oct-16-2020, 11:26 PM)JarredAwesome Wrote: one: as you can see, if an error occurs, but isn't "program ending", it will inform me of the error every time it happens. which means I end up with the same being logged, multiple times. I'd like a way for the program to look up if its happened before, and simply ignore it if it's already logged. Or at least, just append the time. The only way I can think about doing this is with Mysql, or another DB. Anyone have any ideas?

two: When the error details show up in the terminal, it gives me all the information. Including what function was called when the error occurred, and in what file. When I transfer it to the file, it's just the error.

1)
if ERRORSTRING not in ERRORFILE:
	my_log_function(ERRORSTRING)
2) IF you are referring to the traceback you can get the traceback and add it to the error log.
https://pymotw.com/3/traceback/index.html

But again the logging module exists to not have to reinvent the wheel like you are trying to do

Defiantly don't want to reinvent the wheel. I am just trying to understand the tools available.

As I continue to look into Logging, it appears they are meant to be used in tandom
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,282 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,529 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 5,918 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error handling using cmd module leifeng 3 2,898 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Excpetion Handling Getting Error Number gw1500se 4 2,392 May-29-2020, 03:07 PM
Last Post: gw1500se
  Warning / Error handling in python Prarthana_12 1 5,108 Feb-08-2019, 09:21 PM
Last Post: snippsat
  Help With Error Handling jo15765 6 4,127 Sep-14-2018, 06:27 PM
Last Post: jo15765
  Error Handling/No results from SQL Query JP_ROMANO 7 9,483 Jul-18-2018, 02:31 PM
Last Post: JP_ROMANO
  Error handling parthi1705 0 2,010 Jun-13-2018, 11:57 AM
Last Post: parthi1705
  Error Handling is weird PythonAndArduino 1 3,003 Nov-09-2017, 05:08 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

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