Skip to content
This repository was archived by the owner on May 13, 2026. It is now read-only.

Commit a5fb4c0

Browse files
committed
write logs to file
1 parent c599402 commit a5fb4c0

3 files changed

Lines changed: 266 additions & 0 deletions

File tree

‎Cargo.lock‎

Lines changed: 234 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ pingora-proxy = "0.8.0"
1010
nullnet-grpc-lib = { git = "https://github.com/GyulyVGC/nullnet-grpc.git" }
1111
nullnet-liberror = { git = "https://github.com/NullNet-ai/libguard.git", branch = "nullnet-liberror-print" }
1212
tokio = { version = "1.51.1", features = ["rt-multi-thread"] }
13+
gag = "1.0.0"
14+
chrono = "0.4.44"

‎src/main.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use pingora_core::server::Server;
88
use pingora_core::upstreams::peer::HttpPeer;
99
use pingora_core::{Error, ErrorType, Result};
1010
use pingora_proxy::{ProxyHttp, Session};
11+
use std::fs::create_dir_all;
1112
use std::thread;
1213
use std::time::Instant;
1314

@@ -75,6 +76,15 @@ impl ProxyHttp for NullnetProxy {
7576

7677
#[tokio::main]
7778
async fn main() -> Result<(), nullnet_liberror::Error> {
79+
let _gag1: gag::Redirect<std::fs::File>;
80+
let _gag2: gag::Redirect<std::fs::File>;
81+
if let Some((gag1, gag2)) = redirect_stdout_stderr_to_file() {
82+
_gag1 = gag1;
83+
_gag2 = gag2;
84+
} else {
85+
println!("Failed to redirect stdout and stderr to file, logs will be printed to console");
86+
}
87+
7888
let proxy_address = format!("0.0.0.0:{PROXY_PORT}");
7989
println!("Running Nullnet proxy at {proxy_address}\n");
8090

@@ -92,3 +102,23 @@ async fn main() -> Result<(), nullnet_liberror::Error> {
92102
handle.join().unwrap();
93103
Ok(())
94104
}
105+
106+
fn redirect_stdout_stderr_to_file()
107+
-> Option<(gag::Redirect<std::fs::File>, gag::Redirect<std::fs::File>)> {
108+
let dir = "/var/log/nullnet";
109+
create_dir_all(dir).handle_err(location!()).ok()?;
110+
let timestamp = chrono::Utc::now().format("%Y-%m-%d_%H-%M-%S");
111+
let file_path = format!("{dir}/proxy_{timestamp}.txt");
112+
if let Ok(logs_file) = std::fs::OpenOptions::new()
113+
.create(true)
114+
.append(true)
115+
.open(&file_path)
116+
{
117+
println!("Writing logs to '{file_path}'");
118+
return Some((
119+
gag::Redirect::stdout(logs_file.try_clone().ok()?).ok()?,
120+
gag::Redirect::stderr(logs_file).ok()?,
121+
));
122+
}
123+
None
124+
}

0 commit comments

Comments
 (0)