Logging requests to django-rest-framework

后端 未结 8 739
迷失自我
迷失自我 2020-12-02 12:31

For debugging purposes, I would like to use Django\'s logging mechanism to log each and every incoming request when it \"arrives\" at django-rest-framework\'s doorstep.

8条回答
  •  感情败类
    2020-12-02 12:50

    I found for me that the best and most flexible way was to add logging via a decorator. I simply add the decorator to each of the functions (post, get) that I want to log the request from, as opposed to it being a part of the overal view class. More control over what gets logged. These decorator take the request object passed in (arg[1]) and then logs parts of the request object to a file.

    See https://github.com/slogan621/tscharts/commit/39ed479b04b7077f128774d3a203a86d6f68f03e for what amounts to a template for doing this (commit shows changes to settings.py needed to thread the logging into the file logging scheme I have in place, as well as the decorator and example usage).

提交回复
热议问题