From bad76ce2a6433f7f985d682c1ec451dd54bcc74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mo=CC=88nch?= Date: Mon, 25 Feb 2013 15:42:26 +0100 Subject: [PATCH] Added a request token processor to detect log messages per request --- README.mdown | 1 + .../Processor/RequestTokenProcessor.php | 36 +++++++++++++++++++ .../Processor/RequestTokenProcessorTest.php | 27 ++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/Monolog/Processor/RequestTokenProcessor.php create mode 100644 tests/Monolog/Processor/RequestTokenProcessorTest.php diff --git a/README.mdown b/README.mdown index e7494dc7..7737f12f 100644 --- a/README.mdown +++ b/README.mdown @@ -184,6 +184,7 @@ Processors - _MemoryUsageProcessor_: Adds the current memory usage to a log record. - _MemoryPeakUsageProcessor_: Adds the peak memory usage to a log record. - _ProcessIdProcessor_: Adds the process id to a log record. +- _RequestTokenProcessor_: Adds a request token to a log record. About ===== diff --git a/src/Monolog/Processor/RequestTokenProcessor.php b/src/Monolog/Processor/RequestTokenProcessor.php new file mode 100644 index 00000000..179f6985 --- /dev/null +++ b/src/Monolog/Processor/RequestTokenProcessor.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Processor; + +/** + * Adds a request token into records + * + * @author Simon Mönch + */ +class RequestTokenProcessor +{ + private static $requestToken; + + public function __construct() + { + if (null === self::$requestToken) { + self::$requestToken = substr(hash('md5', uniqid('', true)), 0, 7); + } + } + + public function __invoke(array $record) + { + $record['extra']['request_token'] = self::$requestToken; + + return $record; + } +} \ No newline at end of file diff --git a/tests/Monolog/Processor/RequestTokenProcessorTest.php b/tests/Monolog/Processor/RequestTokenProcessorTest.php new file mode 100644 index 00000000..8f7fc75c --- /dev/null +++ b/tests/Monolog/Processor/RequestTokenProcessorTest.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Processor; + +use Monolog\TestCase; + +class RequestTokenProcessorTest extends TestCase +{ + /** + * @covers Monolog\Processor\RequestTokenProcessor::__invoke + */ + public function testProcessor() + { + $processor = new RequestTokenProcessor(); + $record = $processor($this->getRecord()); + $this->assertArrayHasKey('request_token', $record['extra']); + } +}