use log crate instead of raw prints

This commit is contained in:
Akulij 2025-04-28 22:53:15 +03:00
parent fd3ef221c9
commit a51344d089
2 changed files with 14 additions and 12 deletions

View File

@ -8,6 +8,7 @@ use crate::{
db::{CallDB, DB}, db::{CallDB, DB},
BotResult, BotResult,
}; };
use log::info;
// These are should not appear in /help // These are should not appear in /help
#[derive(BotCommands, Clone)] #[derive(BotCommands, Clone)]
@ -38,7 +39,7 @@ pub async fn admin_command_handler(
Some(user) => user, Some(user) => user,
None => return Ok(()), // do nothing, cause its not usecase of function None => return Ok(()), // do nothing, cause its not usecase of function
}; };
println!( info!(
"MSG: {}", "MSG: {}",
msg.html_text().unwrap_or("|EMPTY_MESSAGE|".into()) msg.html_text().unwrap_or("|EMPTY_MESSAGE|".into())
); );
@ -79,7 +80,7 @@ pub async fn secret_command_handler(
cmd: SecretCommands, cmd: SecretCommands,
admin_password: String, admin_password: String,
) -> BotResult<()> { ) -> BotResult<()> {
println!("Admin Pass: {}", admin_password); info!("Admin Pass: {}", admin_password);
let tguser = match msg.from.clone() { let tguser = match msg.from.clone() {
Some(user) => user, Some(user) => user,
None => return Ok(()), // do nothing, cause its not usecase of function None => return Ok(()), // do nothing, cause its not usecase of function
@ -87,7 +88,7 @@ pub async fn secret_command_handler(
let user = db let user = db
.get_or_init_user(tguser.id.0 as i64, &tguser.first_name) .get_or_init_user(tguser.id.0 as i64, &tguser.first_name)
.await?; .await?;
println!( info!(
"MSG: {}", "MSG: {}",
msg.html_text().unwrap_or("|EMPTY_MESSAGE|".into()) msg.html_text().unwrap_or("|EMPTY_MESSAGE|".into())
); );

View File

@ -2,6 +2,7 @@ pub mod admin;
pub mod db; pub mod db;
pub mod mongodb_storage; pub mod mongodb_storage;
use log::info;
use std::time::Duration; use std::time::Duration;
use crate::admin::{admin_command_handler, AdminCommands}; use crate::admin::{admin_command_handler, AdminCommands};
@ -53,7 +54,7 @@ trait LogMsg {
impl LogMsg for <teloxide::Bot as teloxide::prelude::Requester>::SendMessage { impl LogMsg for <teloxide::Bot as teloxide::prelude::Requester>::SendMessage {
fn log(self) -> Self { fn log(self) -> Self {
println!("msg: {}", self.text); info!("msg: {}", self.text);
self self
} }
} }
@ -117,15 +118,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
for event in events { for event in events {
match bc.db.create_event(event).await { match bc.db.create_event(event).await {
Ok(e) => println!("Created event {}", e._id), Ok(e) => info!("Created event {}", e._id),
Err(err) => println!("Failed to create event, error: {}", err), Err(err) => info!("Failed to create event, error: {}", err),
} }
} }
// //
let handler = dptree::entry() let handler = dptree::entry()
.inspect(|u: Update| { .inspect(|u: Update| {
eprintln!("{u:#?}"); // Print the update to the console with inspect info!("{u:#?}"); // Print the update to the console with inspect
}) })
.branch(Update::filter_callback_query().endpoint(callback_handler)) .branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(command_handler(config)) .branch(command_handler(config))
@ -242,11 +243,11 @@ async fn edit_msg_handler(
use teloxide::utils::render::Renderer; use teloxide::utils::render::Renderer;
let chat_id = msg.chat.id; let chat_id = msg.chat.id;
println!("Type: {:#?}", msg.kind); info!("Type: {:#?}", msg.kind);
let msg = if let MessageKind::Common(msg) = msg.kind { let msg = if let MessageKind::Common(msg) = msg.kind {
msg msg
} else { } else {
println!("Not a Common, somehow"); info!("Not a Common, somehow");
return Ok(()); return Ok(());
}; };
@ -414,7 +415,7 @@ async fn user_command_handler(
.await?; .await?;
let user = update_user_tg(user, &tguser); let user = update_user_tg(user, &tguser);
user.update_user(&mut db).await?; user.update_user(&mut db).await?;
println!( info!(
"MSG: {}", "MSG: {}",
msg.html_text().unwrap_or("|EMPTY_MESSAGE|".into()) msg.html_text().unwrap_or("|EMPTY_MESSAGE|".into())
); );
@ -460,7 +461,7 @@ async fn answer_message<RM: Into<ReplyMarkup>>(
None => msg, None => msg,
}; };
let msg = msg.parse_mode(teloxide::types::ParseMode::Html); let msg = msg.parse_mode(teloxide::types::ParseMode::Html);
println!("ENTS: {:?}", msg.entities); info!("ENTS: {:?}", msg.entities);
let msg = msg.await?; let msg = msg.await?;
(msg.chat.id.0, msg.id.0) (msg.chat.id.0, msg.id.0)
@ -588,7 +589,7 @@ async fn make_start_buttons(db: &mut DB) -> BotResult<InlineKeyboardMarkup> {
async fn echo(bot: Bot, msg: Message) -> BotResult<()> { async fn echo(bot: Bot, msg: Message) -> BotResult<()> {
if let Some(photo) = msg.photo() { if let Some(photo) = msg.photo() {
println!("File ID: {}", photo[0].file.id); info!("File ID: {}", photo[0].file.id);
} }
bot.send_message(msg.chat.id, msg.html_text().unwrap_or("UNWRAP".into())) bot.send_message(msg.chat.id, msg.html_text().unwrap_or("UNWRAP".into()))
.parse_mode(teloxide::types::ParseMode::Html) .parse_mode(teloxide::types::ParseMode::Html)