Last active
April 24, 2017 13:49
-
-
Save tonywangcn/6044e68b524423815bda92951f29a1da to your computer and use it in GitHub Desktop.
rewrite logger handler to add some features
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
import logging | |
import threading | |
import time | |
import logging.handlers | |
import os | |
class OneLineExceptionFormatter(logging.Formatter): | |
def formatException(self, exc_info): | |
result = super(OneLineExceptionFormatter, | |
self).formatException(exc_info) | |
return repr(result) # or format into one line however you want to | |
def format(self, record): | |
s = super(OneLineExceptionFormatter, self).format(record) | |
if record.exc_text: | |
s = s.replace('\n', '') + '|' | |
# print s,type(s) | |
return s | |
def handler_configure(**kwargs): | |
http_handler = logging.handlers.HTTPHandler( | |
'ip:port', #log server ip and flask app port | |
'/log', | |
method='POST', | |
) | |
fh = logging.StreamHandler() | |
f = OneLineExceptionFormatter( | |
'%(asctime)s - "%(pathname)s" - %(module)s\ | |
- %(lineno)d - %(process)d - (%(threadName)-10s)\ | |
- %(levelname)s - %(message)s') | |
fh.setFormatter(f) | |
root = logging.getLogger() | |
root.setLevel(logging.DEBUG) | |
root.addHandler(fh) | |
root.addHandler(http_handler) | |
if kwargs: | |
root = logging.LoggerAdapter(root, kwargs) | |
return root |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment