Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .schema/pgdog.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,11 @@
"description": "Structured JSON logs suitable for ECS/Datadog ingestion.",
"type": "string",
"const": "json"
},
{
"description": "Structured JSON logs with event fields flattened into the root object.",
"type": "string",
"const": "json_flattened"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions example.pgdog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ openmetrics_namespace = "pgdog_"
# Available options:
# - text
# - json
# - json_flattened
log_format = "text"
# Log filter directives using the same syntax as RUST_LOG.
#
Expand Down
7 changes: 7 additions & 0 deletions pgdog-config/src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ pub enum LogFormat {
Text,
/// Structured JSON logs suitable for ECS/Datadog ingestion.
Json,
/// Structured JSON logs with event fields flattened into the root object.
JsonFlattened,
}

impl fmt::Display for LogFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Text => f.write_str("text"),
Self::Json => f.write_str("json"),
Self::JsonFlattened => f.write_str("json_flattened"),
}
}
}
Expand All @@ -46,6 +49,7 @@ impl FromStr for LogFormat {
match s.to_ascii_lowercase().as_str() {
"text" => Ok(Self::Text),
"json" => Ok(Self::Json),
"json_flattened" => Ok(Self::JsonFlattened),
_ => Err(()),
}
}
Expand Down Expand Up @@ -1635,6 +1639,9 @@ mod tests {
assert_eq!(General::log_format(), LogFormat::Json);
assert_eq!(General::log_level(), "pgdog=debug,info");

env::set_var("PGDOG_LOG_FORMAT", "json_flattened");
assert_eq!(General::log_format(), LogFormat::JsonFlattened);

env::remove_var("PGDOG_LOG_FORMAT");
env::remove_var("RUST_LOG");

Expand Down
13 changes: 9 additions & 4 deletions pgdog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,11 @@ fn init_logger(general: Option<&General>) {

let throttle = throttle_handle().clone();

match general
let log_format = general
.map(|general| general.log_format)
.unwrap_or_default()
{
.unwrap_or_default();

match log_format {
LogFormat::Text => {
let format = fmt::layer()
.with_ansi(std::io::stderr().is_terminal())
Expand All @@ -191,14 +192,18 @@ fn init_logger(general: Option<&General>) {
.with(filter)
.try_init();
}
LogFormat::Json => {
LogFormat::Json | LogFormat::JsonFlattened => {
let format = fmt::layer()
.json()
.with_ansi(false)
.with_writer(std::io::stderr)
.with_file(false)
.with_current_span(false)
.with_span_list(false);
let format = match log_format {
LogFormat::JsonFlattened => format.flatten_event(true),
_ => format,
};
#[cfg(not(debug_assertions))]
let format = format.with_target(false);
let format = format.with_filter(throttle);
Expand Down
5 changes: 4 additions & 1 deletion pgdog/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ pub fn load(config: &Config) -> Result<(), libloading::Error> {

let pd_config = PdConfig {
log_level: PdStr::from(config.general.log_level.as_str()),
log_json: if config.general.log_format == LogFormat::Json {
log_json: if matches!(
config.general.log_format,
LogFormat::Json | LogFormat::JsonFlattened
) {
1
} else {
0
Expand Down
Loading