Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Allow for a cleaner way to selectively shortcut loggings #373

Closed
aanndryyyy opened this issue Jul 17, 2023 · 6 comments
Closed
Assignees

Comments

@aanndryyyy
Copy link

Background information: I am using Simple History as a logging framework where I want to disable all built-in logging functionality. I have also set the logging database to never purge (days = 0) and disabled the manual clearing button.

Currently the filters for simple_history/log/do_log are returning after each apply_filters() call. I propose to make it return only at the end, so instead of this:

add_filter( 'simple_history/log/do_log', 'my_big_function_to_filter_loggers' );

it would be possible to do this:

add_filter( 'simple_history/log/do_log', '__return_false' ); // Disable all loggers
add_filter( 'simple_history/log/do_log/MyCustomLogger', '__return_true' ); // Allow only my custom logger

resulting in only MyCustomLogger logs being allowed. Or am I missing something and its already possible to do it like this? 🤔


Reference to hooks:

/**
* Filter that makes it possible to shortcut the logging of a message.
* Return bool false to cancel logging .
*
* @since 2.3.1
*
* @param bool $doLog Whether to log or not.
* @param string $level The loglevel.
* @param string $message The log message.
* @param array $context The message context.
* @param Logger $instance Logger instance.
*/
$do_log = apply_filters(
'simple_history/log/do_log',
true,
$level,
$message,
$context,
$this
);
if ( false === $do_log ) {
return $this;
}
/**
* Easy shortcut method to disable logging of messages from
* a specific logger.
*
* Example filter name:
* simple_history/log/do_log/SimpleUserLogger
* simple_history/log/do_log/SimplePostLogger
*
* Example to disable logging of any user login/logout/failed login activity:
* add_filter('simple_history/log/do_log/SimpleUserLogger', '__return_false')
*
* @since 2.nn
*/
$do_log = apply_filters(
"simple_history/log/do_log/{$this->get_slug()}",
true
);
if ( false === $do_log ) {
return $this;
}
/**
* Easy shortcut method to disable logging of messages from
* a specific logger and message.
*
* Example filter name:
* simple_history/log/do_log/SimpleUserLogger/user_logged_in
* simple_history/log/do_log/SimplePostLogger/post_updated
*
* @since 2.nn
*/
$message_key = $context['_message_key'] ?? null;
$do_log = apply_filters(
"simple_history/log/do_log/{$this->get_slug()}/{$message_key}",
true
);
if ( false === $do_log ) {
return $this;
}

@aanndryyyy
Copy link
Author

aanndryyyy commented Jul 18, 2023

It is actually doable with one hook:

add_filter( 'simple_history/core_loggers', '__return_empty_array' );
add_action( 'simple_history/add_custom_logger', '...' );

I guess I was missing something after all 😄

@bonny
Copy link
Owner

bonny commented Jul 18, 2023

Nice solution! Never though about that but I'm happy you found a way.

@bonny bonny closed this as completed Jul 18, 2023
@aanndryyyy
Copy link
Author

@bonny seems that a PHP error is raised at

$arr_found_additional_ip_headers = $this->instantiated_loggers['SimpleLogger']['instance']->get_event_ip_number_headers( $oneLogRow );

when using this solution:

add_filter( 'simple_history/core_loggers', '__return_empty_array' );
add_action( 'simple_history/add_custom_logger', '...' );
The PHP Error
[18-Jul-2023 11:33:48 UTC] PHP Notice:  Undefined index: SimpleLogger in /wp-content/plugins/simple-history/inc/class-simple-history.php on line 2644
[18-Jul-2023 11:33:48 UTC] PHP Notice:  Trying to access array offset on value of type null in /wp-content/plugins/simple-history/inc/class-simple-history.php on line 2644
[18-Jul-2023 11:33:48 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_event_ip_number_headers() on null in /wp-content/plugins/simple-history/inc/class-simple-history.php:2644
Stack trace:
#0 /wp-content/plugins/simple-history/inc/class-simple-history.php(683): Simple_History\Simple_History->get_log_row_html_output(Object(stdClass), Array)
#1 /wp-includes/class-wp-hook.php(308): Simple_History\Simple_History->api('')
#2 /wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array)
#3 /wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#4 /wp-admin/admin-ajax.php(188): do_action('wp_ajax_simple_...')
#5 {main in /wp-content/plugins/simple-history/inc/class-simple-history.php on line 2644

error is solved when allowing Simple_Logger to register:

add_filter( 'simple_history/core_loggers', fn() => [
    Loggers\Simple_Logger::class,
]);

The custom logger in this case is a very simple class that has one $this->info_message(). Error is not raised if the logs have been just cleared but immediately appears when adding a new log with the custom logger.

image

@bonny
Copy link
Owner

bonny commented Jul 20, 2023

Ah, I never tried the plugin with no core loggers loaded at all. I will make a test and then a fix so I works in this scenario.

@bonny bonny reopened this Jul 20, 2023
@bonny bonny self-assigned this Jul 20, 2023
@bonny bonny closed this as completed in 360e258 Jul 28, 2023
@bonny
Copy link
Owner

bonny commented Jul 28, 2023

@aanndryyyy This will hopefully be fixed in the next version. If you want to test already you can test with the main branch here.

@bonny
Copy link
Owner

bonny commented Aug 9, 2023

@aanndryyyy Please check if the latest version (4.4.0) of Simple History solved this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants