Skip to content

Commit

Permalink
feat: json log fmt (#591)
Browse files Browse the repository at this point in the history
* introduce config var `LOG_FMT`

* change log output to json if `LOG_FMT=json`

* update the book

* rebuild docs

* update changelog
  • Loading branch information
sebadob authored Oct 14, 2024
1 parent cd41285 commit 1ef1353
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 9 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changelog

## UNRELEASED

### Changes

#### Upstream Auth Provider Query Params

Some upstream auth providers need custom query params appended to their authorization endpoint URL.
Rauthy will now accept URLs in the auth provider config with pre-defined query params, as long as they
don't interfere with OIDC default params.

[7dee26a](https://github.com/sebadob/rauthy/commit/7dee26af0ab757cd80395652fe03f82ffbc2c8bc)

#### Option Log Fmt as JSON

To make automatic parsing of logs possible (to some extent), you now have the ability to change the logging output from
text to json with the following new config variable:

```
# You can change the log output format to JSON, if you set:
# `LOG_FMT=json`.
# Keep in mind, that some logs will include escaped values,
# for instance when `Text` already logs a JSON in debug level.
# Some other logs like an Event for instance will be formatted
# as Text anyway. If you need to auto-parse events, please consider
# using an API token and listen ot them actively.
# default: text
#LOG_FMT=text
```

[]()

## 0.26.0

### Breaking
Expand Down
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ sqlx = { version = "0.8.2", features = ["macros", "migrate", "postgres", "runtim
strum = { version = "0.26.3", features = ["derive"] }
time = { version = "0.3", features = ["formatting", "local-offset", "macros", "parsing", "serde"] }
tracing = { version = "0.1", features = ["attributes"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "tracing"] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "json", "tracing"] }
tokio = { version = "1", features = ["full"] }
utoipa = { version = "4", features = ["actix_extras", "chrono", "openapi_extensions"] }
utoipa-swagger-ui = { version = "=7.0.1", features = ["actix-web"] }
Expand Down
15 changes: 15 additions & 0 deletions book/src/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,11 @@ EVENT_LEVEL_FAILED_LOGIN=info
# Accepts: error, info, debug, trace (default: info)
#LOG_LEVEL=info
# The log level for the `Hiqlite` persistence layer.
# At the time of writing, only the cache will use `hiqlite`
# default: info
LOG_LEVEL_DATABASE=info
# This is a special config which allows the configuration of
# customized access logs. These logs will be logged with each
# request in addition to the normal LOG_LEVEL logs.
Expand All @@ -937,6 +942,16 @@ EVENT_LEVEL_FAILED_LOGIN=info
# default: Modifying
#LOG_LEVEL_ACCESS=Basic
# You can change the log output format to JSON, if you set:
# `LOG_FMT=json`.
# Keep in mind, that some logs will include escaped values,
# for instance when `Text` already logs a JSON in debug level.
# Some other logs like an Event for instance will be formatted
# as Text anyway. If you need to auto-parse events, please consider
# using an API token and listen ot them actively.
# default: text
#LOG_FMT=text
#####################################
################ MFA ################
#####################################
Expand Down
15 changes: 15 additions & 0 deletions docs/config/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,11 @@ <h1 id="reference-config"><a class="header" href="#reference-config">Reference C
# Accepts: error, info, debug, trace (default: info)
#LOG_LEVEL=info

# The log level for the `Hiqlite` persistence layer.
# At the time of writing, only the cache will use `hiqlite`
# default: info
LOG_LEVEL_DATABASE=info

# This is a special config which allows the configuration of
# customized access logs. These logs will be logged with each
# request in addition to the normal LOG_LEVEL logs.
Expand All @@ -1117,6 +1122,16 @@ <h1 id="reference-config"><a class="header" href="#reference-config">Reference C
# default: Modifying
#LOG_LEVEL_ACCESS=Basic

# You can change the log output format to JSON, if you set:
# `LOG_FMT=json`.
# Keep in mind, that some logs will include escaped values,
# for instance when `Text` already logs a JSON in debug level.
# Some other logs like an Event for instance will be formatted
# as Text anyway. If you need to auto-parse events, please consider
# using an API token and listen ot them actively.
# default: text
#LOG_FMT=text

#####################################
################ MFA ################
#####################################
Expand Down
15 changes: 15 additions & 0 deletions docs/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -4263,6 +4263,11 @@ <h2 id="rotation-event"><a class="header" href="#rotation-event">Rotation Event<
# Accepts: error, info, debug, trace (default: info)
#LOG_LEVEL=info

# The log level for the `Hiqlite` persistence layer.
# At the time of writing, only the cache will use `hiqlite`
# default: info
LOG_LEVEL_DATABASE=info

# This is a special config which allows the configuration of
# customized access logs. These logs will be logged with each
# request in addition to the normal LOG_LEVEL logs.
Expand All @@ -4282,6 +4287,16 @@ <h2 id="rotation-event"><a class="header" href="#rotation-event">Rotation Event<
# default: Modifying
#LOG_LEVEL_ACCESS=Basic

# You can change the log output format to JSON, if you set:
# `LOG_FMT=json`.
# Keep in mind, that some logs will include escaped values,
# for instance when `Text` already logs a JSON in debug level.
# Some other logs like an Event for instance will be formatted
# as Text anyway. If you need to auto-parse events, please consider
# using an API token and listen ot them actively.
# default: text
#LOG_FMT=text

#####################################
################ MFA ################
#####################################
Expand Down
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/searchindex.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions rauthy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,16 @@ LOG_LEVEL_DATABASE=info
# (default: Modifying)
LOG_LEVEL_ACCESS=Basic

# You can change the log output format to JSON, if you set:
# `LOG_FMT=json`.
# Keep in mind, that some logs will include escaped values,
# for instance when `Text` already logs a JSON in debug level.
# Some other logs like an Event for instance will be formatted
# as Text anyway. If you need to auto-parse events, please consider
# using an API token and listen ot them actively.
# default: text
#LOG_FMT=text

#####################################
################ MFA ################
#####################################
Expand Down
22 changes: 16 additions & 6 deletions src/bin/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ pub fn setup_logging() -> tracing::Level {
env::set_var("RUST_BACKTRACE", "1");
}

let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(log_level)
.with_env_filter(filter)
.finish();

tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
if env::var("LOG_FMT").ok() == Some("json".to_string()) {
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.json()
.with_max_level(log_level)
.with_env_filter(filter)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
} else {
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(log_level)
.with_env_filter(filter)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting default subscriber failed");
};

log_level
}
Expand Down

0 comments on commit 1ef1353

Please sign in to comment.