mirror of
https://github.com/oxen-io/session-open-group-server.git
synced 2023-12-13 20:30:35 +01:00
Use log4rs to log to stdout and (optionally) to a file
This commit is contained in:
parent
e9bacc93b2
commit
745e6d4ed9
203
Cargo.lock
generated
203
Cargo.lock
generated
|
@ -60,6 +60,15 @@ version = "0.4.7"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.11.0"
|
||||
|
@ -69,6 +78,18 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "81cddc5f91628367664cc7c69714ff08deee8a3efc54623011c772544d7b2767"
|
||||
|
||||
[[package]]
|
||||
name = "arc-swap"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dabe5a181f83789739c194cbe5a897dde195078fac08568d09221fd6137a7ba8"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.5.2"
|
||||
|
@ -295,6 +316,17 @@ dependencies = [
|
|||
"rusticata-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derivative"
|
||||
version = "2.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.9.0"
|
||||
|
@ -304,6 +336,12 @@ dependencies = [
|
|||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dtoa"
|
||||
version = "0.4.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88d7ed2934d741c6b37e33e3832298e8850b53fd2d2bea03873375596c7cea4e"
|
||||
|
||||
[[package]]
|
||||
name = "fallible-iterator"
|
||||
version = "0.2.0"
|
||||
|
@ -595,6 +633,12 @@ version = "0.3.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "0.14.4"
|
||||
|
@ -709,6 +753,12 @@ dependencies = [
|
|||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linked-hash-map"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.2"
|
||||
|
@ -725,6 +775,40 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log-mdc"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7"
|
||||
|
||||
[[package]]
|
||||
name = "log4rs"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d1572a880d1115ff867396eee7ae2bc924554225e67a0d3c85c745b3e60ca211"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"arc-swap",
|
||||
"chrono",
|
||||
"derivative",
|
||||
"fnv",
|
||||
"humantime",
|
||||
"libc",
|
||||
"log",
|
||||
"log-mdc",
|
||||
"parking_lot",
|
||||
"regex",
|
||||
"serde",
|
||||
"serde-value",
|
||||
"serde_json",
|
||||
"serde_yaml",
|
||||
"thiserror",
|
||||
"thread-id",
|
||||
"typemap",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -868,6 +952,15 @@ version = "0.3.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
|
||||
|
||||
[[package]]
|
||||
name = "ordered-float"
|
||||
version = "2.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "766f840da25490628d8e63e529cd21c014f6600c6b8517add12a6fa6167a6218"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.11.1"
|
||||
|
@ -888,7 +981,7 @@ dependencies = [
|
|||
"cfg-if",
|
||||
"instant",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"redox_syscall 0.2.5",
|
||||
"smallvec",
|
||||
"winapi",
|
||||
]
|
||||
|
@ -1127,6 +1220,12 @@ dependencies = [
|
|||
"rand_core 0.6.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.2.5"
|
||||
|
@ -1142,7 +1241,10 @@ version = "1.4.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"thread_local",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1264,6 +1366,16 @@ dependencies = [
|
|||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-value"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c"
|
||||
dependencies = [
|
||||
"ordered-float",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.123"
|
||||
|
@ -1298,6 +1410,18 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.8.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23"
|
||||
dependencies = [
|
||||
"dtoa",
|
||||
"linked-hash-map",
|
||||
"serde",
|
||||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "session-open-group-server"
|
||||
version = "1.0.0"
|
||||
|
@ -1311,6 +1435,8 @@ dependencies = [
|
|||
"hmac",
|
||||
"http",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"log4rs",
|
||||
"r2d2",
|
||||
"r2d2_sqlite",
|
||||
"rand 0.8.3",
|
||||
|
@ -1465,7 +1591,7 @@ dependencies = [
|
|||
"cfg-if",
|
||||
"libc",
|
||||
"rand 0.8.3",
|
||||
"redox_syscall",
|
||||
"redox_syscall 0.2.5",
|
||||
"remove_dir_all",
|
||||
"winapi",
|
||||
]
|
||||
|
@ -1479,6 +1605,46 @@ dependencies = [
|
|||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread-id"
|
||||
version = "3.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7fbf4c9d56b320106cd64fd024dadfa0be7cb4706725fc44a7d7ce952d820c1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_syscall 0.1.57",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.43"
|
||||
|
@ -1634,6 +1800,12 @@ dependencies = [
|
|||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "traitobject"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
|
||||
|
||||
[[package]]
|
||||
name = "try-lock"
|
||||
version = "0.2.3"
|
||||
|
@ -1668,6 +1840,15 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typemap"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "653be63c80a3296da5551e1bfd2cca35227e13cdd08c6668903ae2f4f77aa1f6"
|
||||
dependencies = [
|
||||
"unsafe-any",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.12.0"
|
||||
|
@ -1729,6 +1910,15 @@ dependencies = [
|
|||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unsafe-any"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f30360d7979f5e9c6e6cea48af192ea8fab4afb3cf72597154b8f08935bc9c7f"
|
||||
dependencies = [
|
||||
"traitobject",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.7.1"
|
||||
|
@ -1931,6 +2121,15 @@ dependencies = [
|
|||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yaml-rust"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
|
||||
dependencies = [
|
||||
"linked-hash-map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zeroize"
|
||||
version = "1.2.0"
|
||||
|
|
|
@ -5,6 +5,8 @@ authors = ["Niels Andriesse <niels@oxen.io>"]
|
|||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
log = "0.4"
|
||||
log4rs = "1"
|
||||
aes-gcm = "0.8"
|
||||
base64 = "0.13"
|
||||
chrono = "0.4"
|
||||
|
|
55
src/logging.rs
Normal file
55
src/logging.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use log::LevelFilter;
|
||||
use log4rs::{
|
||||
append::{
|
||||
console::ConsoleAppender,
|
||||
rolling_file::{policy::compound, RollingFileAppender},
|
||||
},
|
||||
config::{Appender, Logger, Root},
|
||||
encode::pattern::PatternEncoder,
|
||||
filter::threshold::ThresholdFilter,
|
||||
};
|
||||
|
||||
pub(crate) fn init(log_file: Option<String>) {
|
||||
let console_level = LevelFilter::Debug;
|
||||
let file_level = LevelFilter::Info;
|
||||
|
||||
let stdout_appender = {
|
||||
let encoder = Box::new(PatternEncoder::new("{h({l})} {d} - {m}{n}"));
|
||||
let stdout = ConsoleAppender::builder().encoder(encoder).build();
|
||||
let filter = Box::new(ThresholdFilter::new(console_level));
|
||||
Appender::builder().filter(filter).build("stdout", Box::new(stdout))
|
||||
};
|
||||
|
||||
let mut root = Root::builder().appender("stdout");
|
||||
|
||||
// increase chainflip logging level to debug
|
||||
let chainflip = Logger::builder().build("session_open_group_server", LevelFilter::Debug);
|
||||
|
||||
let mut config_builder = log4rs::Config::builder().logger(chainflip).appender(stdout_appender);
|
||||
|
||||
if let Some(log_file) = log_file {
|
||||
// Rotate log files every ~50MB keeping 1 archived
|
||||
let size_trigger = compound::trigger::size::SizeTrigger::new(50_000_000);
|
||||
let roller = compound::roll::fixed_window::FixedWindowRoller::builder()
|
||||
.build(&format!("{}-archive.{{}}", &log_file), 1)
|
||||
.unwrap();
|
||||
let roll_policy = compound::CompoundPolicy::new(Box::new(size_trigger), Box::new(roller));
|
||||
|
||||
// Print to the file at Info level
|
||||
let file_appender =
|
||||
RollingFileAppender::builder().build(&log_file, Box::new(roll_policy)).unwrap();
|
||||
let filter = Box::new(ThresholdFilter::new(file_level));
|
||||
let file_appender =
|
||||
Appender::builder().filter(filter).build("file", Box::new(file_appender));
|
||||
|
||||
config_builder = config_builder.appender(file_appender);
|
||||
|
||||
root = root.appender("file");
|
||||
}
|
||||
|
||||
let root = root.build(file_level);
|
||||
|
||||
let config = config_builder.build(root).unwrap();
|
||||
|
||||
let _ = log4rs::init_config(config).expect("Error initialising log configuration");
|
||||
}
|
19
src/main.rs
19
src/main.rs
|
@ -9,12 +9,15 @@ use warp::Filter;
|
|||
mod crypto;
|
||||
mod errors;
|
||||
mod handlers;
|
||||
mod logging;
|
||||
mod models;
|
||||
mod onion_requests;
|
||||
mod routes;
|
||||
mod rpc;
|
||||
mod storage;
|
||||
|
||||
use log::info;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
|
@ -44,25 +47,33 @@ struct Opt {
|
|||
#[structopt(long = "x25519-private-key", default_value = "x25519_private_key.pem")]
|
||||
x25519_private_key: String,
|
||||
|
||||
/// Path to the file where logs will be saved. If not provided, logs are only
|
||||
/// printed to stdout.
|
||||
#[structopt(long = "log-file")]
|
||||
log_file: Option<String>,
|
||||
|
||||
/// Port to bind to.
|
||||
#[structopt(short = "P", long = "port", default_value = "80")]
|
||||
port: u16,
|
||||
|
||||
/// IP to bind to.
|
||||
#[structopt(short = "H", long = "host", default_value = "0.0.0.0")]
|
||||
host: Ipv4Addr,
|
||||
host: Ipv4Addr
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Parse arguments
|
||||
let opt = Opt::from_args();
|
||||
|
||||
logging::init(opt.log_file);
|
||||
|
||||
let addr = SocketAddr::new(IpAddr::V4(opt.host), opt.port);
|
||||
*crypto::PRIVATE_KEY_PATH.lock().unwrap() = opt.x25519_private_key;
|
||||
*crypto::PUBLIC_KEY_PATH.lock().unwrap() = opt.x25519_public_key;
|
||||
// Print the server public key
|
||||
let hex_public_key = hex::encode(crypto::PUBLIC_KEY.as_bytes());
|
||||
println!("The public key of this server is: {}", hex_public_key);
|
||||
info!("The public key of this server is: {}", hex_public_key);
|
||||
// Create the main database
|
||||
storage::create_main_database_if_needed();
|
||||
// Create required folders
|
||||
|
@ -77,7 +88,7 @@ async fn main() {
|
|||
// Serve routes
|
||||
let routes = routes::root().or(routes::lsrpc());
|
||||
if opt.tls {
|
||||
println!("Running on {} with TLS.", addr);
|
||||
info!("Running on {} with TLS.", addr);
|
||||
let serve_routes_future = warp::serve(routes)
|
||||
.tls()
|
||||
.cert_path(opt.tls_certificate)
|
||||
|
@ -91,7 +102,7 @@ async fn main() {
|
|||
serve_routes_future
|
||||
);
|
||||
} else {
|
||||
println!("Running on {}.", addr);
|
||||
info!("Running on {}.", addr);
|
||||
let serve_routes_future = warp::serve(routes).run(addr);
|
||||
// Keep futures alive
|
||||
join!(
|
||||
|
|
Loading…
Reference in a new issue