Python Forum

Full Version: Error Handling
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
(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?
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?
(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
(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