Skip to content

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

@Worst45

Description

@Worst45

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,

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions