[GH-ISSUE #276] Stream Handler #89

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

Originally created by @harikt on GitHub (Nov 22, 2013).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/276

Hi @Seldaek ,

I was running StreamHandler inside a Gearman Job server and it seems it could not log all the errors.

Probably need to look another handler or is this a bug ?

Originally created by @harikt on GitHub (Nov 22, 2013). Original GitHub issue: https://github.com/Seldaek/monolog/issues/276 Hi @Seldaek , I was running `StreamHandler` inside a Gearman Job server and it seems it could not log all the errors. Probably need to look another handler or is this a bug ?
kerem closed this issue 2026-03-04 02:12:05 +03:00
Author
Owner

@Seldaek commented on GitHub (Nov 22, 2013):

I don't see why it couldn't.. Without more details I can't really help you.

<!-- gh-comment-id:29066065 --> @Seldaek commented on GitHub (Nov 22, 2013): I don't see why it couldn't.. Without more details I can't really help you.
Author
Owner

@harikt commented on GitHub (Nov 22, 2013):

Hey @Seldaek ,

Thanks for the quick reply .

$logger = new Monolog\Logger('fetch');
$stream = new StreamHandler(__DIR__ . '/logs/somelog.log', Logger::DEBUG);
$logger->pushHandler($stream);
$logger->addInfo("I am doing something");

and I have around 5 to 10 gearman clients running.

<!-- gh-comment-id:29066820 --> @harikt commented on GitHub (Nov 22, 2013): Hey @Seldaek , Thanks for the quick reply . ``` php $logger = new Monolog\Logger('fetch'); $stream = new StreamHandler(__DIR__ . '/logs/somelog.log', Logger::DEBUG); $logger->pushHandler($stream); $logger->addInfo("I am doing something"); ``` and I have around 5 to 10 gearman clients running.
Author
Owner

@harikt commented on GitHub (Nov 22, 2013):

probably http://us3.php.net/file_put_contents will be better I guess. Anything else I could share ?

<!-- gh-comment-id:29066880 --> @harikt commented on GitHub (Nov 22, 2013): probably http://us3.php.net/file_put_contents will be better I guess. Anything else I could share ?
Author
Owner

@Seldaek commented on GitHub (Nov 22, 2013):

It should be no problem since the StreamHandler opens all logs with fopen(..., 'a') it will always append to the end.

You can try with:

<?php
$a = fopen('a.log', 'a');
$b = fopen('a.log', 'a');

fwrite($a, 'first'.PHP_EOL);
fwrite($b, 'second'.PHP_EOL);
fwrite($a, 'third'.PHP_EOL);

You end up with a.log that has the three lines we wrote, even with two handles open on the same file.

<!-- gh-comment-id:29069102 --> @Seldaek commented on GitHub (Nov 22, 2013): It should be no problem since the StreamHandler opens all logs with `fopen(..., 'a')` it will always append to the end. You can try with: ``` php <?php $a = fopen('a.log', 'a'); $b = fopen('a.log', 'a'); fwrite($a, 'first'.PHP_EOL); fwrite($b, 'second'.PHP_EOL); fwrite($a, 'third'.PHP_EOL); ``` You end up with a.log that has the three lines we wrote, even with two handles open on the same file.
Author
Owner

@harikt commented on GitHub (Nov 22, 2013):

I am trying in gearman worker. Do you see an overlapping of the items ?

<!-- gh-comment-id:29070893 --> @harikt commented on GitHub (Nov 22, 2013): I am trying in gearman worker. Do you see an overlapping of the items ?
Author
Owner

@Seldaek commented on GitHub (Nov 22, 2013):

I don't have a gearman setup but no I don't see any overlap, well multiple requests can be interlaced but every line is a clean write from one request.

<!-- gh-comment-id:29071525 --> @Seldaek commented on GitHub (Nov 22, 2013): I don't have a gearman setup but no I don't see any overlap, well multiple requests can be interlaced but every line is a clean write from one request.
Author
Owner

@harikt commented on GitHub (Nov 22, 2013):

:( bad time .

<!-- gh-comment-id:29080197 --> @harikt commented on GitHub (Nov 22, 2013): :( bad time .
Author
Owner

@harikt commented on GitHub (Dec 2, 2013):

Tested with client and worker . Seems something else is wrong. So closing the issue.

client.php

<?php
$client = new GearmanClient();
$client->addServer("127.0.0.1", "4730");
$client->setCompleteCallback(function ($task) {                    
    $data = $task->data();
    echo "task completed " . PHP_EOL;
});
for ($i = 0; $i < 2000; $i++) {
    $payload = array(
        'i' => $i
    );
    $tasks[] = $client->addTask('myworker', json_encode($payload));
}
$client->runTasks();

worker.php

<?php
require_once __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$worker= new GearmanWorker();
$worker->addServer("127.0.0.1", "4730");
$worker->addFunction("myworker", "something");
while ($worker->work());

function something($job) 
{      
    $payload = json_decode($job->workload());    
    // Create the logger
    $logger = new Monolog\Logger('fetch');
    $stream = new StreamHandler(__DIR__ . '/logs/error.log', Logger::DEBUG);
    $logger->pushHandler($stream);
    $logger->addInfo($payload->i);
    return json_encode($payload);
}
<!-- gh-comment-id:29599438 --> @harikt commented on GitHub (Dec 2, 2013): Tested with client and worker . Seems something else is wrong. So closing the issue. `client.php` ``` php <?php $client = new GearmanClient(); $client->addServer("127.0.0.1", "4730"); $client->setCompleteCallback(function ($task) { $data = $task->data(); echo "task completed " . PHP_EOL; }); for ($i = 0; $i < 2000; $i++) { $payload = array( 'i' => $i ); $tasks[] = $client->addTask('myworker', json_encode($payload)); } $client->runTasks(); ``` `worker.php` ``` php <?php require_once __DIR__ . '/vendor/autoload.php'; use Monolog\Logger; use Monolog\Handler\StreamHandler; $worker= new GearmanWorker(); $worker->addServer("127.0.0.1", "4730"); $worker->addFunction("myworker", "something"); while ($worker->work()); function something($job) { $payload = json_decode($job->workload()); // Create the logger $logger = new Monolog\Logger('fetch'); $stream = new StreamHandler(__DIR__ . '/logs/error.log', Logger::DEBUG); $logger->pushHandler($stream); $logger->addInfo($payload->i); return json_encode($payload); } ```
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#89
No description provided.