mirror of
https://github.com/OMGeeky/twba.common.git
synced 2025-12-26 17:02:35 +01:00
init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/.idea
|
||||
/target
|
||||
2987
Cargo.lock
generated
Normal file
2987
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
Normal file
15
Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "twba-common"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
tracing-appender = "0.2"
|
||||
|
||||
|
||||
twba-backup-config = { version = "0.1", git = "https://github.com/OMGeeky/backup_config.git" }
|
||||
twba-local-db = { version = "0.2", git = "https://github.com/OMGeeky/twitch_backup.local_db.git" }
|
||||
41
src/lib.rs
Normal file
41
src/lib.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use tracing::{info, Level};
|
||||
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
|
||||
use tracing_subscriber::fmt::format::{DefaultFields, Format};
|
||||
use tracing_subscriber::fmt::{Layer, Subscriber};
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::{fmt, EnvFilter};
|
||||
use twba_backup_config::Conf;
|
||||
|
||||
pub fn get_config() -> Conf {
|
||||
twba_backup_config::get_default_builder()
|
||||
.load()
|
||||
.expect("Failed to load config")
|
||||
}
|
||||
|
||||
pub fn init_tracing(crate_name: &str) -> WorkerGuard {
|
||||
let (guard, file) = file_tracer(crate_name);
|
||||
|
||||
let file_subscriber = Subscriber::builder()
|
||||
.with_max_level(Level::INFO)
|
||||
.with_env_filter(format!("warn,{}=trace", crate_name))
|
||||
.finish()
|
||||
.with(Layer::default().with_writer(file));
|
||||
|
||||
// Set the layered subscriber as the global default
|
||||
tracing::subscriber::set_global_default(file_subscriber)
|
||||
.expect("Failed to set global default subscriber");
|
||||
info!("Tracing initialized for {}", crate_name);
|
||||
guard
|
||||
}
|
||||
|
||||
pub fn file_tracer(crate_name: &str) -> (WorkerGuard, NonBlocking) {
|
||||
let dir = get_config().log_path();
|
||||
let trace_writer = tracing_appender::rolling::RollingFileAppender::builder()
|
||||
.rotation(tracing_appender::rolling::Rotation::HOURLY)
|
||||
.filename_prefix(format!("{}-trace", crate_name))
|
||||
.filename_suffix("log")
|
||||
.build(dir)
|
||||
.unwrap();
|
||||
let (file, guard) = tracing_appender::non_blocking(trace_writer);
|
||||
(guard, file)
|
||||
}
|
||||
12
src/main.rs
Normal file
12
src/main.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use tracing::*;
|
||||
use twba_common::init_tracing;
|
||||
|
||||
pub fn main() {
|
||||
let _guard = init_tracing("common");
|
||||
|
||||
info!("Hello, world! info");
|
||||
error!("Hello, world! error");
|
||||
warn!("Hello, world! warn");
|
||||
debug!("Hello, world! debug");
|
||||
trace!("Hello, world! trace");
|
||||
}
|
||||
Reference in New Issue
Block a user