• Resolved flynsarmy

    (@flynsarmy)


    At the bottom of /threewp-broadcast/src/sdk/wordpress/traits/debug.php::debug() you have checks for $this->debugging_to_browser() and $this->debugging_to_file() which is great however I’d like to debug to my own preferred logging solution.

    Would it be possible to add an action here for use in third party plugins? I’d like the log text string, current post and where it’s being broadcasted to etc. This way I could for example display all broadcast related debug info in a metabox on my post edit page along with my other logs.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author edward_plainview

    (@edward_plainview)

    I’m sure something can be arranged. I’ll see what I can do tomorrow.

    Plugin Author edward_plainview

    (@edward_plainview)

    Step 1: https://plainviewplugins.com/wp-content/uploads/2018/04/threewp-broadcast-43.3.1524479845.27825.debug_text.zip

    Step 2:

    add_filter( 'ThreeWP_Broadcast_debug_text', 'my_ThreeWP_Broadcast_debug_text' );
    function my_ThreeWP_Broadcast_debug_text( $data )
    {
    	$data->timestamp .= 'ts';
    	$data->class_name .= 'cn';
    	$data->text .= 'text';
    	return $data;
    }
    

    How does that work for ya?

    Thread Starter flynsarmy

    (@flynsarmy)

    This is a good start however I’m not able to associate the logs with anything. For example I’m logging:

    ThreeWP_Broadcast: Post_Queue: Broadcast complete. Return value: 1
    ThreeWP_Broadcast: Calculator. Time saved for this post: 9 seconds
    ThreeWP_Broadcast: Calculator. Post update only costs 10 %.
    ThreeWP_Broadcast: Calculator. 0 attachments @ 60 seconds = 0 seconds
    etc

    however I don’t have a post ID, a blog ID or any sort of context for the logs.

    Would it be possible for the debug method to take an optional second argument containing context such as the info above, the broadcast_data object or basically anything and everything possible? It’d make life a lot easier when debugging.

    Thread Starter flynsarmy

    (@flynsarmy)

    I kind of got it working with the following code. This will log the entire broadcast process from start to finish into a single DB row – far more efficient than 30+ DB calls at once.

    add_action('save_post', function($post_id) {
        new DebugBroadcast($post_id, get_current_blog_id());
    }, 1);
    
    class DebugBroadcast
    {
        protected $post_id = 0;
        protected $blog_id = 0;
        protected $logs = [];
    
        public function __construct($post_id, $blog_id)
        {
            $this->post_id = $post_id;
            $this->blog_id = $blog_id;
            add_action('threewp_broadcast_broadcasting_finished', [$this, 'broadcasting_finished']);
            add_filter('ThreeWP_Broadcast_debug_text', [$this, 'debug_text']);
        }
    
        public function broadcasting_finished()
        {
            $this->log();
        }
    
        public function debug_text($data)
        {
            $this->logs[] = sprintf( "%s %s: %s", $data->timestamp, $data->class_name, $data->text );
    
            return $data;
        }
    
        protected function log()
        {
            if ( !$this->logs )
                return;
    
            global $wpdb;
    
            $text = implode("\n", $this->logs);
    
            $wpdb->insert('my_logs_table', [
                'post_id' => $this->post_id,
                'blog_id' => $this->blog_id,
                'message' => $text,
            ]);
    
            $this->logs = [];
        }
    }

    Ideally I’d still like my above suggestion implemented though to allow for picking and choosing of what to log rather than just logging everything the way I’m doing here. There’s a lot of useless info that I really don’t need to log.

    Plugin Author edward_plainview

    (@edward_plainview)

    It’s difficult to get any more context to the debug calls, since that would require rewriting every single debug() call.

    The best you can do it so get the broadcasting_data yourself by requesting it from Broadcast.

    reset( ThreeWP_Broadcast()->broadcasting )

    or perhaps last(), since broadcasting is an array of broadcasting_data objects. From there you can extract the current post ID and link data and what not.

    Thread Starter flynsarmy

    (@flynsarmy)

    No worries, this should be enough for now. Thanks for your help on this and great response time!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Request: Custom Debug action’ is closed to new replies.