forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Stefan Kleff
committed
Oct 10, 2012
1 parent
19f2be8
commit ae3c072
Showing
8 changed files
with
463 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Log | ||
*/ | ||
|
||
namespace Zend\Log\Processor; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Log | ||
* @subpackage Processor | ||
*/ | ||
class Backtrace implements ProcessorInterface | ||
{ | ||
/** | ||
* Maximum stack level of backtrace (PHP > 5.4.0) | ||
* @var int | ||
*/ | ||
protected $traceLimit = 10; | ||
|
||
/** | ||
* Classes within this namespace in the stack are ignored | ||
* @var string | ||
*/ | ||
protected $ignoredNamespace = 'Zend\\Log'; | ||
|
||
/** | ||
* Adds the origin of the log() call to the event extras | ||
* | ||
* @param array $event event data | ||
* @return array event data | ||
*/ | ||
public function process(array $event) | ||
{ | ||
$trace = $this->getBacktrace(); | ||
|
||
array_shift($trace); // ignore $this->getBacktrace(); | ||
array_shift($trace); // ignore $this->process() | ||
|
||
$i = 0; | ||
while (isset($trace[$i]['class']) && false !== strpos($trace[$i]['class'], $this->ignoredNamespace)) { | ||
$i++; | ||
} | ||
|
||
$origin = array( | ||
'file' => isset($trace[$i-1]['file']) ? $trace[$i-1]['file'] : null, | ||
'line' => isset($trace[$i-1]['line']) ? $trace[$i-1]['line'] : null, | ||
'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null, | ||
'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null, | ||
); | ||
|
||
if(!isset($event['extra'])) { | ||
$event['extra'] = $origin; | ||
} else { | ||
$event['extra'] = array_merge($origin, $event['extra']); | ||
} | ||
|
||
return $event; | ||
} | ||
|
||
/** | ||
* Provide backtrace as slim as posible | ||
* | ||
* @return array: | ||
*/ | ||
protected function getBacktrace() | ||
{ | ||
if (version_compare(PHP_VERSION, '5.4.0') >= 0) { | ||
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->traceLimit); | ||
} | ||
|
||
if (version_compare(PHP_VERSION, '5.3.6') >= 0) { | ||
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); | ||
} | ||
|
||
return debug_backtrace(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Log | ||
*/ | ||
|
||
namespace Zend\Log\Processor; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Log | ||
*/ | ||
interface ProcessorInterface | ||
{ | ||
/** | ||
* Processes a log message before it is given to the writers | ||
* | ||
* @param array $event | ||
* @return WriterInterface | ||
*/ | ||
public function process(array $event); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Log | ||
*/ | ||
|
||
namespace Zend\Log\Processor; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Log | ||
* @subpackage Processor | ||
*/ | ||
class RequestId implements ProcessorInterface | ||
{ | ||
|
||
/** | ||
* Adds a identfier for the request to the log. | ||
* This enables to filter the log for messages belonging to a specific request | ||
* | ||
* @param array $event event data | ||
* @return array event data | ||
*/ | ||
public function process(array $event) | ||
{ | ||
if(!isset($event['extra'])) { | ||
$event['extra'] = array(); | ||
} | ||
|
||
$event['extra']['requestId'] = $this->getIdentifier(); | ||
return $event; | ||
} | ||
|
||
/** | ||
* Provide unique identifier for a request | ||
* | ||
* @return array: | ||
*/ | ||
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']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Log | ||
*/ | ||
|
||
namespace Zend\Log; | ||
|
||
use Zend\ServiceManager\AbstractPluginManager; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Log | ||
*/ | ||
class ProcessorPluginManager extends AbstractPluginManager | ||
{ | ||
/** | ||
* Default set of writers | ||
* | ||
* @var array | ||
*/ | ||
protected $invokableClasses = array( | ||
'backtrace' => 'Zend\Log\Processor\Backtrace', | ||
'requestid' => 'Zend\Log\Processor\RequestId', | ||
); | ||
|
||
/** | ||
* Allow many writers of the same type | ||
* | ||
* @var bool | ||
*/ | ||
protected $shareByDefault = false; | ||
|
||
/** | ||
* Validate the plugin | ||
* | ||
* Checks that the processor loaded is an instance of Processor\ProcessorInterface. | ||
* | ||
* @param mixed $plugin | ||
* @return void | ||
* @throws Exception\InvalidArgumentException if invalid | ||
*/ | ||
public function validatePlugin($plugin) | ||
{ | ||
if ($plugin instanceof Processor\ProcessorInterface) { | ||
// we're okay | ||
return; | ||
} | ||
|
||
throw new Exception\InvalidArgumentException(sprintf( | ||
'Plugin of type %s is invalid; must implement %s\Processor\ProcessorInterface', | ||
(is_object($plugin) ? get_class($plugin) : gettype($plugin)), | ||
__NAMESPACE__ | ||
)); | ||
} | ||
} |
Oops, something went wrong.