Initial work to switch to tracing (#2185)

* Add tracing dep

* Switch over basic events

* Switch over dynamically enabled events

* Fix missing SocketAddr formatting

* More format fixing

* refactor: Apply tracing changes to new crate structure
This commit is contained in:
CosmicHorror
2023-02-10 18:35:12 -07:00
committed by Austin Bonander
parent b72a52b066
commit acaee75a30
16 changed files with 183 additions and 119 deletions

View File

@@ -61,6 +61,7 @@ once_cell = "1.9.0"
smallvec = "1.7.0"
stringprep = "0.1.2"
thiserror = "1.0.35"
tracing = { version = "0.1.37", features = ["log"] }
whoami = "1.2.1"
serde = { version = "1.0.144", features = ["derive"] }

View File

@@ -151,10 +151,10 @@ impl PgAdvisoryLock {
// architecture for server software, so it should be a no-op there.
let key = PgAdvisoryLockKey::BigInt(i64::from_le_bytes(output_key_material));
log::trace!(
"generated {:?} from key string {:?}",
key,
input_key_material
tracing::trace!(
?key,
key_string = ?input_key_material,
"generated key from key string",
);
Self::with_key(key)
@@ -346,9 +346,9 @@ impl<'lock, C: AsMut<PgConnection>> PgAdvisoryLockGuard<'lock, C> {
.await?;
if !released {
log::warn!(
"PgAdvisoryLockGuard: advisory lock {:?} was not held by the contained connection",
self.lock.key
tracing::warn!(
lock = ?self.lock.key,
"PgAdvisoryLockGuard: advisory lock was not held by the contained connection",
);
}

View File

@@ -5,7 +5,10 @@ use std::str::FromStr;
use futures_channel::mpsc::UnboundedSender;
use futures_util::SinkExt;
use log::Level;
use sqlx_core::bytes::{Buf, Bytes};
use sqlx_core::{
bytes::{Buf, Bytes},
logger,
};
use crate::connection::tls::MaybeUpgradeTls;
use crate::error::Error;
@@ -141,25 +144,29 @@ impl PgStream {
let notice: Notice = message.decode()?;
let lvl = match notice.severity() {
PgSeverity::Fatal | PgSeverity::Panic | PgSeverity::Error => Level::Error,
PgSeverity::Warning => Level::Warn,
PgSeverity::Notice => Level::Info,
PgSeverity::Debug => Level::Debug,
PgSeverity::Info => Level::Trace,
PgSeverity::Log => Level::Trace,
let (log_level, tracing_level) = match notice.severity() {
PgSeverity::Fatal | PgSeverity::Panic | PgSeverity::Error => {
(Level::Error, tracing::Level::ERROR)
}
PgSeverity::Warning => (Level::Warn, tracing::Level::WARN),
PgSeverity::Notice => (Level::Info, tracing::Level::INFO),
PgSeverity::Debug => (Level::Debug, tracing::Level::DEBUG),
PgSeverity::Info | PgSeverity::Log => (Level::Trace, tracing::Level::TRACE),
};
if log::log_enabled!(target: "sqlx::postgres::notice", lvl) {
log::logger().log(
&log::Record::builder()
.args(format_args!("{}", notice.message()))
.level(lvl)
.module_path_static(Some("sqlx::postgres::notice"))
.target("sqlx::postgres::notice")
.file_static(Some(file!()))
.line(Some(line!()))
.build(),
let log_is_enabled = log::log_enabled!(
target: "sqlx::postgres::notice",
log_level
) || sqlx_core::private_tracing_dynamic_enabled!(
target: "sqlx::postgres::notice",
tracing_level
);
if log_is_enabled {
let message = format!("{}", notice.message());
sqlx_core::private_tracing_dynamic_event!(
target: "sqlx::postgres::notice",
tracing_level,
message
);
}

View File

@@ -96,7 +96,7 @@ impl PgConnectOptions {
}
}
_ => log::warn!("ignoring unrecognized connect parameter: {}={}", key, value),
_ => tracing::warn!(%key, %value, "ignoring unrecognized connect parameter"),
}
}

View File

@@ -47,10 +47,10 @@ fn load_password_from_file(
let permissions = metadata.permissions();
let mode = permissions.mode();
if mode & 0o77 != 0 {
log::warn!(
"ignoring {}: permissions for not strict enough: {:o}",
path.to_string_lossy(),
mode
tracing::warn!(
path = %path.to_string_lossy(),
permissions = format!("{:o}", mode),
"Ignoring path. Permissions are not strict enough",
);
return None;
}
@@ -136,7 +136,7 @@ fn matches_next_field(whole_line: &str, line: &mut &str, value: &str) -> Option<
}
}
None => {
log::warn!("Malformed line in pgpass file: {}", whole_line);
tracing::warn!(line = whole_line, "Malformed line in pgpass file");
None
}
}