1
1
"""
2
2
Logging related functions.
3
-
4
- TODO test the except hooks more.
5
3
"""
6
4
import datetime
7
5
import logging
@@ -32,9 +30,7 @@ def log_exception(ex, more_info="", location=None):
32
30
33
31
"""
34
32
if location is None :
35
- default_loc = os .path .join (
36
- os .path .expanduser ("~" ), ".skm_python" , "caught_errors.txt"
37
- )
33
+ default_loc = get_default_log_loc ("caught_errors.txt" )
38
34
else :
39
35
default_loc = location
40
36
@@ -53,27 +49,25 @@ def log_exception(ex, more_info="", location=None):
53
49
54
50
def default_excepthook (exc_type , exc_value , exc_traceback ):
55
51
"""Any uncaught exceptions will be logged from here."""
56
- default_loc = os .path .join (
57
- os .path .expanduser ("~" ), ".skm_python" , "uncaught_errors.txt"
58
- )
52
+ default_loc = get_default_log_loc ("uncaught_errors.txt" )
59
53
60
- this_logger = logging .getLogger (__name__ )
54
+ file_logger = logging .getLogger (__name__ )
55
+ file_logger .propagate = False
61
56
handler = logging .FileHandler (default_loc )
62
- this_logger .addHandler (handler )
57
+ file_logger .addHandler (handler )
63
58
64
59
# Don't catch CTRL+C exceptions
65
60
if issubclass (exc_type , KeyboardInterrupt ):
66
61
sys .__excepthook__ (exc_type , exc_value , exc_traceback )
67
62
return
68
63
69
64
now = datetime .datetime .now ()
70
- this_logger .critical (
65
+ file_logger .critical (
71
66
"\n ----------Uncaught Exception at {}----------" .format (now ),
72
67
exc_info = (exc_type , exc_value , exc_traceback ),
73
68
)
74
69
75
- sys .stdout .write = default_write
76
- print ("A fatal error occurred in this Python program" )
70
+ print ("\n A fatal error occurred in this Python program" )
77
71
print (
78
72
"The error info was: {}" .format (
79
73
"" .join (
@@ -83,6 +77,8 @@ def default_excepthook(exc_type, exc_value, exc_traceback):
83
77
)
84
78
print ("Please report this to {} and provide the file {}" .format ("us" , default_loc ))
85
79
80
+ sys .exit (- 1 )
81
+
86
82
87
83
def override_excepthook (excepthook = None ):
88
84
"""
@@ -104,3 +100,9 @@ def override_excepthook(excepthook=None):
104
100
if excepthook is None :
105
101
excepthook = default_excepthook
106
102
sys .excepthook = excepthook
103
+
104
+
105
+ def get_default_log_loc (name ):
106
+ default_loc = os .path .join (os .path .expanduser ("~" ), ".skm_python" , name )
107
+
108
+ return default_loc
0 commit comments