Skip to content

Commit

Permalink
Support logging in geyser plugins (solana-labs#34101)
Browse files Browse the repository at this point in the history
The change creates a new interface in the GeyserPlugin interface to allow the runtime to pass the logging configuration to the plugin.
  • Loading branch information
lijunwangs authored Nov 29, 2023
1 parent 60c09d3 commit f211c86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
29 changes: 29 additions & 0 deletions geyser-plugin-interface/src/geyser_plugin_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,35 @@ pub type Result<T> = std::result::Result<T, GeyserPluginError>;
/// Geyser plugins must describe desired behavior for load and unload,
/// as well as how they will handle streamed data.
pub trait GeyserPlugin: Any + Send + Sync + std::fmt::Debug {
/// The callback to allow the plugin to setup the logging configuration using the logger
/// and log level specified by the validator. Will be called first on load/reload, before any other
/// callback, and only called once.
/// # Examples
///
/// ```
/// use solana_geyser_plugin_interface::geyser_plugin_interface::{GeyserPlugin,
/// GeyserPluginError, Result};
///
/// #[derive(Debug)]
/// struct SamplePlugin;
/// impl GeyserPlugin for SamplePlugin {
/// fn setup_logger(&self, logger: &'static dyn log::Log, level: log::LevelFilter) -> Result<()> {
/// log::set_max_level(level);
/// if let Err(err) = log::set_logger(logger) {
/// return Err(GeyserPluginError::Custom(Box::new(err)));
/// }
/// Ok(())
/// }
/// fn name(&self) -> &'static str {
/// &"sample"
/// }
/// }
/// ```
#[allow(unused_variables)]
fn setup_logger(&self, logger: &'static dyn log::Log, level: log::LevelFilter) -> Result<()> {
Ok(())
}

fn name(&self) -> &'static str;

/// The callback called when a plugin is loaded by the system,
Expand Down
18 changes: 18 additions & 0 deletions geyser-plugin-manager/src/geyser_plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ impl GeyserPluginManager {
});
}

setup_logger_for_plugin(&*new_plugin)?;

// Call on_load and push plugin
new_plugin
.on_load(new_config_file, false)
Expand Down Expand Up @@ -193,6 +195,8 @@ impl GeyserPluginManager {
});
}

setup_logger_for_plugin(&*new_plugin)?;

// Attempt to on_load with new plugin
match new_plugin.on_load(new_parsed_config_file, true) {
// On success, push plugin and library
Expand Down Expand Up @@ -228,6 +232,20 @@ impl GeyserPluginManager {
}
}

// Initialize logging for the plugin
fn setup_logger_for_plugin(new_plugin: &dyn GeyserPlugin) -> Result<(), jsonrpc_core::Error> {
new_plugin
.setup_logger(log::logger(), log::max_level())
.map_err(|setup_logger_err| jsonrpc_core::Error {
code: ErrorCode::InvalidRequest,
message: format!(
"setup_logger method of plugin {} failed: {setup_logger_err}",
new_plugin.name()
),
data: None,
})
}

#[derive(Debug)]
pub enum GeyserPluginManagerRequest {
ReloadPlugin {
Expand Down

0 comments on commit f211c86

Please sign in to comment.