Skip to content

Commit

Permalink
feat: add control over the log levels using the RUST_LOG env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyAB committed Feb 5, 2024
1 parent 6def43b commit f2ef299
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,26 @@ The `turn-me-off` binary should located in `target/release/`.

## Usage

### Starting the server

Executing the binary starts the HTTP server, and you should be able to access a
Swagger UI documenting the API at the address http://localhost:3000/docs/#/

### Changing the port

Note that the port 3000 is the default port, to control the port on which it listens,
you can use the environment variable `TMF_PORT`:

```Shell
TMF_PORT=3001 turn-me-off
```

### Controlling log levels

By default the server will only log the minimum information (startup, shutdown, ...),
you can enable more logs, for example all connections established with the server, by
setting the env variable [`RUST_LOG`](https://docs.rs/env_logger/0.11.1/env_logger/#enabling-logging):

```Shell
RUST_LOG=trace turn-me-off
```
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ async fn main() {
struct ApiDoc;

tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "turn_me_off=info".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();

Expand All @@ -38,7 +42,7 @@ async fn main() {
.await
.unwrap_or_else(|_| panic!("Could not listen on port {port}"));

tracing::debug!(
tracing::info!(
"Linstening on {}",
listener
.local_addr()
Expand Down Expand Up @@ -89,7 +93,7 @@ async fn shutdown() {
};

tokio::select! {
() = ctrl_c => { tracing::debug!("Ctrl+c received. Bye!")},
_ = terminate => { tracing::debug!("SIGTERM received. Bye!")},
() = ctrl_c => { tracing::info!("Ctrl+c received. Bye!")},
_ = terminate => { tracing::info!("SIGTERM received. Bye!")},
}
}

0 comments on commit f2ef299

Please sign in to comment.