Skip to content

Commit

Permalink
Merge branch 'dev-logger' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
gsxhnd committed Jan 16, 2024
2 parents b16f472 + e948c33 commit 879b78d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 18 deletions.
106 changes: 104 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bytes = { version = "1.4", features = ["serde"] }
clap = { version = "4.3", features = ["derive", "env"] }
futures = { version = "0.3" }
futures-util = { version = "0.3" }
log = {version="0.4"}
log = { version = "0.4" }
reqwest = { version = "0.11", features = ["json"] }
scraper = "0.18"
sea-orm = { version = "0.12", features = [
Expand All @@ -52,6 +52,7 @@ tokio-util = { version = "0.7", features = ["codec"] }
tokio-stream = "0.1.14"
tokio-tungstenite = { version = "0.19" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3" }
tracing-appender = { version = "0.2" }
tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] }
tracing-log = { version = "0.2" }
walkdir = { version = "2" }
3 changes: 2 additions & 1 deletion garage-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ serde = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-log = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true, features = ["json", "env-filter"] }
19 changes: 6 additions & 13 deletions garage-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ mod ffmpeg;
mod jav;
mod spider;
mod tenhou;
mod utils;
use crate::utils::Logger;

use clap::Command;
use log;
use tracing::info;
use tracing_log::LogTracer;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
async fn main() {
match LogTracer::init() {
Ok(_) => {}
Err(err) => {
println!("log init error: {}", err);
}
}
log::warn!("test");
// tracing_subscriber::registry().with(fmt::layer()).init();
Logger::new().init();

let cmd = Command::new("garage")
.bin_name("garage")
Expand All @@ -35,10 +28,10 @@ async fn main() {
jav::parse_jav_cmd(sub_cmd.0, sub_cmd.1).await;
}
Some(("spider", _sub_m)) => {
println!("not work")
info!("todo");
}
Some(("tenhou", _sub_m)) => {
println!("not work")
info!("todo");
}
Some(("ffmpeg-batch", sub_m)) => {
info!("ffmpeg-batch starting...");
Expand Down
67 changes: 67 additions & 0 deletions garage-cli/src/utils/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use tracing_appender::rolling;
use tracing_subscriber::{self, filter, fmt, layer::SubscriberExt, Layer};

pub struct Logger {
log_path: Option<String>,
show_file: bool,
show_thread_names: bool,
log_level: filter::LevelFilter,
}

impl Logger {
pub fn new() -> Self {
Logger {
log_path: None,
show_file: false,
show_thread_names: false,
log_level: filter::LevelFilter::TRACE,
}
}

pub fn set_log_path(mut self, log_path: String) -> Self {
self.log_path = Some(log_path);
self
}

pub fn set_show_file(mut self, show: bool) -> Self {
self.show_file = show;
self
}

pub fn set_show_thread_name(mut self, show: bool) -> Self {
self.show_thread_names = show;
self
}

pub fn set_log_level(mut self, lvl: filter::LevelFilter) -> Self {
self.log_level = lvl;
self
}

pub fn init(self) {
match self.log_path {
Some(path) => {
let debug_file = rolling::minutely(path, "debug");
let a = fmt::Layer::new()
.json()
.with_file(self.show_file)
.with_thread_names(self.show_thread_names)
.with_writer(debug_file)
.with_filter(self.log_level);
let subscriber = tracing_subscriber::Registry::default().with(a);
tracing::subscriber::set_global_default(subscriber)
.expect("unable to set global subscriber");
}
None => {
let a = fmt::Layer::new()
.with_file(self.show_file)
.with_thread_names(self.show_thread_names)
.with_writer(std::io::stdout)
.with_filter(self.log_level);
let subscriber = tracing_subscriber::Registry::default().with(a);
tracing::subscriber::set_global_default(subscriber)
.expect("unable to set global subscriber");
}
}
}
}
2 changes: 2 additions & 0 deletions garage-cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod logger;
pub use logger::Logger;

0 comments on commit 879b78d

Please sign in to comment.