Last active
June 17, 2016 03:34
-
-
Save mcchae/d06ca623b850daf3b2a779e5b4f27939 to your computer and use it in GitHub Desktop.
python logging exception stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding=utf8 | |
########################################################################################## | |
import traceback | |
########################################################################################## | |
class A: | |
def m1(self,a,b): | |
return a/b | |
def m0(self): | |
return 0 | |
########################################################################################## | |
class B: | |
def m1(self,a,b): | |
return a/b | |
def m0(self): | |
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding=utf8 | |
########################################################################################## | |
import sys | |
import traceback | |
from datetime import datetime | |
import logging | |
from logging.handlers import RotatingFileHandler | |
g__logger = None | |
########################################################################################## | |
def get_exception_logger(logfile='./log_exception.log', maxBytes=1024, backupCount=5): | |
if g__logger is not None: | |
return g__logger | |
logger = logging.getLogger("Rotating Log") | |
logger.setLevel(logging.INFO) | |
# add a rotating handler | |
handler = RotatingFileHandler(logfile, maxBytes=maxBytes, backupCount=backupCount) | |
logger.addHandler(handler) | |
global g__logger | |
g__logger = logger | |
return g__logger | |
########################################################################################## | |
def log_exception(): | |
exc_info = sys.exc_info() | |
out = traceback.format_exception(*exc_info) | |
del exc_info | |
logger = get_exception_logger() | |
logger.info("[%s]%s\n%s" % (datetime.now(), '='*50, ''.join(out))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding=utf8 | |
########################################################################################## | |
from class_test import A, B | |
from except_log import log_exception | |
########################################################################################## | |
def main_1(): | |
a = A() | |
try: | |
print a.m0() | |
print a.m1(3,0) | |
except Exception as e: | |
log_exception() | |
print "main_1:Error <%s>" % str(e) | |
########################################################################################## | |
def main_2(): | |
a = A() | |
b = B() | |
try: | |
print b.m0() | |
print b.m1(3,a.m0()) | |
except Exception as e: | |
log_exception() | |
print "main_2:Error <%s>" % str(e) | |
########################################################################################## | |
def main(): | |
try: | |
main_1() | |
main_2() | |
except Exception as e: | |
print "main:Error <%s>" % str(e) | |
########################################################################################## | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2016-06-17 12:15:46.520199]================================================== | |
Traceback (most recent call last): | |
File "/Users/mcchae/test/exception/except_test.py", line 13, in main_1 | |
print a.m1(3,0) | |
File "/Users/mcchae/test/exception/class_test.py", line 10, in m1 | |
return a/b | |
ZeroDivisionError: integer division or modulo by zero | |
[2016-06-17 12:15:46.521788]================================================== | |
Traceback (most recent call last): | |
File "/Users/mcchae/test/exception/except_test.py", line 24, in main_2 | |
print b.m1(3,a.m0()) | |
File "/Users/mcchae/test/exception/class_test.py", line 17, in m1 | |
return a/b | |
ZeroDivisionError: integer division or modulo by zero | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment