[GH-ISSUE #1039] Unable to extend SlackWebhookHandler #424

Closed
opened 2026-03-04 02:14:55 +03:00 by kerem · 1 comment
Owner

Originally created by @JayChandler on GitHub (Aug 11, 2017).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1039

I need to extend the slack record before it gets posted via curl. My idea was to subclass SlackWebhookHandler and overwrite the write method but unfortunately the properties from the base class are private

The only thing i have is

public function getSlackRecord()
    {
        return $this->slackRecord;
    }

but then there is this!

 /**
     * Slack Webhook token
     * @var string
     */
    private $webhookUrl;

To get this working i would have to copy the whole class into a new file and do my moifications. Thats not very nice.

It would be very nice if the class would have a callback that is called before the JSON serialization happens to have some kind of extensibility.

protected function write(array $record)
    {
        $postData = $this->slackRecord->getSlackData($record);
        // apply callback
        $postData = $this->extendRecord(  $postData );
        $postString = json_encode($postData);

        $ch = curl_init();
        $options = array(
            CURLOPT_URL => $this->webhookUrl,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => array('Content-type: application/json'),
            CURLOPT_POSTFIELDS => $postString
        );
        if (defined('CURLOPT_SAFE_UPLOAD')) {
            $options[CURLOPT_SAFE_UPLOAD] = true;
        }

        curl_setopt_array($ch, $options);

        Curl\Util::execute($ch);
    }
Originally created by @JayChandler on GitHub (Aug 11, 2017). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1039 I need to extend the slack record before it gets posted via curl. My idea was to subclass SlackWebhookHandler and overwrite the `write` method but unfortunately the properties from the base class are **private** The only thing i have is ``` public function getSlackRecord() { return $this->slackRecord; } ``` but then there is this! ``` /** * Slack Webhook token * @var string */ private $webhookUrl; ``` To get this working i would have to copy the whole class into a new file and do my moifications. Thats not very nice. It would be very nice if the class would have a callback that is called before the JSON serialization happens to have some kind of extensibility. ``` protected function write(array $record) { $postData = $this->slackRecord->getSlackData($record); // apply callback $postData = $this->extendRecord( $postData ); $postString = json_encode($postData); $ch = curl_init(); $options = array( CURLOPT_URL => $this->webhookUrl, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_POSTFIELDS => $postString ); if (defined('CURLOPT_SAFE_UPLOAD')) { $options[CURLOPT_SAFE_UPLOAD] = true; } curl_setopt_array($ch, $options); Curl\Util::execute($ch); } ```
kerem closed this issue 2026-03-04 02:14:55 +03:00
Author
Owner

@Seldaek commented on GitHub (Jun 8, 2018):

Fixed by #1088

<!-- gh-comment-id:395724907 --> @Seldaek commented on GitHub (Jun 8, 2018): Fixed by #1088
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#424
No description provided.