mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
ref(backend) start of refactor
This commit is contained in:
36 files changed
+183
-42
No files matched your search
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "api_server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
constants = { path = "../constants" }
|
||||
chrono = "0.4.38"
|
||||
tracing = "0.1.40"
|
||||
tracing-subscriber = "0.3.18"
|
||||
tracing-appender = "0.2.3"
|
||||
dotenv = "0.15.0"
|
||||
@@ -0,0 +1,22 @@
|
||||
use crate::logger;
|
||||
use std::env;
|
||||
use tracing_appender::non_blocking::WorkerGuard;
|
||||
|
||||
pub fn logger_init() -> (WorkerGuard, WorkerGuard) {
|
||||
logger::init()
|
||||
}
|
||||
|
||||
pub fn load_env() {
|
||||
if let Some(env_file) = env::args().nth(1) {
|
||||
match dotenv::from_filename(&env_file) {
|
||||
Ok(_) => {
|
||||
println!("[info] env loaded from {}, starting up", env_file);
|
||||
}
|
||||
Err(e) => {
|
||||
panic!("[fatal] failed to read {}: {}", env_file, e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("[info] no .env file referred, starting up")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
use std::{env, io};
|
||||
|
||||
use chrono::Local;
|
||||
use tracing::info;
|
||||
use tracing_appender::non_blocking::WorkerGuard;
|
||||
use tracing_appender::{non_blocking, rolling};
|
||||
use tracing_subscriber::fmt::format::Writer;
|
||||
use tracing_subscriber::fmt::time::FormatTime;
|
||||
use tracing_subscriber::fmt::MakeWriter;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::{fmt, Registry};
|
||||
|
||||
struct LocalTimer;
|
||||
|
||||
impl FormatTime for LocalTimer {
|
||||
fn format_time(&self, w: &mut Writer<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
w,
|
||||
"{}",
|
||||
Local::now().format(constants::time::DEFAULT_TIME_FORMAT)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
type _LogWriter = dyn for<'writer> MakeWriter<'writer, Writer = dyn io::Write + 'static> + 'static;
|
||||
|
||||
pub fn init() -> (WorkerGuard, WorkerGuard) {
|
||||
let log_path = env::var("LOG_ROOT_DIR").unwrap_or_else(|e| {
|
||||
panic!("[fatal] failed to load LOG_ROOT_DIR: {}", e);
|
||||
});
|
||||
|
||||
let log_path = format!("{}/backend", log_path);
|
||||
|
||||
let (info_log_writer, info_guard) = non_blocking(rolling::never(&log_path, "info.log"));
|
||||
|
||||
let (err_log_writer, error_guard) = non_blocking(rolling::never(&log_path, "error.log"));
|
||||
|
||||
let log_format = tracing_subscriber::fmt::format()
|
||||
.with_level(true)
|
||||
.with_target(true)
|
||||
.with_source_location(true)
|
||||
.with_timer(LocalTimer);
|
||||
|
||||
let subscribers = Registry::default()
|
||||
.with(
|
||||
fmt::Layer::default()
|
||||
.with_writer(info_log_writer)
|
||||
.with_ansi(false),
|
||||
)
|
||||
.with(
|
||||
fmt::Layer::default()
|
||||
.with_writer(err_log_writer)
|
||||
.with_ansi(false),
|
||||
)
|
||||
.with(
|
||||
fmt::Layer::default()
|
||||
.with_writer(io::stdout)
|
||||
.with_ansi(true)
|
||||
.event_format(log_format),
|
||||
);
|
||||
|
||||
tracing::subscriber::set_global_default(subscribers)
|
||||
.expect("[fatal] Failed to setup logging system");
|
||||
|
||||
info!("Module Started: Logger");
|
||||
|
||||
(info_guard, error_guard)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
mod init;
|
||||
mod logger;
|
||||
|
||||
fn main() {
|
||||
init::load_env();
|
||||
let _guards = init::logger_init();
|
||||
}
|
||||
Reference in new issue
Block a user