This thread contains a patchset. You're looking at the original emails,
but you may wish to use the patch review UI.
Review patch
8
2
[PATCH hitide 0/3] Logging via env_logger
Hi,
here are some patches for hitide to introduce logging via env_logger in the
codebase.
Best,
Matthias
Matthias Beyer (3):
Add env_logger for logging
Replace eprintln!() calls with log::error!()
Replace println!() calls with log::debug!()
Cargo.lock | 59 +++++++++++++++++++++++++++++++++++++++
Cargo.toml | 2 ++
src/main.rs | 9 +++---
src/routes/communities.rs | 4 +--
src/routes/mod.rs | 2 +-
src/routes/posts.rs | 2 +-
6 files changed, 70 insertions(+), 8 deletions(-)
--
2.29.2
[PATCH hitide 1/3] Add env_logger for logging
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
---
Cargo.lock | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
Cargo.toml | 2 ++
src/main.rs | 1 +
3 files changed, 62 insertions(+)
diff --git a/Cargo.lock b/Cargo.lock
index 08f4162..2784790 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,6 +24,17 @@ dependencies = [
"url",
]
+ [[package]]
+ name = "atty"
+ version = "0.2.14"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+ dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi 0.3.9",
+ ]
+
[[package]]
name = "autocfg"
version = "1.0.1"
@@ -121,6 +132,19 @@ dependencies = [
"cfg-if 1.0.0",
]
+ [[package]]
+ name = "env_logger"
+ version = "0.8.2"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e"
+ dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+ ]
+
[[package]]
name = "fallible-iterator"
version = "0.2.0"
@@ -374,12 +398,22 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
+ [[package]]
+ name = "hermit-abi"
+ version = "0.1.17"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8"
+ dependencies = [
+ "libc",
+ ]
+
[[package]]
name = "hitide"
version = "0.8.0-pre"
dependencies = [
"ammonia",
"chrono",
+ "env_logger",
"fallible-iterator",
"fluent",
"fluent-langneg",
@@ -390,6 +424,7 @@ dependencies = [
"hyper",
"hyper-tls",
"lazy_static",
+ "log",
"multer",
"render",
"serde",
@@ -456,6 +491,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
+ [[package]]
+ name = "humantime"
+ version = "2.0.1"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
+
[[package]]
name = "hyper"
version = "0.13.9"
@@ -1271,6 +1312,15 @@ dependencies = [
"utf-8",
]
+ [[package]]
+ name = "termcolor"
+ version = "1.1.2"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+ dependencies = [
+ "winapi-util",
+ ]
+
[[package]]
name = "thiserror"
version = "1.0.22"
@@ -1616,6 +1666,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+ [[package]]
+ name = "winapi-util"
+ version = "0.1.5"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+ dependencies = [
+ "winapi 0.3.9",
+ ]
+
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
diff --git a/Cargo.toml b/Cargo.toml
index f89498c..089dc69 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -33,3 +33,5 @@ futures-util = "0.3.5"
hitide_icons = { path = "./icons" }
multer = "1.2.2"
fluent = "0.13.1"
+ log = "0.4"
+ env_logger = "0.8"
diff --git a/src/main.rs b/src/main.rs
index 1bd7be8..ec08e91 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -184,6 +184,7 @@ pub fn get_lang_for_req(req: &hyper::Request<hyper::Body>) -> Translator {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ let _ = env_logger::init();
let backend_host = std::env::var("BACKEND_HOST").expect("Missing BACKEND_HOST");
let port = match std::env::var("PORT") {
--
2.29.2
[PATCH hitide 2/3] Replace eprintln!() calls with log::error!()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
---
src/main.rs | 8 ++++ ----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index ec08e91..0633004 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -122,7 +122,7 @@ impl Translator {
let pattern = match pattern {
Some(pattern) => pattern,
None => {
- eprintln!("Missing message in translation: {}", key);
+ log::error!("Missing message in translation: {}", key);
return Cow::Borrowed(key);
}
};
@@ -130,7 +130,7 @@ impl Translator {
let mut errors = Vec::with_capacity(0);
let out = self.bundle.format_pattern(pattern, args, &mut errors);
if !errors.is_empty() {
- eprintln!("Errors in translation: {:?}", errors);
+ log::error!("Errors in translation: {:?}", errors);
}
out
@@ -167,7 +167,7 @@ pub fn get_lang_for_headers(headers: &hyper::header::HeaderMap) -> Translator {
match err {
fluent::FluentError::Overriding { .. } => {}
_ => {
- eprintln!("Failed to add language resource: {:?}", err);
+ log::error!("Failed to add language resource: {:?}", err);
break;
}
}
@@ -216,7 +216,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Err(Error::UserError(res)) => res,
Err(Error::RoutingError(err)) => err.to_simple_response(),
Err(err) => {
- eprintln!("Error: {:?}", err);
+ log::error!("Error: {:?}", err);
simple_response(
hyper::StatusCode::INTERNAL_SERVER_ERROR,
--
2.29.2
[PATCH hitide 3/3] Replace println!() calls with log::debug!()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
---
src/routes/communities.rs | 4 ++ --
src/routes/mod.rs | 2 + -
src/routes/posts.rs | 2 + -
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/routes/communities.rs b/src/routes/communities.rs
index 67d670f..44fc1ec 100644
--- a/src/routes/communities.rs
+++ b/src/routes/communities.rs
@@ -871,7 +871,7 @@ async fn handler_communities_new_post_submit(
Some(lang.tr("post_new_missing_content_type", None).into_owned());
}
Some(mime) => {
- println!("will upload media");
+ log::debug!("will upload media");
let res = res_to_error(
ctx.http_client
.request(for_client(
@@ -906,7 +906,7 @@ async fn handler_communities_new_post_submit(
}
}
- println!("finished media upload");
+ log::debug!("finished media upload");
}
}
}
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 67d3ad4..5affcf9 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -665,7 +665,7 @@ async fn handler_comment_submit_reply(
}
}
- println!("finished media upload");
+ log::debug!("finished media upload");
}
}
} else {
diff --git a/src/routes/posts.rs b/src/routes/posts.rs
index 100c097..9c3e5b5 100644
--- a/src/routes/posts.rs
+++ b/src/routes/posts.rs
@@ -522,7 +522,7 @@ async fn handler_post_submit_reply(
}
}
- println!("finished media upload");
+ log::debug!("finished media upload");
}
}
} else {
--
2.29.2
Re: [PATCH hitide 1/3] Add env_logger for logging
> #[tokio::main]
> async fn main() -> Result<(), Box<dyn std::error::Error>> {
> + let _ = env_logger::init();
Is assigning this necessary? Doesn't it just return ()?
Re: [PATCH hitide 1/3] Add env_logger for logging
Am Dienstag, 29. Dezember 2020, 19:00:17 CET schrieb Colin Reeder:
> > + let _ = env_logger::init();
>
> Is assigning this necessary? Doesn't it just return ()?
Ah, yes. There's also the env_logger::try_init() which returns a Result and I
mixed up that bit!
Fixup incoming.
--
Matthias
[PATCH hitide v2 1/3] Add env_logger for logging
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
---
Cargo.lock | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
Cargo.toml | 2 ++
src/main.rs | 1 +
3 files changed, 62 insertions(+)
diff --git a/Cargo.lock b/Cargo.lock
index 08f4162..2784790 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,6 +24,17 @@ dependencies = [
"url",
]
+ [[package]]
+ name = "atty"
+ version = "0.2.14"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+ dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi 0.3.9",
+ ]
+
[[package]]
name = "autocfg"
version = "1.0.1"
@@ -121,6 +132,19 @@ dependencies = [
"cfg-if 1.0.0",
]
+ [[package]]
+ name = "env_logger"
+ version = "0.8.2"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "f26ecb66b4bdca6c1409b40fb255eefc2bd4f6d135dab3c3124f80ffa2a9661e"
+ dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+ ]
+
[[package]]
name = "fallible-iterator"
version = "0.2.0"
@@ -374,12 +398,22 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
+ [[package]]
+ name = "hermit-abi"
+ version = "0.1.17"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8"
+ dependencies = [
+ "libc",
+ ]
+
[[package]]
name = "hitide"
version = "0.8.0-pre"
dependencies = [
"ammonia",
"chrono",
+ "env_logger",
"fallible-iterator",
"fluent",
"fluent-langneg",
@@ -390,6 +424,7 @@ dependencies = [
"hyper",
"hyper-tls",
"lazy_static",
+ "log",
"multer",
"render",
"serde",
@@ -456,6 +491,12 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
+ [[package]]
+ name = "humantime"
+ version = "2.0.1"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
+
[[package]]
name = "hyper"
version = "0.13.9"
@@ -1271,6 +1312,15 @@ dependencies = [
"utf-8",
]
+ [[package]]
+ name = "termcolor"
+ version = "1.1.2"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+ dependencies = [
+ "winapi-util",
+ ]
+
[[package]]
name = "thiserror"
version = "1.0.22"
@@ -1616,6 +1666,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+ [[package]]
+ name = "winapi-util"
+ version = "0.1.5"
+ source = "registry+https://github.com/rust-lang/crates.io-index"
+ checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+ dependencies = [
+ "winapi 0.3.9",
+ ]
+
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
diff --git a/Cargo.toml b/Cargo.toml
index f89498c..089dc69 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -33,3 +33,5 @@ futures-util = "0.3.5"
hitide_icons = { path = "./icons" }
multer = "1.2.2"
fluent = "0.13.1"
+ log = "0.4"
+ env_logger = "0.8"
diff --git a/src/main.rs b/src/main.rs
index 1bd7be8..76b2405 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -184,6 +184,7 @@ pub fn get_lang_for_req(req: &hyper::Request<hyper::Body>) -> Translator {
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
+ env_logger::init();
let backend_host = std::env::var("BACKEND_HOST").expect("Missing BACKEND_HOST");
let port = match std::env::var("PORT") {
--
2.29.2
[PATCH hitide v2 2/3] Replace eprintln!() calls with log::error!()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
---
src/main.rs | 8 ++++ ----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 76b2405..e757cc4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -122,7 +122,7 @@ impl Translator {
let pattern = match pattern {
Some(pattern) => pattern,
None => {
- eprintln!("Missing message in translation: {}", key);
+ log::error!("Missing message in translation: {}", key);
return Cow::Borrowed(key);
}
};
@@ -130,7 +130,7 @@ impl Translator {
let mut errors = Vec::with_capacity(0);
let out = self.bundle.format_pattern(pattern, args, &mut errors);
if !errors.is_empty() {
- eprintln!("Errors in translation: {:?}", errors);
+ log::error!("Errors in translation: {:?}", errors);
}
out
@@ -167,7 +167,7 @@ pub fn get_lang_for_headers(headers: &hyper::header::HeaderMap) -> Translator {
match err {
fluent::FluentError::Overriding { .. } => {}
_ => {
- eprintln!("Failed to add language resource: {:?}", err);
+ log::error!("Failed to add language resource: {:?}", err);
break;
}
}
@@ -216,7 +216,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Err(Error::UserError(res)) => res,
Err(Error::RoutingError(err)) => err.to_simple_response(),
Err(err) => {
- eprintln!("Error: {:?}", err);
+ log::error!("Error: {:?}", err);
simple_response(
hyper::StatusCode::INTERNAL_SERVER_ERROR,
--
2.29.2
[PATCH hitide v2 3/3] Replace println!() calls with log::debug!()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
---
src/routes/communities.rs | 4 ++ --
src/routes/mod.rs | 2 + -
src/routes/posts.rs | 2 + -
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/routes/communities.rs b/src/routes/communities.rs
index 67d670f..44fc1ec 100644
--- a/src/routes/communities.rs
+++ b/src/routes/communities.rs
@@ -871,7 +871,7 @@ async fn handler_communities_new_post_submit(
Some(lang.tr("post_new_missing_content_type", None).into_owned());
}
Some(mime) => {
- println!("will upload media");
+ log::debug!("will upload media");
let res = res_to_error(
ctx.http_client
.request(for_client(
@@ -906,7 +906,7 @@ async fn handler_communities_new_post_submit(
}
}
- println!("finished media upload");
+ log::debug!("finished media upload");
}
}
}
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 67d3ad4..5affcf9 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -665,7 +665,7 @@ async fn handler_comment_submit_reply(
}
}
- println!("finished media upload");
+ log::debug!("finished media upload");
}
}
} else {
diff --git a/src/routes/posts.rs b/src/routes/posts.rs
index 100c097..9c3e5b5 100644
--- a/src/routes/posts.rs
+++ b/src/routes/posts.rs
@@ -522,7 +522,7 @@ async fn handler_post_submit_reply(
}
}
- println!("finished media upload");
+ log::debug!("finished media upload");
}
}
} else {
--
2.29.2