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

Composite logger only log on last child when they are created at the same microtime() #12

Closed
Worst45 opened this issue Apr 8, 2016 · 1 comment
Assignees

Comments

@Worst45
Copy link

Worst45 commented Apr 8, 2016

Hello,

I see you use microtime() to get unique ids for loggers... this is not sufficient on recent machines anymore, as many PHP commands may be executed at the same microsecond...
(on constructors: $this->_id = md5(microtime()); )

I wondered why my composite logger only logged on my last child... after digging a little, I saw that each child was stored in an array with its id for key... which is the same for 2 loggers created at the same time:

// This only log on last added child:
$logger = Log::singleton('composite');
$logConsole = Log::factory('console', '', 'TEST');
$logFile = Log::factory('file', '/tmp/log', 'TEST');
$logger->addChild($logConsole);
$logger->addChild($logFile);
$logger->info('log me');

// This log on all children
$logger = Log::singleton('composite');
$logConsole = Log::factory('console', '', 'TEST');
sleep(1);   // Wait!
$logFile = Log::factory('file', '/tmp/log', 'TEST');
$logger->addChild($logConsole);
$logger->addChild($logFile);
$logger->info('log me');

A quick fix should be to add a random number after the microseconds on _id declaration, for example like this:

$this->_id = md5(microtime().rand());

Best regards,

@jparise jparise self-assigned this Apr 8, 2016
@jparise
Copy link
Member

jparise commented Apr 8, 2016

Yes, that would definitely address the problem.

But as I look over the code, I can't remember the specific rationale for assigning each instance a unique identifier in the first place (rather than just storing direct references to the objects themselves).

@jparise jparise closed this as completed in 2076365 Apr 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants