implement tracing logging
This commit is contained in:
parent
3a5624facb
commit
c17e334c24
7 changed files with 203 additions and 12 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -6,4 +6,4 @@
|
|||
*.mp3
|
||||
*.mp4
|
||||
*.db
|
||||
*.log
|
||||
*.log*
|
||||
152
Cargo.lock
generated
152
Cargo.lock
generated
|
|
@ -235,6 +235,15 @@ version = "2.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "176dc175b78f56c0f321911d9c8eb2b77a78a4860b9c19db83835fea1a46649b"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.3.11"
|
||||
|
|
@ -306,6 +315,15 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.3.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
|
||||
dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_more"
|
||||
version = "0.99.17"
|
||||
|
|
@ -986,6 +1004,9 @@ dependencies = [
|
|||
"sqlx",
|
||||
"teloxide",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-appender",
|
||||
"tracing-subscriber",
|
||||
"url",
|
||||
]
|
||||
|
||||
|
|
@ -1028,6 +1049,16 @@ dependencies = [
|
|||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
|
||||
dependencies = [
|
||||
"overload",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint-dig"
|
||||
version = "0.8.4"
|
||||
|
|
@ -1045,6 +1076,12 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-conv"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
|
|
@ -1153,6 +1190,12 @@ dependencies = [
|
|||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "overload"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.1"
|
||||
|
|
@ -1256,6 +1299,12 @@ version = "0.3.27"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
|
|
@ -1606,6 +1655,15 @@ dependencies = [
|
|||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.1"
|
||||
|
|
@ -2070,6 +2128,47 @@ dependencies = [
|
|||
"syn 2.0.49",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"itoa",
|
||||
"num-conv",
|
||||
"powerfmt",
|
||||
"serde",
|
||||
"time-core",
|
||||
"time-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-core"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774"
|
||||
dependencies = [
|
||||
"num-conv",
|
||||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.6.0"
|
||||
|
|
@ -2157,17 +2256,28 @@ checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52"
|
|||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.37"
|
||||
version = "0.1.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
|
||||
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"log",
|
||||
"pin-project-lite",
|
||||
"tracing-attributes",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-appender"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"thiserror",
|
||||
"time",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.27"
|
||||
|
|
@ -2181,11 +2291,37 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "tracing-core"
|
||||
version = "0.1.31"
|
||||
version = "0.1.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a"
|
||||
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
|
||||
dependencies = [
|
||||
"nu-ansi-term",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2269,6 +2405,12 @@ dependencies = [
|
|||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
|
|
|
|||
|
|
@ -16,3 +16,6 @@ serde_json = "1.0.113"
|
|||
ordered-float = "4.2.0"
|
||||
regex = "1.10.3"
|
||||
url = "2.5.0"
|
||||
tracing = { version = "0.1.40", features = ["async-await"] }
|
||||
tracing-appender = "0.2.3"
|
||||
tracing-subscriber = "0.3.18"
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod bot;
|
||||
pub mod log;
|
||||
pub mod sanitize;
|
||||
pub mod util;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use anyhow;
|
||||
use sqlx::migrate::MigrateDatabase;
|
||||
use sqlx::{Sqlite, SqlitePool};
|
||||
use tracing::{event, Level};
|
||||
use std::env;
|
||||
use std::fmt;
|
||||
use std::str;
|
||||
|
|
@ -10,8 +12,8 @@ use teloxide::dispatching::dialogue::InMemStorage;
|
|||
use teloxide::dispatching::UpdateHandler;
|
||||
use teloxide::types::InputFile;
|
||||
use teloxide::{prelude::*, update_listeners::Polling, utils::command::BotCommands};
|
||||
use sqlx::{Sqlite, SqlitePool};
|
||||
|
||||
use super::log::log_init;
|
||||
use super::util::make_database_url;
|
||||
|
||||
use crate::dl::delete_if_exists;
|
||||
|
|
@ -37,9 +39,14 @@ where
|
|||
}
|
||||
|
||||
pub async fn bot_main() -> anyhow::Result<()> {
|
||||
log_init();
|
||||
event!(Level::INFO, "start");
|
||||
|
||||
let db_url = make_database_url();
|
||||
if !Sqlite::database_exists(&db_url).await.unwrap_or(false) {
|
||||
Sqlite::create_database(&db_url).await.expect("failed to create database");
|
||||
Sqlite::create_database(&db_url)
|
||||
.await
|
||||
.expect("failed to create database");
|
||||
}
|
||||
|
||||
let db = SqlitePool::connect(&db_url).await?;
|
||||
|
|
|
|||
29
src/bot/log.rs
Normal file
29
src/bot/log.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use super::util::VAR_LOG;
|
||||
use tracing::subscriber::set_global_default;
|
||||
use tracing::Level;
|
||||
use tracing_appender::{
|
||||
non_blocking,
|
||||
rolling::{RollingFileAppender, Rotation},
|
||||
};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::fmt;
|
||||
|
||||
pub fn log_init() {
|
||||
let file_appender = RollingFileAppender::builder()
|
||||
.rotation(Rotation::DAILY)
|
||||
.filename_prefix("mk-dl-bot.log")
|
||||
.max_log_files(7)
|
||||
.build(VAR_LOG)
|
||||
.unwrap();
|
||||
|
||||
let (non_blocking, guard) = non_blocking(file_appender);
|
||||
let file_layer = fmt::layer().with_ansi(true).with_writer(non_blocking);
|
||||
|
||||
let subscriber = fmt()
|
||||
.with_ansi(true)
|
||||
.with_max_level(Level::TRACE)
|
||||
.pretty()
|
||||
.finish()
|
||||
.with(file_layer);
|
||||
set_global_default(subscriber).expect("set_global_default subscriber");
|
||||
}
|
||||
|
|
@ -1,18 +1,27 @@
|
|||
use std::path::Path;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const VAR_LIB: &str = ".";
|
||||
pub const VAR_LIB: &str = ".";
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const VAR_LIB: &str = "/var/lib/mk-dl-bot";
|
||||
pub const VAR_LIB: &str = "/var/lib/mk-dl-bot";
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const VAR_LOG: &str = ".";
|
||||
pub const VAR_LOG: &str = ".";
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
const VAR_LOG: &str = "/var/log/mk-dl-bot";
|
||||
pub const VAR_LOG: &str = "/var/log/mk-dl-bot";
|
||||
|
||||
pub fn make_database_url() -> String {
|
||||
let path = Path::new(VAR_LIB).join("mk-dl-bot.db");
|
||||
format!("sqlite://{}", path.as_os_str().to_str().unwrap()).to_string()
|
||||
}
|
||||
|
||||
pub fn make_log_path() -> String {
|
||||
Path::new(VAR_LOG)
|
||||
.join("mk-dl-bot.log")
|
||||
.as_os_str()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue