Skip to content

Commit

Permalink
Feat: Setting log level in the .env
Browse files Browse the repository at this point in the history
  • Loading branch information
xairaven committed Jul 8, 2024
1 parent 5727fa4 commit 1097d5f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -43,19 +46,13 @@ 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`).

<h3>Method 2. Docker</h3>

Place `.env` in `<repo_folder>/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
Expand Down
4 changes: 0 additions & 4 deletions media_fetch_bot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions media_fetch_bot/src/bot_config.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

Expand All @@ -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),
Expand All @@ -37,6 +54,7 @@ impl BotConfig {
Ok(BotConfig {
token,
name,
log_level,
tiktok_api_key,
})
}
Expand Down
4 changes: 2 additions & 2 deletions media_fetch_bot/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions media_fetch_bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 1097d5f

Please sign in to comment.