Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save levacic/f7f7e8c152cf24e39952 to your computer and use it in GitHub Desktop.
Save levacic/f7f7e8c152cf24e39952 to your computer and use it in GitHub Desktop.

Revisions

  1. levacic created this gist Dec 12, 2014.
    48 changes: 48 additions & 0 deletions Creitive_Monolog_Processor_ExtraDataProcessor.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?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;
    }
    }
    11 changes: 11 additions & 0 deletions bootstrap.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    $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);