Cover All Exceptions: Python Error Logging

  • Python
  • Thread starter cpscdave
  • Start date
In summary, the conversation discusses the deployment of a large program on a headless server and the concern about potential errors that may have been missed. The possibility of using Python to handle exceptions and write error logs is mentioned, as well as the idea of creating a program to automatically log and reboot the server with any errors. The conversation ends with a humorous mention of adding GOTO statements and not using comments in the code.
  • #1
cpscdave
403
120
I feel a bit dirty asking this..
But I've written a fairly large program that is going to be deployed in a few weeks. Its a headless server. I've done error checking throughout the code where I am aware of it, but I'm worried about the things I have missed.

Is there a way in python I can get it to continue on all exceptions? Perhaps write out an error log as well?? I highly doubt there is but thought I'd check :)
 
Technology news on Phys.org
  • #2
Can you predict which exceptions are likely to be thrown? If so, maybe something like this is feasible:
Python:
try:
   <some code that might throw>
except (RuntimeError, TypeError, NameError):
   <handle error>
finally:
   <clean up>
 
  • Like
Likes cpscdave
  • #3
You could double down on that slightly dirty feeling and write a program with an infinite loop that launches your server, captures stderr and diverts it to a logfile. In other words, an automatic log-it-and-reboot.
 
  • Like
Likes cpscdave
  • #4
Ibix said:
You could double down on that slightly dirty feeling and write a program with an infinite loop that launches your server, captures stderr and diverts it to a logfile. In other words, an automatic log-it-and-reboot.

Haha that might actually not be a bad idea.
This is the classic case of I want a few weeks to test, and I have a few days :P

I'm going to add some GOTO's just to make my fall from grace complete.
And who needs comments in the code??
 

Related to Cover All Exceptions: Python Error Logging

1. What is error logging in Python?

Error logging in Python refers to the process of recording and tracking errors that occur during the execution of a Python program. This includes capturing information about the error, such as the type of error and the location in the code where it occurred, and writing it to a log file for later analysis.

2. Why is error logging important in Python?

Error logging is important in Python because it allows developers to identify and troubleshoot issues in their code more efficiently. By logging errors, developers can track down the source of the problem and make necessary changes to fix it, leading to more reliable and robust code.

3. How do you enable error logging in Python?

To enable error logging in Python, you can use the built-in logging module. This module provides functions and classes for logging messages to a file or other output stream. You can configure the logging level and format, as well as specify where the log file should be saved.

4. What is the difference between logging and printing in Python?

The main difference between logging and printing in Python is the purpose and usage. Printing is used for displaying messages to the console for debugging or informational purposes. Logging, on the other hand, is used for recording and tracking errors and other important events that occur during program execution.

5. Can error logging be disabled in Python?

Yes, error logging can be disabled in Python by setting the logging level to a higher value, such as CRITICAL or OFF. This will prevent any logs from being written, but it is not recommended as it can make it difficult to debug and troubleshoot issues in the code. It is better to properly configure the logging level and format to suit your needs instead of disabling it entirely.

Similar threads

  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
3
Views
903
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
11K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
943
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top