Created
          December 12, 2014 13:33 
        
      - 
      
- 
        Save levacic/f7f7e8c152cf24e39952 to your computer and use it in GitHub Desktop. 
    ExtraDataProcessor for Monolog
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | $extraDataProcessor = new Creitive\Monolog\Processor\ExtraDataProcessor([ | |
| 'httpMethod' => Request::method(), | |
| 'url' => Request::url(), | |
| 'environment' => App::environment(), | |
| 'console' => App::runningInConsole(), | |
| 'clientIps' => Request::getClientIps(), | |
| 'referrer' => Request::server('HTTP_REFERER'), | |
| 'userAgent' => Request::header('User-Agent'), | |
| ]); | |
| $monolog->pushProcessor($extraDataProcessor); | 
  
    
      This file contains hidden or 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
    
  
  
    
  | <?php | |
| namespace Creitive\Monolog\Processor; | |
| /** | |
| * Injects arbitrary extra data in all records. | |
| * | |
| * @author Miloš Levačić <[email protected]> | |
| */ | |
| class ExtraDataProcessor | |
| { | |
| /** | |
| * @var array | |
| */ | |
| protected $extraData = array(); | |
| /** | |
| * @param array $extraData Extra data to be added | |
| */ | |
| public function __construct(array $extraData = array()) | |
| { | |
| $this->extraData = $extraData; | |
| } | |
| /** | |
| * @param array $record | |
| * @return array | |
| */ | |
| public function __invoke(array $record) | |
| { | |
| $record['extra'] = $this->appendExtraFields($record['extra']); | |
| return $record; | |
| } | |
| /** | |
| * @param array $extra | |
| * @return array | |
| */ | |
| private function appendExtraFields(array $extra) | |
| { | |
| foreach ($this->extraData as $key => $value) { | |
| $extra[$key] = $value; | |
| } | |
| return $extra; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment