actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

feat: change log level based on response status code

Open fawni opened this issue 2 years ago • 1 comments
trafficstars

PR Type

Feature

PR Checklist

  • [ ] Tests for the changes have been added / updated.
  • [ ] Documentation comments have been added / updated.
  • [x] A changelog entry has been made for the appropriate packages.
  • [x] Format code with the latest stable rustfmt.
  • [ ] (Team) Label with affected crates and semver status.

Overview

Changes the log level to Warn on client errors and redirection messages, and to Error on server errors.

The current behavior is to log everything on an Info level.

This helps logging crates customize the output. Here's an example using the femme crate:

Before:

20_08_2023-17_16_WindowsTerminal_0vZ7vIB

After:

20_08_2023-17_17_WindowsTerminal_dYRJ1Pq

It would also be colored red on a 5xx status code.

I do not know how to test or document those changes so I apologize if I should have done that.

fawni avatar Aug 20 '23 14:08 fawni

Hi @fawni,

I was thinking to do a similar thing, but instead of directly set a log level for a response type (which still do make sense), I was thinking to set the log level as parameter, and than filter the range of status to log, for the range filtering I did already a PR here : https://github.com/actix/actix-web/pull/3086

so let the user choose a what level to log request, with multiple loggers, something like:

 Logger::new("....").statuses(StatusCode::OK..StatusCode::BAD_REQUEST).level(Level::Info)
 Logger::new("....").statuses(StatusCode::BAD_REQUEST..).level(Level::Warn)

WDYT ?

tglman avatar Sep 15 '23 18:09 tglman

In middleware, any response (regardless of status code) that is sent is considered a success. Logging as INFO level is the policy of this textual logger. If you want to separately track errors, I encourage you to write a middleware to add in your stack.

Also, we're gradually transitioning to the tracing ecosystem and the whole Logger will need a re-think. Until then I'd like to keep this one relatively simple with obvious behavior.

Thank you for your ideas and contribution.

robjtede avatar Jun 11 '24 01:06 robjtede