1

I have a flask app with various routes setup.

I want to log messages to go to a different log file for each route.

Is this possible?

In the function that responds to the request on each route I have tried doing something like this (note this is just pseudo-code for sake of example):


@app.route('/routeA', methods=['POST'])
def routeA():
    logging.basicConfig(filename='/path/to/routeA.log'))
    ...

@app.route('/routeB', methods=['POST'])
def routeB():
    logging.basicConfig(filename='/path/to/routeB.log'))
    ...

@app.route('/routeC', methods=['POST'])
def routeC():
    logging.basicConfig(filename='/path/to/routeC.log'))
    ... 

Which I would hope means that requests to routeC go to RouteC.log and similar for A and B

BUT if after starting uwsgi I hit /routeC first this seems to set up all logs to got to routeC.log such that when I then hit routes A or B their log lines also end up in the routeC.log

Similar occurs if I hit routeB or routeA first - whichever gets the first request establishes logging to that file and the other routes don't seem to be able to override that for their requests.

Is this kind of dynamic setting log file location depending on request actually possible?

If so what am I doing wrong?

I feel like I'm missing something basic here...

CoderChris
  • 1,161
  • 2
  • 12
  • 20
  • Maybe [this SO post](https://stackoverflow.com/a/17037016/8573336) helps. – above_c_level Oct 22 '20 at 15:28
  • It helps but the issue with it for me is that it seems like I need to name the loggers independently. I have an extensive application that just uses the standard logging class throughout and is fronted by flask so I wanted to set the name/logging location within the object as required dependent on the route, not setup whole distinct loggers for each route. I could write a proxy class or function to handle switching to some named logger or extend the base logging class to facilitate this I guess but both options require a number of updates to the rest of the application which I'd like to avoid – CoderChris Oct 23 '20 at 12:36

0 Answers0