Custom log formatter
hanzhenbjm opened this issue · 2 comments
1 Custom log formatter ERROR
class CustomRequestJSONLog(json_logging.JSONRequestLogFormatter):
def _format_log_object(self, record, request_util):
# request and response object can be extracted from record like this
request = record.request_response_data._request
response = record.request_response_data._response
json_log_object = super(CustomRequestJSONLog, self)._format_log_object(record, request_util)
json_log_object.update({
"customized_prop": "customized value",
"test": "test"
})
return json_log_object`
class CustomDefaultRequestResponseDTO(json_logging.DefaultRequestResponseDTO):
def __init__(self, request, **kwargs):
super(CustomDefaultRequestResponseDTO, self).__init__(request, **kwargs)
def on_request_complete(self, response):
super(CustomDefaultRequestResponseDTO, self).on_request_complete(response)
self.status = response.status
app = Flask(name)
json_logging.JSONRequestLogFormatter=1
json_logging.init_flask(enable_json=True)
json_logging.init_request_instrument(app, custom_formatter=CustomRequestJSONLog,
request_response_data_extractor_class=CustomDefaultRequestResponseDTO)
logger = logging.getLogger("test-logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
ERROR:Traceback (most recent call last):
File "D:\WORK\flask_test\main.py", line 24, in
class CustomDefaultRequestResponseDTO(json_logging.DefaultRequestResponseDTO):
AttributeError: module 'json_logging' has no attribute 'DefaultRequestResponseDTO'
json_logging VERSION 1.3.0
2 How to set the log path and save to file
3 How to set the save time of log file
like this
logging.handlers.TimedRotatingFileHandler(log_path, when="D", interval=1, backupCount=7)
- Can u try the latest verision? https://github.com/bobbui/json-logging-python/blob/master/example/custom_log_format_request.py still working ok for me.
- and 3. Is handled just like how we handle normal logging. Just add another handler to your logging.
To customize request logger u can use json_logging.get_request_logger() to access request logging instance
closed as assume it is resolved