Comments (3)
Thanks for the detailed bug report. You're right, the behaviour is not optimal and I will improve it. I will fix it in wsgi-request-logger but you can work-around this issue by setting the "Content-Length" header in your application. If you call send_file()
with a file name instead of a file-like object, this is done automatically. If you really need to provide send_file()
with a file-like object, you can also set the content_length yourself (and you should, if there's any way of knowing it the moment you create the stream to let Browsers display an e.t.a.). In your example, this could be done like this:
@app.route('/file', methods=['GET'])
def serve_file():
f = open('data', 'rb')
sf = send_file(f, mimetype="application/ocet-stream")
sf.headers['Content-Length'] = str(os.path.getsize('data'))
return sf
Concerning the how-to-fix-it in wsgi-request-logger I'm thinking about something like this:
if content_lengths :
content_length = content_lengths[0]
elif all(hasattr(retval, attr) for attr in ('seek', 'tell')):
retval.seek(0, 2)
content_length = retval.tell()
retval.seek(0)
elif hasattr(retval, 'file') and hasattr(retval.file, 'seek') and hasattr(retval.file, 'tell'):
retval.file.seek(0, 2)
content_length = retval.file.tell()
retval.file.seek(0)
else:
content_length = len(b''.join(retval))
Not optimal yet... If you're really streaming content where you don't know the content size at time when you start serving it (e.g. live content such as mjpeg frames from a web cam), the seek(0,2)
will most probably block. So what should the log-file contain for the file size in those cases? I'm not sure.
Let me know what you think and thanks again for reporting the issue.
from wsgi-request-logger.
the alternative is to delay the log until response close, you have to do some counting during the response,
I just hit by this now I am searching an alternative.
from wsgi-request-logger.
simple POC
def __call__(self, environ, start_response):
start = clock()
status_codes = []
content_lengths = []
def custom_start_response(status, response_headers, exc_info=None):
status_codes.append(int(status.partition(' ')[0]))
for name, value in response_headers:
if name.lower() == 'content-length':
content_lengths.append(int(value))
break
return start_response(status, response_headers, exc_info)
retval = self.application(environ, custom_start_response)
try:
counter = 0
for i in retval:
yield i
counter += len(i)
finally:
runtime = int((clock() - start) * 10**6)
content_length = content_lengths[0] if content_lengths else counter
msg = self.formatter(status_codes[0], environ, content_length, ip_header=self.ip_header, rt_us=runtime)
self.logger.info(msg)
from wsgi-request-logger.
Related Issues (12)
- ZERO not defined in timehacks HOT 2
- wsgi-request-logger fails to pip install if pypandoc is already installed HOT 1
- Keep a CHANGELOG // Upgrade Gracefully
- REQUEST_URI instead of PATH_INFO HOT 1
- Escaping for user agent and referer
- NOT PRODUCTION READY - This project makes the application respond with empty responses on some cases
- Py3 Support
- Request time accuracy to low HOT 5
- Using 'yield' in bottle gets an exception
- allow to set self.logger.propagate=False HOT 3
- HTTP 5xx responses not logged to access log HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wsgi-request-logger.