[GH-ISSUE #729] SlackHandler doesn’t work #272

Closed
opened 2026-03-04 02:13:40 +03:00 by kerem · 8 comments
Owner

Originally created by @r3nat on GitHub (Feb 8, 2016).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/729

monolog 1.15.0, tried 1.17.1

use Monolog\Handler\SlackHandler;
use Monolog\Logger;

$Logger = new Logger("Whatever");
$Handler = new SlackHandler($token, "@renat");
$Logger->pushHandler($Handler);

$Logger->emerg("Hey, " . date("H:i:s"));

30-70% of messages are lost without error.
Adding fread($this->resource, 8192); to the end of SocketHandler->writeToSocket fixes the problem, although it looks like dirty hack.

Originally created by @r3nat on GitHub (Feb 8, 2016). Original GitHub issue: https://github.com/Seldaek/monolog/issues/729 monolog 1.15.0, tried 1.17.1 ``` use Monolog\Handler\SlackHandler; use Monolog\Logger; $Logger = new Logger("Whatever"); $Handler = new SlackHandler($token, "@renat"); $Logger->pushHandler($Handler); $Logger->emerg("Hey, " . date("H:i:s")); ``` 30-70% of messages are lost without error. Adding `fread($this->resource, 8192);` to the end of `SocketHandler->writeToSocket` fixes the problem, although it looks like dirty hack.
kerem 2026-03-04 02:13:40 +03:00
  • closed this issue
  • added the
    Bug
    label
Author
Owner

@Aliance commented on GitHub (Feb 8, 2016):

+1

<!-- gh-comment-id:181342070 --> @Aliance commented on GitHub (Feb 8, 2016): +1
Author
Owner

@GeeH commented on GitHub (Feb 18, 2016):

+1 :trollface:

<!-- gh-comment-id:185854540 --> @GeeH commented on GitHub (Feb 18, 2016): +1 :trollface:
Author
Owner

@pprishchepa commented on GitHub (Mar 1, 2016):

+1

<!-- gh-comment-id:190600919 --> @pprishchepa commented on GitHub (Mar 1, 2016): +1
Author
Owner

@stloyd commented on GitHub (Mar 1, 2016):

Unfortunatelly can confirm that if amount of logs is quite big, slack handler loses those messages.

<!-- gh-comment-id:190601103 --> @stloyd commented on GitHub (Mar 1, 2016): Unfortunatelly can confirm that if amount of logs is quite big, slack handler loses those messages.
Author
Owner

@pprishchepa commented on GitHub (Mar 1, 2016):

@stloyd In my case it is not correlate with amount of logs. It's about 1 message per 10 seconds.

@r3nat Adding fread($this->resource, 8192); helps me too, thanks.

<!-- gh-comment-id:190607384 --> @pprishchepa commented on GitHub (Mar 1, 2016): @stloyd In my case it is not correlate with amount of logs. It's about 1 message per 10 seconds. @r3nat `Adding fread($this->resource, 8192);` helps me too, thanks.
Author
Owner

@Seldaek commented on GitHub (Mar 1, 2016):

I don't really see what the fread does there.. Can someone try and explain? Do you add it at the very end or does it also work around SocketHandler line 294 just after the fwrite?

Also could you maybe var_dump what the fread() returns to see what is being sent back by slack that we need to acknowledge for it to not drop messages?

Another fix would be to add a fread() inside SlackHandler::write before closing the handler I guess. I don't really want to apply this blindly to all SocketHandler derivatives as it might cause issues.

<!-- gh-comment-id:190795276 --> @Seldaek commented on GitHub (Mar 1, 2016): I don't really see what the fread does there.. Can someone try and explain? Do you add it at the very end or does it also work around SocketHandler line 294 just after the fwrite? Also could you maybe var_dump what the fread() returns to see what is being sent back by slack that we need to acknowledge for it to not drop messages? Another fix would be to add a fread() inside SlackHandler::write before closing the handler I guess. I don't really want to apply this blindly to _all_ SocketHandler derivatives as it might cause issues.
Author
Owner

@r3nat commented on GitHub (Mar 2, 2016):

I think if socket closes too early, than slack will not process request.
Adding usleep(10000) instead of fread does the trick too.

Result of fread:

string(1137) "HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 356
Connection: keep-alive
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Security-Policy: referrer no-referrer;
Date: Wed, 02 Mar 2016 17:48:54 GMT
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Pragma: no-cache
Server: Apache
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Vary: Accept-Encoding
X-Accepted-OAuth-Scopes: chat:write:bot,post
X-Content-Type-Options: nosniff
X-OAuth-Scopes: identify,read,post,client,admin
X-XSS-Protection: 0
X-Cache: Miss from cloudfront
Via: 1.1 1228e077cd8e3d045c8d3f88beaa5abd.cloudfront.net (CloudFront)
X-Amz-Cf-Id: daiAXEapRDWtmBEVEaIuuuCrocmdZL6Z6ZE_a8TIMRVX8Y6ZtPCGYQ==

{"ok":true,"channel":"D02AF3J3X","ts":"1456940934.000002","message":{"text":"","username":"Monolog","attachments":[{"fallback":"Hey, 20:48:54","id":1,"color":"d00000","fields":[{"title":"Message","value":"Hey, 20:48:54","short":false},{"title":"Level","value":"EMERGENCY","short":true}]}],"type":"message","subtype":"bot_message","ts":"1456940934.000002"}}"
<!-- gh-comment-id:191350669 --> @r3nat commented on GitHub (Mar 2, 2016): I think if socket closes too early, than slack will not process request. Adding `usleep(10000)` instead of `fread` does the trick too. Result of `fread`: ``` string(1137) "HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Content-Length: 356 Connection: keep-alive Access-Control-Allow-Origin: * Cache-Control: private, no-cache, no-store, must-revalidate Content-Security-Policy: referrer no-referrer; Date: Wed, 02 Mar 2016 17:48:54 GMT Expires: Mon, 26 Jul 1997 05:00:00 GMT Pragma: no-cache Server: Apache Strict-Transport-Security: max-age=31536000; includeSubDomains; preload Vary: Accept-Encoding X-Accepted-OAuth-Scopes: chat:write:bot,post X-Content-Type-Options: nosniff X-OAuth-Scopes: identify,read,post,client,admin X-XSS-Protection: 0 X-Cache: Miss from cloudfront Via: 1.1 1228e077cd8e3d045c8d3f88beaa5abd.cloudfront.net (CloudFront) X-Amz-Cf-Id: daiAXEapRDWtmBEVEaIuuuCrocmdZL6Z6ZE_a8TIMRVX8Y6ZtPCGYQ== {"ok":true,"channel":"D02AF3J3X","ts":"1456940934.000002","message":{"text":"","username":"Monolog","attachments":[{"fallback":"Hey, 20:48:54","id":1,"color":"d00000","fields":[{"title":"Message","value":"Hey, 20:48:54","short":false},{"title":"Level","value":"EMERGENCY","short":true}]}],"type":"message","subtype":"bot_message","ts":"1456940934.000002"}}" ```
Author
Owner

@lesaff commented on GitHub (Mar 3, 2016):

To confirm @r3nat, this works 👍
SlackHandler.php, line 236

    /**
     * {@inheritdoc}
     *
     * @param array $record
     */
    protected function write(array $record)
    {
        parent::write($record);
        usleep(10000); // Delay before socket close
        $this->closeSocket();
    }
<!-- gh-comment-id:191782551 --> @lesaff commented on GitHub (Mar 3, 2016): To confirm @r3nat, this works :+1: SlackHandler.php, line 236 ``` /** * {@inheritdoc} * * @param array $record */ protected function write(array $record) { parent::write($record); usleep(10000); // Delay before socket close $this->closeSocket(); } ```
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#272
No description provided.