Added ability to add additional information to the logs via processors. #2727

Closed
wants to merge 4 commits into
from

Projects

None yet

3 participants

Contributor

With processors logs can be enhanced with additional information.
They are called from the logger before the event is passed to the writers and have access to the event array to add any kind of information.

I included two processors in this PR

Backtrace processor to include backtraces in the logs.

$logger->addProcessor(new Processors\Backtrace());

will add backtrace information about the class which had triggered the logging. Result

$event = array(
    [...]
    'extras' => array(
        [...]
        'file' => 'SomeFile.php',
        'line' => 1337,
        'class' => 'Foo\MyClass',
        'function' => 'myMethod'
    )
);

Requestid processor to include a unique id per request in the logs.

$logger->addProcessor(new Processors\RequestId());

Often it is useful to know which log entries belong to a specific request to filter the logfile later on. This Processor generates a md5 hash based on request time and remote address and add this information to the log event. (Inspired by Ben Waines talk on PHPNW https://joind.in/7003)

$event = array(
    [...]
    'extras' => array(
        [...]
        'requestId' => 'SomeMd5Hash'
    ),
);
stefankleff added some commits Oct 10, 2012
@stefankleff stefankleff Added ability to add additional information to the logs via processors.
Added backtrace processor to include backtraces in the logs.
Added requestid processor to include a unique id per request in the logs.
ae3c072
@stefankleff stefankleff Test CS 4a5af10
@blanchonvincent blanchonvincent commented on an outdated diff Oct 10, 2012
library/Zend/Log/Processor/RequestId.php
+ protected function getIdentifier()
+ {
+ $requestTime = (version_compare(PHP_VERSION, '5.4.0') >= 0) ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
+ $remoteAddr = $this->isCli() ? 'local' : $_SERVER['REMOTE_ADDR'];
+
+ return md5($requestTime . $remoteAddr);
+ }
+
+ /**
+ * Check if PHP is run from command line
+ *
+ * @return boolean true if php is run from command line
+ */
+ protected function isCli()
+ {
+ return php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']);
blanchonvincent
blanchonvincent Oct 10, 2012 Contributor

Why not just "php_sapi_name() == 'cli'" ? Why don't use "Console::isConsole()"

Owner

Just a quick question: how does this vary from log formatters semantically? (I'm not against the idea, I just want to make sure end-users will understand the differences clearly.)

Contributor

@blanchonvincent afaik because you have to check that if you use php-cli via cgi/apache or cgi-fast. But doesn't matter: I'm going to use Console::isConsole. I was not aware of that. Thank you!

@weierophinney Semantically the difference is that processors add information while formatters consumes (and formats) them. In general this allows you to combine multiple information resources without writing new formaters for each information. Like SimpleBacktraceFormatter, SimpleRequestIdFormatter, SimpleBacktraceRequestIdFormatter.

Contributor

@stefankleff ok :) With CLI, $_SERVER['REMOTE_ADDR'] will be always empty no ? verification on $_SERVER['REMOTE_ADDR'] is useless ?

Contributor

@blanchonvincent I'm not 100% sure about that - therefore I tested both ;)

Contributor

Current build fails because of some failed captcha test not related to this PR.
btw if somebody knows a better hashing function for RequestId::getIdentifier() let me know!

@weierophinney weierophinney added a commit that referenced this pull request Oct 30, 2012
@weierophinney weierophinney [#2727] CS review
- indentation
- trailing whitespace
- object calisthenics
8635f0a
@weierophinney weierophinney added a commit that referenced this pull request Oct 30, 2012
@weierophinney weierophinney [#2727] CS review
- Trailing whitespace
- Cache generated value, as it will not change in lifetime of request
602858d
@weierophinney weierophinney added a commit to zendframework/zend-log that referenced this pull request May 15, 2015
@weierophinney weierophinney [zendframework/zendframework#2727] CS review
- indentation
- trailing whitespace
- object calisthenics
1771985
@weierophinney weierophinney added a commit to zendframework/zend-log that referenced this pull request May 15, 2015
@weierophinney weierophinney [zendframework/zendframework#2727] CS review
- Trailing whitespace
- Cache generated value, as it will not change in lifetime of request
1fe274e
@weierophinney weierophinney added a commit to zendframework/zend-log that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge branch 'feature/log-processors' into develop 63be611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment