diff --git a/README.md b/README.md index 2ac92bf..8163d64 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,12 @@ Firstly, create a file named `.env` with this structure: ```.env BOT_TOKEN=... BOT_NAME=your_telegram_bot_username +LOG_LEVEL=... TIKTOK_API_KEY=... ``` +If you want to change the log level (default is `ERROR`), set the LOG_LEVEL variable to the desired level (`error`, `warn`, `info`, `debug`, `trace`, `off`). + Place it in the location specified in the instruction for the preferred method. Then clone the repo if you want to use method 2 or 3: @@ -43,8 +46,6 @@ Download the archive from the [latest release](github.com/xairaven/MediaFetchBot In the archives, there is only one file — the executable. Place `.env` in the same folder. -If you want to change the log level (default is `ERROR`), set the environmental variable `RUST_LOG` to the desired level (`error`, `warn`, `info`, `debug`, `trace`, `off`). -

Method 2. Docker

Place `.env` in `/media_fetch_bot/`. @@ -52,10 +53,6 @@ Place `.env` in `/media_fetch_bot/`. Change `bot_username` in Dockerfile: ```Dockerfile -# Log level. Error, warn, info, debug, trace, off. -# Can also be "main" or "your_app_name" (same values) -ENV RUST_LOG=info - # Your app name (write same name to both) ARG APP_NAME_ARG=... ENV APP_NAME_ENV=...same diff --git a/media_fetch_bot/Dockerfile b/media_fetch_bot/Dockerfile index ad668d5..9f49a38 100644 --- a/media_fetch_bot/Dockerfile +++ b/media_fetch_bot/Dockerfile @@ -24,10 +24,6 @@ RUN apt update && apt install -y \ ######################################################## # ENV VARIABLES -# Log level. Error, warn, info, debug, trace, off. -# Can also be "main" or "your_app_name" (same values) -ENV RUST_LOG=info - # Your app name (write same name to both) ARG APP_NAME_ARG=media_fetch_bot ENV APP_NAME_ENV=media_fetch_bot diff --git a/media_fetch_bot/src/bot_config.rs b/media_fetch_bot/src/bot_config.rs index ad11a7b..1fd5263 100644 --- a/media_fetch_bot/src/bot_config.rs +++ b/media_fetch_bot/src/bot_config.rs @@ -1,10 +1,12 @@ use crate::error::BotError; use dotenvy::dotenv; use std::env; +use log::LevelFilter; pub struct BotConfig { pub token: String, pub name: String, + pub log_level: LevelFilter, pub tiktok_api_key: Option, } @@ -27,6 +29,21 @@ impl BotConfig { Err(_) => return Err(BotError::EnvBotName) }; + // Loading log level + let log_level = match env::var("LOG_LEVEL") { + Ok(value) => value, + Err(_) => return Err(BotError::EnvBotLogLevel) + }; + let log_level = match log_level.to_lowercase().trim() { + "off" => LevelFilter::Off, + "error" => LevelFilter::Error, + "warn" => LevelFilter::Warn, + "info" => LevelFilter::Info, + "debug" => LevelFilter::Debug, + "trace" => LevelFilter::Trace, + _ => return Err(BotError::EnvBotLogLevel) + }; + // Loading tiktok_api_key let tiktok_api_key = match env::var("TIKTOK_API_KEY") { Ok(value) => Some(value), @@ -37,6 +54,7 @@ impl BotConfig { Ok(BotConfig { token, name, + log_level, tiktok_api_key, }) } diff --git a/media_fetch_bot/src/error.rs b/media_fetch_bot/src/error.rs index 0fec94d..e6c4c4b 100644 --- a/media_fetch_bot/src/error.rs +++ b/media_fetch_bot/src/error.rs @@ -14,8 +14,8 @@ pub enum BotError { #[error("EnvBotName: Something wrong with .env BOT_NAME. May be, there's no field with this name.")] EnvBotName, - #[error("EnvSaveDir: Something wrong with .env SAVE_DIR. May be, there's no field with this name.")] - EnvSaveDir, + #[error("EnvEnvBotLogLevel: Something wrong with .env LOG_LEVEL. May be, there's no field with this name.")] + EnvBotLogLevel, #[error("FailedGetUserMessage: Failed to get user message.")] FailedGetUserMessage, diff --git a/media_fetch_bot/src/main.rs b/media_fetch_bot/src/main.rs index a7937dc..c413a46 100644 --- a/media_fetch_bot/src/main.rs +++ b/media_fetch_bot/src/main.rs @@ -6,6 +6,7 @@ use rust_i18n::t; use teloxide::{prelude::*, utils::command::BotCommands}; use teloxide::types::{ParseMode}; use std::{process}; +use pretty_env_logger::env_logger::Target; pub mod bot_commands; pub mod bot_config; @@ -19,18 +20,18 @@ rust_i18n::i18n!("locales"); #[tokio::main] async fn main() { - pretty_env_logger::init(); - - log::info!("Started executable..."); - rust_i18n::set_locale("en"); - log::info!("Set 'en' locale..."); let bot_config = BotConfig::build().unwrap_or_else(|err| { log::error!("Error: {err}"); process::exit(1); }); + pretty_env_logger::formatted_builder() + .filter_level(bot_config.log_level) + .target(Target::Stdout) + .init(); + log::info!("Starting bot..."); let bot = Bot::new(&bot_config.token);