Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mrsarm/rust-actix-contrib-logger
Drop-in replacement for the Actix Web HTTP Logger middleware
https://github.com/mrsarm/rust-actix-contrib-logger
actix-web log logger logging rust
Last synced: about 1 month ago
JSON representation
Drop-in replacement for the Actix Web HTTP Logger middleware
- Host: GitHub
- URL: https://github.com/mrsarm/rust-actix-contrib-logger
- Owner: mrsarm
- License: apache-2.0
- Created: 2023-08-13T05:13:54.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-07T11:56:21.000Z (6 months ago)
- Last Synced: 2024-12-24T06:12:16.591Z (about 2 months ago)
- Topics: actix-web, log, logger, logging, rust
- Language: Rust
- Homepage: https://crates.io/crates/actix-contrib-logger
- Size: 27.3 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
actix-contrib-logger
====================Logger middleware for the [Actix Web framework](https://actix.rs/).
Actually it's a _copy & paste_ from the official [Logger](https://actix.rs/docs/middleware/#logging)
middleware (original [source code](https://github.com/actix/actix-web/blob/master/actix-web/src/middleware/logger.rs)),
but it allows to choose the logging level depending on the HTTP status code responded,
(see [`Logger::custom_level()`](https://docs.rs/actix-contrib-logger/latest/actix_contrib_logger/middleware/struct.Logger.html#method.custom_level)
and [`Logger::custom_error_resp_level()`](https://docs.rs/actix-contrib-logger/latest/actix_contrib_logger/middleware/struct.Logger.html#method.custom_error_resp_level))
and by default server errors are logged with `ERROR` level.Moreover, error in response logs are also configurable, and by default logged as `ERROR`
for server side failures.The Logger middleware uses the standard log crate to log information. You should enable logger for
the `http_logger` to see access logs ([`env_logger`](https://docs.rs/env_logger) or similar).### Examples
In the following example `ERROR` level is used for server errors, `WARN` for
HTTP 404 responses (_Not Found_), and for the rest `INFO` level:```rust
use actix_contrib_logger::middleware::Logger;
use env_logger::Env;
use http::StatusCode;
use log::Level;#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};env_logger::init_from_env(Env::default().default_filter_or("info"));
HttpServer::new(|| {
let logger = Logger::default()
.custom_level(|status| {
if status.is_server_error() {
Level::Error
} else if status == StatusCode::NOT_FOUND {
Level::Warn
} else {
Level::Info
}
});
App::new().wrap(logger)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
```Requests logs will look like:
```
[2023-08-13T07:28:00Z INFO http_logger] 127.0.0.1 "GET / HTTP/1.1" 200 802 "-" "Mozilla/5.0 ..." 0.001985
[2023-08-13T07:29:10Z ERROR http_logger] 127.0.0.1 "POST /users HTTP/1.1" 500 86 "-" "curl/7.68.0" 0.002023
[2023-08-13T07:29:10Z WARN http_logger] 127.0.0.1 "PUT /users HTTP/1.1" 404 55 "-" "HTTPie/3.2.1" 0.002023
```Using the method [`logger.custom_error_resp_level()`](https://docs.rs/actix-contrib-logger/latest/actix_contrib_logger/middleware/struct.Logger.html#method.custom_error_resp_level)
the log level of error in responses are also configurable, otherwise by default like the original
logger all are printed in `DEBUG` level, except for server errors that are printed out in `ERROR` level.
The log also includes the HTTP status information (original logger doesn't). E.g.:```
[2023-08-13T20:59:53Z ERROR http_logger] Error in "500 Internal Server Error" response: DB(PoolTimedOut)
```### About
**Project Home**: https://github.com/mrsarm/rust-actix-contrib-logger.
#### Authors
- Original Authors: The Actix Project created the original `logger` module.
- Modifications in this project: Mariano Ruiz (mrsarm at gmail.com).## License
This project is licensed under either of the following licenses, at your option:
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0])
- MIT license ([LICENSE-MIT](LICENSE-MIT) or [http://opensource.org/licenses/MIT])