cln-plugin: Add log filtering support

We filter based on the environment variable `CLN_PLUGIN_LOG`,
defaulting to `info` as that is not as noisy as `debug` or `trace`, at
least libraries will not spam us too heavily.

Changelog-Added cln-plugin: The logs level from cln-plugins can be configured by the `CLN_PLUGIN_LOG` environment variable.
This commit is contained in:
Christian Decker
2022-07-29 15:06:11 +02:00
parent 8898511cf6
commit 064a5a6940
2 changed files with 11 additions and 5 deletions

View File

@@ -21,8 +21,8 @@ tokio = { version="1", features = ['io-std', 'rt', 'sync', 'macros', 'io-util']
tokio-stream = "0.1" tokio-stream = "0.1"
futures = "0.3" futures = "0.3"
cln-rpc = { path = "../cln-rpc", version = "0.1.0" } cln-rpc = { path = "../cln-rpc", version = "0.1.0" }
env_logger = "0.9"
[dev-dependencies] [dev-dependencies]
tokio = { version = "1", features = ["macros", "rt-multi-thread", ] } tokio = { version = "1", features = ["macros", "rt-multi-thread", ] }
env_logger = "0.9"
cln-grpc = { path = "../cln-grpc" } cln-grpc = { path = "../cln-grpc" }

View File

@@ -1,6 +1,7 @@
use crate::codec::JsonCodec; use crate::codec::JsonCodec;
use env_logger::filter;
use futures::SinkExt; use futures::SinkExt;
use log::{Level, Metadata, Record}; use log::{Metadata, Record};
use serde::Serialize; use serde::Serialize;
use std::sync::Arc; use std::sync::Arc;
use tokio::io::AsyncWrite; use tokio::io::AsyncWrite;
@@ -42,6 +43,7 @@ struct PluginLogger {
// happen to emit a log record while holding the lock on the // happen to emit a log record while holding the lock on the
// plugin connection. // plugin connection.
sender: tokio::sync::mpsc::UnboundedSender<LogEntry>, sender: tokio::sync::mpsc::UnboundedSender<LogEntry>,
filter: filter::Filter,
} }
/// Initialize the logger starting a flusher to the passed in sink. /// Initialize the logger starting a flusher to the passed in sink.
@@ -50,6 +52,10 @@ where
O: AsyncWrite + Send + Unpin + 'static, O: AsyncWrite + Send + Unpin + 'static,
{ {
let out = out.clone(); let out = out.clone();
let filter_str = std::env::var("CLN_PLUGIN_LOG").unwrap_or("info".to_string());
let filter = filter::Builder::new().parse(&filter_str).build();
let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::<LogEntry>(); let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::<LogEntry>();
tokio::spawn(async move { tokio::spawn(async move {
while let Some(i) = receiver.recv().await { while let Some(i) = receiver.recv().await {
@@ -68,17 +74,17 @@ where
.await; .await;
} }
}); });
log::set_boxed_logger(Box::new(PluginLogger { sender })) log::set_boxed_logger(Box::new(PluginLogger { sender, filter }))
.map(|()| log::set_max_level(log::LevelFilter::Debug)) .map(|()| log::set_max_level(log::LevelFilter::Debug))
} }
impl log::Log for PluginLogger { impl log::Log for PluginLogger {
fn enabled(&self, metadata: &Metadata) -> bool { fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Debug self.filter.enabled(metadata)
} }
fn log(&self, record: &Record) { fn log(&self, record: &Record) {
if self.enabled(record.metadata()) { if self.filter.matches(record) {
self.sender self.sender
.send(LogEntry { .send(LogEntry {
level: record.level().into(), level: record.level().into(),