KiwiJuicer
KiwiJuicer

Reputation: 1972

ZF2 logging: Adding custom information to the logging output

I'm developing in Zend Framework 2 and would like to extend my logging customised by the request url.

What is the best way to do so. Can I pass the $_REQUEST onBootstrap to the logger and make use of it by overwriting the logger class?

The logging should look like this:

2015-10-05T09:52:49+02:00 CRIT (2): Log Message, Request URL: http://www.website.com/page?field=value&....

The module.config looks as following:

'log' => array(

        'Log\ErrorHandler' => array(
            'writers' => array(
                array(
                    'name' => 'stream',
                    'options' => array(
                        'stream' => 'logs/php.log',
                    )
                )
            ),
        ),

        'Log\ExceptionHandler' => array(
            'writers' => array(
                array(
                    'name' => 'stream',
                    'options' => array(
                        'stream' => 'logs/exception.log',
                    )
                )
            ),
        ),

        'Log\Main' => array(
            'writers' => array(
                array(
                    'name' => 'stream',
                    'options' => array(
                        'stream' => 'logs/main.log',
                    )
                )
            ),
        ),

    )

OR could I hook into all loggers somewhere to add the request to the extra array like you can do when you log something:

$this->getServiceLocator()->get('Log\Main')->crit('Log Message', ['request' => $this->getRequest()]);

Upvotes: 0

Views: 679

Answers (2)

KiwiJuicer
KiwiJuicer

Reputation: 1972

Thank you @Ed209, your answer brought me on the right track.

I've added a new processor and implemented also the ServiceLocatorAwareInterface:

<?php
namespace Your\Namespace\ToProcessor;

use Zend\Log\Processor\ProcessorInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Class Request
 *
 * Processor adds some request information to the logger extra
 *
 */
class RequestInformation implements ServiceLocatorAwareInterface, ProcessorInterface
{
    /**
     * Service Locator
     *
     * @var \Zend\Di\ServiceLocator
     */
    protected $serviceLocator = null;

    /**
     * Processes the given event and adds request information to the extras
     *
     * @param array $event
     * @return array
     */
    public function process(array $event)
    {
        if (!isset($event['extra'])) {
            $event['extra'] = array();
        }

        $request = $this->getServiceLocator()->getServiceLocator()->get('Request');

        if ($request instanceof \Zend\Http\Request) {
            $event['extra']['requestUrl'] = $request->getUriString();
        } elseif ($request instanceof \Zend\Console\Request) {
            $event['extra']['consoleParameters'] = $request->toString();
        }

        return $event;
    }

    /**
     * Set service locator
     *
     * @param ServiceLocatorInterface $serviceLocator
     */
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * Get service locator
     *
     * @return ServiceLocatorInterface
     */
    public function getServiceLocator() {
        return $this->serviceLocator;
    }

}

and changed the configuration of my module.config:

'log' => array(

    'Log\Main' => array(
        'writers' => array(
            array(
                'name' => 'stream',
                'options' => array(
                    'stream' => 'logs/main.log',
                )
            )
        ),
        'processors' => array(
            array(
                'name' => '\PathToProcessor\RequestInformation'
            )
        )
    ),
)

Upvotes: 1

Ed209
Ed209

Reputation: 821

You probably want to add a processor to your log service.

$logger->addProcessor(new LogExtra());

class LogExtra implements ProcessorInterface
{
    public function process(array $event)
    {
        if (!isset($event['extra'])) {
            $event['extra'] = array();
        }

        $event['extra']['request'] = // value you want to log
        return $event;
    }
}

Upvotes: 2

Related Questions