refactor(logging): move from log to tracing

This commit is contained in:
Paolo Barbolini
2020-08-28 12:19:35 +02:00
committed by Alexis Mousset
parent 41d68616e0
commit 542ea4ffd2
11 changed files with 35 additions and 35 deletions

View File

@@ -29,7 +29,7 @@ base64 = { version = "0.12", optional = true }
hostname = { version = "0.3", optional = true } hostname = { version = "0.3", optional = true }
hyperx = { version = "1", optional = true, features = ["headers"] } hyperx = { version = "1", optional = true, features = ["headers"] }
idna = "0.2" idna = "0.2"
log = { version = "0.4", optional = true } tracing = { version = "0.1.16", optional = true }
mime = { version = "0.3", optional = true } mime = { version = "0.3", optional = true }
native-tls = { version = "0.2", optional = true } native-tls = { version = "0.2", optional = true }
nom = { version = "5", optional = true } nom = { version = "5", optional = true }
@@ -47,7 +47,7 @@ webpki-roots = { version = "0.20", optional = true }
[dev-dependencies] [dev-dependencies]
criterion = "0.3" criterion = "0.3"
env_logger = "0.7" tracing-subscriber = "0.2.10"
glob = "0.3" glob = "0.3"
walkdir = "2" walkdir = "2"
tokio02_crate = { package = "tokio", version = "0.2.7", features = ["macros", "rt-threaded"] } tokio02_crate = { package = "tokio", version = "0.2.7", features = ["macros", "rt-threaded"] }

View File

@@ -1,7 +1,7 @@
use lettre::{Message, SmtpTransport, Transport}; use lettre::{Message, SmtpTransport, Transport};
fn main() { fn main() {
env_logger::init(); tracing_subscriber::fmt::init();
let email = Message::builder() let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap()) .from("NoBody <nobody@domain.tld>".parse().unwrap())

View File

@@ -2,6 +2,8 @@ use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport}; use lettre::{Message, SmtpTransport, Transport};
fn main() { fn main() {
tracing_subscriber::fmt::init();
let email = Message::builder() let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap()) .from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())

View File

@@ -2,6 +2,8 @@ use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport}; use lettre::{Message, SmtpTransport, Transport};
fn main() { fn main() {
tracing_subscriber::fmt::init();
let email = Message::builder() let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap()) .from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())

View File

@@ -8,6 +8,8 @@ use lettre::{AsyncSmtpTransport, Message, Tokio02Connector, Tokio02Transport};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
tracing_subscriber::fmt::init();
let email = Message::builder() let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap()) .from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())

View File

@@ -8,6 +8,8 @@ use lettre::{AsyncSmtpTransport, Message, Tokio02Connector, Tokio02Transport};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
tracing_subscriber::fmt::init();
let email = Message::builder() let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap()) .from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap()) .reply_to("Yuin <yuin@domain.tld>".parse().unwrap())

View File

@@ -16,7 +16,7 @@
//! * **rustls-tls**: TLS support with the `rustls` crate //! * **rustls-tls**: TLS support with the `rustls` crate
//! * **native-tls**: TLS support with the `native-tls` crate //! * **native-tls**: TLS support with the `native-tls` crate
//! * **r2d2**: Connection pool for SMTP transport //! * **r2d2**: Connection pool for SMTP transport
//! * **log**: Logging using the `log` crate //! * **tracing**: Logging using the `tracing` crate
//! * **serde**: Serialization/Deserialization of entities //! * **serde**: Serialization/Deserialization of entities
//! * **hostname**: Ability to try to use actual hostname in SMTP transaction //! * **hostname**: Ability to try to use actual hostname in SMTP transaction

View File

@@ -2,9 +2,6 @@ use std::{fmt::Display, io};
use futures_util::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use futures_util::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
#[cfg(feature = "log")]
use log::debug;
use super::{AsyncNetworkStream, ClientCodec, TlsParameters}; use super::{AsyncNetworkStream, ClientCodec, TlsParameters};
use crate::{ use crate::{
transport::smtp::{ transport::smtp::{
@@ -17,7 +14,7 @@ use crate::{
Envelope, Envelope,
}; };
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
use super::escape_crlf; use super::escape_crlf;
macro_rules! try_smtp ( macro_rules! try_smtp (
@@ -79,8 +76,8 @@ impl AsyncSmtpConnection {
conn.ehlo(hello_name).await?; conn.ehlo(hello_name).await?;
// Print server information // Print server information
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("server {}", conn.server_info); tracing::debug!("server {}", conn.server_info);
Ok(conn) Ok(conn)
} }
@@ -133,8 +130,8 @@ impl AsyncSmtpConnection {
self.stream.get_mut().upgrade_tls(tls_parameters).await, self.stream.get_mut().upgrade_tls(tls_parameters).await,
self self
); );
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("connection encrypted"); tracing::debug!("connection encrypted");
// Send EHLO again // Send EHLO again
try_smtp!(self.ehlo(hello_name).await, self); try_smtp!(self.ehlo(hello_name).await, self);
Ok(()) Ok(())
@@ -237,8 +234,8 @@ impl AsyncSmtpConnection {
self.stream.get_mut().write_all(string).await?; self.stream.get_mut().write_all(string).await?;
self.stream.get_mut().flush().await?; self.stream.get_mut().flush().await?;
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("Wrote: {}", escape_crlf(&String::from_utf8_lossy(string))); tracing::debug!("Wrote: {}", escape_crlf(&String::from_utf8_lossy(string)));
Ok(()) Ok(())
} }
@@ -247,8 +244,8 @@ impl AsyncSmtpConnection {
let mut buffer = String::with_capacity(100); let mut buffer = String::with_capacity(100);
while self.stream.read_line(&mut buffer).await? > 0 { while self.stream.read_line(&mut buffer).await? > 0 {
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("<< {}", escape_crlf(&buffer)); tracing::debug!("<< {}", escape_crlf(&buffer));
match parse_response(&buffer) { match parse_response(&buffer) {
Ok((_remaining, response)) => { Ok((_remaining, response)) => {
if response.is_positive() { if response.is_positive() {

View File

@@ -5,9 +5,6 @@ use std::{
time::Duration, time::Duration,
}; };
#[cfg(feature = "log")]
use log::debug;
use super::{ClientCodec, NetworkStream, TlsParameters}; use super::{ClientCodec, NetworkStream, TlsParameters};
use crate::{ use crate::{
transport::smtp::{ transport::smtp::{
@@ -20,7 +17,7 @@ use crate::{
Envelope, Envelope,
}; };
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
use super::escape_crlf; use super::escape_crlf;
macro_rules! try_smtp ( macro_rules! try_smtp (
@@ -76,8 +73,8 @@ impl SmtpConnection {
conn.ehlo(hello_name)?; conn.ehlo(hello_name)?;
// Print server information // Print server information
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("server {}", conn.server_info); tracing::debug!("server {}", conn.server_info);
Ok(conn) Ok(conn)
} }
@@ -125,8 +122,8 @@ impl SmtpConnection {
{ {
try_smtp!(self.command(Starttls), self); try_smtp!(self.command(Starttls), self);
try_smtp!(self.stream.get_mut().upgrade_tls(tls_parameters), self); try_smtp!(self.stream.get_mut().upgrade_tls(tls_parameters), self);
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("connection encrypted"); tracing::debug!("connection encrypted");
// Send EHLO again // Send EHLO again
try_smtp!(self.ehlo(hello_name), self); try_smtp!(self.ehlo(hello_name), self);
Ok(()) Ok(())
@@ -237,8 +234,8 @@ impl SmtpConnection {
self.stream.get_mut().write_all(string)?; self.stream.get_mut().write_all(string)?;
self.stream.get_mut().flush()?; self.stream.get_mut().flush()?;
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("Wrote: {}", escape_crlf(&String::from_utf8_lossy(string))); tracing::debug!("Wrote: {}", escape_crlf(&String::from_utf8_lossy(string)));
Ok(()) Ok(())
} }
@@ -247,8 +244,8 @@ impl SmtpConnection {
let mut buffer = String::with_capacity(100); let mut buffer = String::with_capacity(100);
while self.stream.read_line(&mut buffer)? > 0 { while self.stream.read_line(&mut buffer)? > 0 {
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("<< {}", escape_crlf(&buffer)); tracing::debug!("<< {}", escape_crlf(&buffer));
match parse_response(&buffer) { match parse_response(&buffer) {
Ok((_remaining, response)) => { Ok((_remaining, response)) => {
if response.is_positive() { if response.is_positive() {

View File

@@ -74,7 +74,7 @@ impl ClientCodec {
/// Returns the string replacing all the CRLF with "\<CRLF\>" /// Returns the string replacing all the CRLF with "\<CRLF\>"
/// Used for debug displays /// Used for debug displays
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
pub(super) fn escape_crlf(string: &str) -> String { pub(super) fn escape_crlf(string: &str) -> String {
string.replace("\r\n", "<CRLF>") string.replace("\r\n", "<CRLF>")
} }

View File

@@ -9,8 +9,6 @@ use crate::{
}, },
Address, Address,
}; };
#[cfg(feature = "log")]
use log::debug;
use std::{ use std::{
convert::AsRef, convert::AsRef,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
@@ -275,12 +273,12 @@ impl Auth {
let encoded_challenge = response let encoded_challenge = response
.first_word() .first_word()
.ok_or(Error::ResponseParsing("Could not read auth challenge"))?; .ok_or(Error::ResponseParsing("Could not read auth challenge"))?;
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("auth encoded challenge: {}", encoded_challenge); tracing::debug!("auth encoded challenge: {}", encoded_challenge);
let decoded_challenge = String::from_utf8(base64::decode(&encoded_challenge)?)?; let decoded_challenge = String::from_utf8(base64::decode(&encoded_challenge)?)?;
#[cfg(feature = "log")] #[cfg(feature = "tracing")]
debug!("auth decoded challenge: {}", decoded_challenge); tracing::debug!("auth decoded challenge: {}", decoded_challenge);
let response = Some(mechanism.response(&credentials, Some(decoded_challenge.as_ref()))?); let response = Some(mechanism.response(&credentials, Some(decoded_challenge.as_ref()))?);