2017 © Pedro Peláez
 

library zf2-logger

Zend Framework 2 Logger - request & response log

image

eddiejaoude/zf2-logger

Zend Framework 2 Logger - request & response log

  • Sunday, June 7, 2015
  • by eddiejaoude
  • Repository
  • 3 Watchers
  • 36 Stars
  • 18,910 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 18 Forks
  • 7 Open issues
  • 14 Versions
  • 1 % Grown

The README.md

Build Status Coverage Status Total Downloads Dependency Status Scrutinizer Quality Score, (*1)

DashboardHub Badge, (*2)

EddieJaoude\Zf2Logger

Zend Framework 2 Event Logger.

  • Log incoming Requests & Response data with host name
  • Manually log your application information with priorities (i.e. emerg..debug)
  • Change your logging output via config without changing code
  • Multiple logging outputs (i.e. file(s), stdout, stderr etc)
  • Filter errors to log per environment (i.e production > error, development > debug)
  • Default log information includes (Session Id, Host, IP)

Installation via Composer

Steps

1. Add to composer.

    "require" : {
        "eddiejaoude/zf2-logger" : "0.*"
    }

Update your dependencies php composer.phar update eddiejaoude/zf2-logger, (*3)

2. Copy the configuration file config/module.config.php.dist to config/autoload/zf2Logger.global.php

3. Add module to application config (/config/application.config.php)

   //...
   'modules' => array(
        'EddieJaoude\Zf2Logger',
   ),
   //...

Then you are good to go. Logging READY! All requests & responses will be logged automatically as DEBUG, (*4)


Example usage of manual logging & prority

As the Zend\Log\Logger is returned from the Service call, one can use the methods: * emerg // Emergency: system is unusable * alert // Alert: action must be taken immediately * crit // Critical: critical conditions * err // Error: error conditions * warn // Warning: warning conditions * notice // Notice: normal but significant condition * info // Informational: informational messages * debug // Debug: debug messages, (*5)

    //...
    $serviceLocator->get('EddieJaoude\Zf2Logger')->emerg('Emergency message');
    //...

Use an alias for decoupling

Instead of using EddieJaoude\Zf2Logger in your code, put an Alias in your service manager, therefore allowing you to swap out different logger libraries later on without modifying your code & usage., (*6)

i.e., (*7)

    //...
    'aliases'    => array(
        // alias used, so can be swapped out later without changing any code
        'Logger' => 'EddieJaoude\Zf2Logger'
    ),
    //...

Then your usage in your code becomes..., (*8)

    //...
    $serviceLocator->get('Logger')->emerg('Emergency message');
    //...

Add to default logging parameters

Additional default logging information includes:, (*9)

  • IP
  • Host
  • Session Id

To log more additional default information, use $logger->addCustomExtra($extraArray). Full example below., (*10)

  1. Change the alias to your new service (point 2 below) i.e.
    'aliases' => array(
        // ...
        'Logger' => 'Zf2Logger',
        // ...
    ),
  1. Create your new service
    // ...
    'Zf2Logger' => function($sm) {
        $logger = $sm->get('EddieJaoude\Zf2Logger');
        $logger->addCustomExtra(
            array(
                'host' => !empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'CLI',
            )
        );

        return $logger;
    },
    // ...

Example - built in logging

Each output includes & is prepended with the host - this is especially useful when working with multi layer/tier architecture, i.e. F/E (UI) -> B/E (API). As these can all write to the same output in the stack execution order or alternatively to different outputs., (*11)

Request (priority DEBUG)

    2014-01-09T16:28:23+00:00 DEBUG (7): Array
    (
        [zf2.local] => Array
            (
                [Request] => Zend\Uri\Http Object
                    (
                        [validHostTypes:protected] => 19
                        [user:protected] =>
                        [password:protected] =>
                        [scheme:protected] => http
                        [userInfo:protected] =>
                        [host:protected] => zf2.local
                        [port:protected] =>
                        [path:protected] => /api/user
                        [query:protected] =>
                        [fragment:protected] =>
                    )

            )

    )

Response (priority DEBUG)

    2014-01-09T16:28:24+00:00 DEBUG (7): Array
    (
        [zf2.local] => Array
            (
                [Response] => Array
                    (
                        [statusCode] => 200
                        [content] => {"total":2,"data":[{"id":"12345 ...
                        ...
                    )
            )
    )

Configuration (config)

    return array(
        'EddieJaoude\Zf2Logger' => array(

            // will add the $logger object before the current PHP error handler
            'registerErrorHandler'     => 'true', // errors logged to your writers
            'registerExceptionHandler' => 'true', // exceptions logged to your writers

            // do not log binary responses
            // mime types reference http://www.sitepoint.com/web-foundations/mime-types-complete-list/
            'doNotLog'                 => array(
                'mediaTypes' => array(
                    'application/octet-stream',
                    'image/png',
                    'image/jpeg',
                    'application/pdf'
                ),
            ),

            // multiple zend writer output & zend priority filters
            'writers' => array(
                'standard-file' => array(
                    'adapter'  => '\Zend\Log\Writer\Stream',
                    'options'  => array(
                        'output' => 'data/application.log', // path to file
                    ),
                    // options: EMERG, ALERT, CRIT, ERR, WARN, NOTICE, INFO, DEBUG
                    'filter' => \Zend\Log\Logger::DEBUG,
                    'enabled' => true
                ),
                'tmp-file' => array(
                    'adapter'  => '\Zend\Log\Writer\Stream',
                    'options'  => array(
                        'output' => '/tmp/application-' . $_SERVER['SERVER_NAME'] . '.log', // path to file
                    ),
                    // options: EMERG, ALERT, CRIT, ERR, WARN, NOTICE, INFO, DEBUG
                    'filter' => \Zend\Log\Logger::DEBUG,
                    'enabled' => false
                ),
                'standard-output' => array(
                    'adapter'  => '\Zend\Log\Writer\Stream',
                    'options'  => array(
                        'output' => 'php://output'
                    ),
                    // options: EMERG, ALERT, CRIT, ERR, WARN, NOTICE, INFO, DEBUG
                    'filter' => \Zend\Log\Logger::NOTICE,
                    'enabled' => $_SERVER['APPLICATION_ENV'] == 'development' ? true : false
                ),
                'standard-error' => array(
                    'adapter'  => '\Zend\Log\Writer\Stream',
                    'options'  => array(
                        'output' => 'php://stderr'
                    ),
                    // options: EMERG, ALERT, CRIT, ERR, WARN, NOTICE, INFO, DEBUG
                    'filter' => \Zend\Log\Logger::NOTICE,
                    'enabled' => true
                )
            )
        )
    );


Unit tests

To run unit tests (from root diectory), (*12)

  1. Download Composer
curl -sS https://getcomposer.org/installer | php
  1. Install dependencies
php composer.phar install
  1. Run tests
vendor/bin/phpunit -c tests/phpunit.xml

Example output of Log file

2014-05-08T19:46:43+01:00 DEBUG (7): Array
(
    [zf2.be.local] => Array
        (
            [Request] => Zend\Uri\Http Object
                (
                    [validHostTypes:protected] => 19
                    [user:protected] =>
                    [password:protected] =>
                    [scheme:protected] => http
                    [userInfo:protected] =>
                    [host:protected] => zf2.local
                    [port:protected] => 8080
                    [path:protected] => /api/ddc
                    [query:protected] =>
                    [fragment:protected] =>
                )

        )

)

2014-05-08T19:46:43+01:00 DEBUG (7): Authorisation Check
Role: System Admin
Resource: api-ddc
Method: post
IsAllowed: 1

2014-05-08T19:46:43+01:00 DEBUG (7): Authorisation Check
Role: OPG User
Resource: api-ddc
Method: post
IsAllowed:

2014-05-08T19:46:43+01:00 INFO (6): Import: Starting...
2014-05-08T19:46:43+01:00 INFO (6): Import: Loaded XML (SET.xsd).
2014-05-08T19:46:43+01:00 INFO (6): Import: Found XSD SET.xsd (module/Ddc/src/Ddc/Validator/SET.xsd)
2014-05-08T19:46:43+01:00 INFO (6): Import: Validated XML (SET.xsd).
2014-05-08T19:46:43+01:00 INFO (6): Import: Loaded XML (LPA002.xsd).
2014-05-08T19:46:43+01:00 INFO (6): Import: Found XSD LPA002.xsd (module/Ddc/src/Ddc/Validator/LPA002.xsd)
2014-05-08T19:46:43+01:00 INFO (6): Import: Validated XML (LPA002.xsd).
2014-05-08T19:46:43+01:00 INFO (6): Import: Failed. 'P1 DOB' was not in the expected format d/m/Y H:i:s
2014-05-08T19:46:43+01:00 DEBUG (7): Array
(
    [zf2.local] => Array
        (
            [Response] => Array
                (
                    [statusCode] => 400
                    [content] => {"data":{"success":false},"additionalData":null}
                )

        )

)

What Next...

  • Additional events

Ideas & requirements welcome., (*13)


Contributing

  • Discussions from Ideas & Discussions to Pull Requests
  • Pull requests with Unit tests

Resources

  • Github https://github.com/eddiejaoude/zf2-logger
  • Packagist https://packagist.org/packages/eddiejaoude/zf2-logger
  • Zend Framework 2 Modules http://modules.zendframework.com/eddiejaoude/zf2-logger
  • Travis CI https://travis-ci.org/eddiejaoude/zf2-logger
  • Coveralls https://coveralls.io/r/eddiejaoude/zf2-logger
  • Scrutinizer https://scrutinizer-ci.com/g/eddiejaoude/zf2-logger/

The Versions

07/06 2015

dev-master

9999999-dev

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

17/12 2014

0.5.0-rc2

0.5.0.0-RC2

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

15/08 2014

0.5.0-rc1

0.5.0.0-RC1

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

20/07 2014

dev-multiple-channels

dev-multiple-channels

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

07/06 2014

0.4.1

0.4.1.0

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

10/05 2014

0.4.0-rc2

0.4.0.0-RC2

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

09/05 2014

0.4.0-rc1

0.4.0.0-RC1

Zend Framework 2 Logger - request & response log

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eddie Jaoude

logger log zf2 zend zend framework 2

09/05 2014
09/04 2014
09/04 2014
24/03 2014
19/02 2014
19/02 2014
15/01 2014