-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
request(middleware): Use log levels when logging responses #308
Comments
You mean you only want to log errors? If you change https://github.com/jrobichaud/django-structlog/blob/master/config/settings/local.py#L141 I might not understand what your are trying to achieve. |
Hmm, no I'd like the request middleware to use the log level functions that align with each status codes' severity to be used.
Preferred: if response.status_code >= 500:
level = "error"
elif response.status_code >= 400:
level = "warning"
else:
level = "info"
getattr(logger, level)(
"request_finished",
*args,
extra={
"code": response.status_code,
"request": self.format_request(request),
},
) This would ensure that the method used would be If you take a look at structlogs stdlib they use method name to add log levels or filter: https://github.com/hynek/structlog/blob/main/src/structlog/_log_levels.py#L51-L72 This allows me to adjust the log level and actually drop logs that are TLDR; my request is to only log 4xx and 5xx responses :D and adjusting |
I get it now. You want to change the log level based on the response status. I'll have to look into it. |
I agree if response.status_code >= 500:
level = "error"
elif response.status_code >= 400:
level = "warning"
else:
level = "info" is opinionated but I suppose for a library named I am considering using |
@adinhodovic, please see #309 |
Yes exact! Tried it locally, worked well. Thanks for the quick response and implementation! |
@adinhodovic, I am not ready to release 6.0.0 yet. Will it be ok with you if I make a pre release version? |
Yes, works fine with me! Thank you. |
You can install this version explicitely: pip install django-structlog==6.0.0.dev1 |
Did, thanks! |
Hello @jrobichaud and @adinhodovic , due to these changes I would like to ask, how we can suppress the Also when exception happens, it is propagated to Sentry as unhandled and then 500 code returned to the client. With these changes it means it will create the The question is, what is the best way to "suppress" this new behavior? Should I listen to the event of request failed and DO something there with context or similar? Best regards, Sergei |
sentry-structlog seems to have a |
@jrobichaud Let me try to check and confirm. If it will resolve, I will post a snippet here if someone needed it also. |
Would be nice if error levels were used when logging responses. Similar to Django's request handler:
https://github.com/django/django/blob/main/django/utils/log.py#L233-L249
Would simplify integration with structlog's stdlibs
filter_by_level
that filters based on method name andadd_log_level
that adds the log level. I currently add my processor for adding status_code -> level. But having issues with a custom filter to exclude log levels based onlevel
instead ofmethod_name
since I think the python logging framework drops my log either way based on themethod_name
when setting logging level to warning for example.Opinionated request, understandable if it does not go through.
The text was updated successfully, but these errors were encountered: