feat: allow logging output to file

This commit is contained in:
Jamin M 2023-04-21 08:32:53 -07:00 committed by Greg Heartsfield
parent 73285683a3
commit f4beb884b3
5 changed files with 106 additions and 58 deletions

118
Cargo.lock generated
View File

@ -54,15 +54,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.69" version = "1.0.69"
@ -420,7 +411,7 @@ dependencies = [
"js-sys", "js-sys",
"num-integer", "num-integer",
"num-traits", "num-traits",
"time", "time 0.1.45",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
] ]
@ -565,7 +556,7 @@ dependencies = [
"tonic", "tonic",
"tracing", "tracing",
"tracing-core", "tracing-core",
"tracing-subscriber 0.3.16", "tracing-subscriber",
] ]
[[package]] [[package]]
@ -1511,15 +1502,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "matchers"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1"
dependencies = [
"regex-automata",
]
[[package]] [[package]]
name = "matchers" name = "matchers"
version = "0.1.0" version = "0.1.0"
@ -1695,12 +1677,23 @@ dependencies = [
"tonic", "tonic",
"tonic-build", "tonic-build",
"tracing", "tracing",
"tracing-subscriber 0.2.25", "tracing-appender",
"tracing-subscriber",
"tungstenite", "tungstenite",
"url", "url",
"uuid", "uuid",
] ]
[[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]] [[package]]
name = "num" name = "num"
version = "0.2.1" version = "0.2.1"
@ -1861,6 +1854,12 @@ version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]] [[package]]
name = "parking" name = "parking"
version = "2.0.0" version = "2.0.0"
@ -2990,6 +2989,33 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "time"
version = "0.3.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890"
dependencies = [
"itoa",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd"
[[package]]
name = "time-macros"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36"
dependencies = [
"time-core",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -3223,6 +3249,17 @@ dependencies = [
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-appender"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e"
dependencies = [
"crossbeam-channel",
"time 0.3.20",
"tracing-subscriber",
]
[[package]] [[package]]
name = "tracing-attributes" name = "tracing-attributes"
version = "0.1.23" version = "0.1.23"
@ -3265,51 +3302,22 @@ dependencies = [
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-serde"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc6b213177105856957181934e4920de57730fc69bf42c37ee5bb664d406d9e1"
dependencies = [
"serde",
"tracing-core",
]
[[package]] [[package]]
name = "tracing-subscriber" name = "tracing-subscriber"
version = "0.2.25" version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e0d2eaa99c3c2e41547cfa109e910a68ea03823cccad4a0525dcbc9b01e8c71" checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [ dependencies = [
"ansi_term", "matchers",
"chrono", "nu-ansi-term",
"lazy_static", "once_cell",
"matchers 0.0.1",
"regex", "regex",
"serde",
"serde_json",
"sharded-slab", "sharded-slab",
"smallvec", "smallvec",
"thread_local", "thread_local",
"tracing", "tracing",
"tracing-core", "tracing-core",
"tracing-log", "tracing-log",
"tracing-serde",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
dependencies = [
"matchers 0.1.0",
"once_cell",
"regex",
"sharded-slab",
"thread_local",
"tracing",
"tracing-core",
] ]
[[package]] [[package]]

View File

@ -13,8 +13,9 @@ categories = ["network-programming", "web-programming"]
[dependencies] [dependencies]
clap = { version = "4.0.32", features = ["env", "default", "derive"]} clap = { version = "4.0.32", features = ["env", "default", "derive"]}
tracing = "0.1.36" tracing = "0.1.37"
tracing-subscriber = "0.2.0" tracing-appender = "0.2.2"
tracing-subscriber = "0.3.16"
tokio = { version = "1", features = ["full", "tracing", "signal"] } tokio = { version = "1", features = ["full", "tracing", "signal"] }
prost = "0.11" prost = "0.11"
tonic = "0.8.3" tonic = "0.8.3"

View File

@ -57,6 +57,9 @@ description = "A newly created nostr-rs-relay.\n\nCustomize this with your own i
# different nodes. Ignore for single-database instances. # different nodes. Ignore for single-database instances.
#connection_write = "postgresql://postgres:nostr@localhost:7500/nostr" #connection_write = "postgresql://postgres:nostr@localhost:7500/nostr"
[logging]
folder_path = "./log"
[grpc] [grpc]
# gRPC interfaces for externalized decisions and other extensions to # gRPC interfaces for externalized decisions and other extensions to
# functionality. # functionality.

View File

@ -166,6 +166,13 @@ impl VerifiedUsers {
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(unused)]
pub struct Logging {
pub folder_path: Option<String>,
pub file_prefix: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(unused)] #[allow(unused)]
pub struct Settings { pub struct Settings {
@ -180,6 +187,7 @@ pub struct Settings {
pub verified_users: VerifiedUsers, pub verified_users: VerifiedUsers,
pub retention: Retention, pub retention: Retention,
pub options: Options, pub options: Options,
pub logging: Logging,
} }
impl Settings { impl Settings {
@ -320,6 +328,10 @@ impl Default for Settings {
options: Options { options: Options {
reject_future_seconds: None, // Reject events in the future if defined reject_future_seconds: None, // Reject events in the future if defined
}, },
logging: Logging {
folder_path: None,
file_prefix: None,
},
} }
} }
} }

View File

@ -4,6 +4,8 @@ use console_subscriber::ConsoleLayer;
use nostr_rs_relay::cli::CLIArgs; use nostr_rs_relay::cli::CLIArgs;
use nostr_rs_relay::config; use nostr_rs_relay::config;
use nostr_rs_relay::server::start_server; use nostr_rs_relay::server::start_server;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::EnvFilter;
use std::sync::mpsc as syncmpsc; use std::sync::mpsc as syncmpsc;
use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender}; use std::sync::mpsc::{Receiver as MpscReceiver, Sender as MpscSender};
use std::thread; use std::thread;
@ -22,6 +24,8 @@ fn main() {
// get config file name from args // get config file name from args
let config_file_arg = args.config; let config_file_arg = args.config;
let mut _log_guard: Option<WorkerGuard> = None;
// configure settings from the config file (defaults to config.toml) // configure settings from the config file (defaults to config.toml)
// replace default settings with those read from the config file // replace default settings with those read from the config file
let mut settings = config::Settings::new(&config_file_arg); let mut settings = config::Settings::new(&config_file_arg);
@ -32,8 +36,28 @@ fn main() {
ConsoleLayer::builder().with_default_env().init(); ConsoleLayer::builder().with_default_env().init();
} else { } else {
// standard logging // standard logging
if let Some(path) = &settings.logging.folder_path {
// write logs to a folder
let prefix = match &settings.logging.file_prefix {
Some(p) => p.as_str(),
None => "relay",
};
let file_appender = tracing_appender::rolling::hourly(path, prefix);
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let filter = EnvFilter::from_default_env();
// assign to a variable that is not dropped till the program ends
_log_guard = Some(guard);
tracing_subscriber::fmt()
.with_env_filter(filter)
.with_writer(non_blocking)
.try_init()
.unwrap();
} else {
// write to stdout
tracing_subscriber::fmt::try_init().unwrap(); tracing_subscriber::fmt::try_init().unwrap();
} }
}
info!("Starting up from main"); info!("Starting up from main");
// get database directory from args // get database directory from args