-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): create lightweight background logging to stdout
- Loading branch information
1 parent
24299a7
commit 163abb9
Showing
5 changed files
with
79 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,77 @@ | ||
use std::io; | ||
|
||
use async_trait::async_trait; | ||
use pingora::{server::ShutdownWatch, services::background::BackgroundService}; | ||
use pingora::{ | ||
server::{ListenFds, ShutdownWatch}, | ||
services::Service, | ||
}; | ||
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; | ||
use tracing_subscriber::fmt::MakeWriter; | ||
|
||
/// A io::Write implementation that sends logs to a background service | ||
#[derive(Debug, Clone)] | ||
pub struct StdoutLogger(UnboundedSender<Vec<u8>>); | ||
|
||
impl io::Write for StdoutLogger { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
let buf_copy = buf.to_owned(); | ||
if let Ok(()) = self.0.send(buf_copy) { | ||
return Ok(buf.len()); | ||
} | ||
|
||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
todo!() | ||
} | ||
} | ||
|
||
/// A naive implementation of a logger that delegate sending logs to a background channel | ||
#[derive(Debug)] | ||
pub struct ProxyLogger { | ||
stdout: StdoutLogger, | ||
} | ||
|
||
use crate::config::LogLevel; | ||
impl ProxyLogger { | ||
pub fn new(sender: UnboundedSender<Vec<u8>>) -> Self { | ||
ProxyLogger { | ||
// level, | ||
stdout: StdoutLogger(sender.clone()), | ||
} | ||
} | ||
} | ||
|
||
/// impl from tracing_subscriber::fmt::MakeWriter | ||
impl<'a> MakeWriter<'a> for ProxyLogger { | ||
type Writer = StdoutLogger; | ||
|
||
fn make_writer(&'a self) -> Self::Writer { | ||
self.stdout.clone() | ||
} | ||
} | ||
|
||
pub struct ProxyLogger(pub LogLevel); | ||
/// A background service that receives logs from the main thread and writes them to stdout | ||
/// TODO: implement log rotation/write to disk (or use an existing lightweight crate) | ||
pub struct ProxyLoggerReceiver(pub UnboundedReceiver<Vec<u8>>); | ||
|
||
#[async_trait] | ||
impl BackgroundService for ProxyLogger { | ||
async fn start(&self, _shutdown: ShutdownWatch) { | ||
tracing_subscriber::fmt() | ||
.with_max_level(&self.0) | ||
.with_writer(std::io::stdout) | ||
.init() | ||
impl Service for ProxyLoggerReceiver { | ||
async fn start_service(&mut self, _fds: Option<ListenFds>, _shutdown: ShutdownWatch) { | ||
loop { | ||
if let Some(buf) = self.0.recv().await { | ||
let buf = std::str::from_utf8(&buf).unwrap(); | ||
// TODO: flush/rotate logs to disk | ||
print!("{}", buf); | ||
} | ||
} | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"ProxyLogger" | ||
} | ||
|
||
fn threads(&self) -> Option<usize> { | ||
Some(1) | ||
} | ||
} |