2017 © Pedro Peláez
 

library acplolog

AcploLog provides some log utility your ZF2 application

image

acplo/acplolog

AcploLog provides some log utility your ZF2 application

  • Monday, May 4, 2015
  • by abelclopes
  • Repository
  • 2 Watchers
  • 1 Stars
  • 39 Installations
  • PHP
  • 1 Dependents
  • 2 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

AcploLog

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

Introduction

This module provides some useful log classes:, (*2)

  • ErrorLogger = PHP error and Exception
  • EntityLogger = Doctrine ORM Entity
  • SqlLogger = Doctrine DBAL SQL
  • StaticLogger = "Shortcut" to a generic file logger. Can be attached to the Z-Ray in Zend Server (NEW)

Requirements

This module only requires zendframework 2 framework.zend.com., (*3)

Instalation

Instalation can be done with composer ou manually, (*4)

Installation with composer

For composer documentation, please refer to getcomposer.org., (*5)

  1. Enter your project directory
  2. Create or edit your composer.json file with following contents:, (*6)

    json { "minimum-stability": "dev", "require": { "acplo/acplolog": "1.*" } }, (*7)

  3. Run php composer.phar install
  4. Open my/project/directory/config/application.config.php and add AcploLog to your modules, (*8)

    Usually insert as the first module to enable catch errors and exceptions in all modules., (*9)

    <?php
    return array(
        'modules' => array(
            'AcploLog',
            'Application'
        ),
        'module_listener_options' => array(
            'config_glob_paths'    => array(
                'config/autoload/{,*.}{global,local}.php',
            ),
            'module_paths' => array(
                './module',
                './vendor',
            ),
        ),
    );
    

Installation without composer

  1. Clone this module AcploLog to your vendor directory
  2. Enable it in your config/application.config.php like the step 4 in the previous section.

Usage

To change the options, copy the file acplolog.global.php.dist to your config/autoload/ , rename it to acplolog.global.php and change the default options., (*10)

ErrorLogger

To enable the ErrorLogger just add the registerHandlers inside your public/index.php, (*11)

chdir(dirname(__DIR__));

require 'init_autoloader.php';

AcploLog\Log\ErrorLogger::registerHandlers();

Zend\Mvc\Application::init(require 'config/application.config.php')->run();

You can use the logger with your phpunit tests. Just call it in your bootstrap file just after the autoload is created:, (*12)

AcploLog\Log\ErrorLogger::registerHandlers();

Output examples

PHP Error
2012-10-30T17:58:10-02:00 ERR (3): Error: Call to a member function format() on a non-object in <filename> on line <line>
Exception
2012-11-01T09:23:53-02:00 ERR (3): Exception- An exception was raised while creating "Application\Service\Test"; no instance returned in <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php in line 733.
Previous: "data/logs2/erros.log" cannot be opened with mode "a" in <dir>/vendor/zendframework/zendframework/library/Zend/Log/Writer/Stream.php in line 87.
Previous: fopen(data/logs2/erros.log): failed to open stream: No such file or directory in <dir>/vendor/zendframework/zendframework/library/Zend/Log/Writer/Stream.php in line 84.
Trace:
#0 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(843): Zend\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'teste', 'Application\Service\Tes...')
#1 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(487): Zend\ServiceManager\ServiceManager->createFromFactory('teste', 'Application\Service\Tes...')
#2 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(442): Zend\ServiceManager\ServiceManager->create(Array)
#3 <dir>/src/Application/Module.php(29): Zend\ServiceManager\ServiceManager->get('Application\Service\Tes...')
#4 [internal function]: Application\Module->onBootstrap(Object(Zend\Mvc\MvcEvent))
#5 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#6 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(208): Zend\EventManager\EventManager->triggerListeners('bootstrap', Object(Zend\Mvc\MvcEvent), Array)
#7 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(146): Zend\EventManager\EventManager->trigger('bootstrap', Object(Zend\Mvc\MvcEvent))
#8 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(243): Zend\Mvc\Application->bootstrap()
#9 <dir>/public/index.php(23): Zend\Mvc\Application::init(Array)
#10 {main}

The default logfile is data/log/error.log, (*13)

EntityLogger

The first usage is to dump a Doctrine Entity to the screen, either HTML or console., (*14)

To do that just call a static function:, (*15)

echo \AcploLog\Log\EntityLogger::dump($entity);

and it will print:, (*16)

stdClass Object
(
    [__CLASS__] => User\Entity\User
    [name] => Admin
    [email] => admin@foo.com
    [password] => $2y$14$4n/JYSM7ZtSaZpg1/PgFZefzoblrmaMmMZga.nhf7TZNAd
    [id] => 1
    [created] => DateTime
)

The "dump" function provides two more arguments:, (*17)

public static function dump($entity, $maxDepth = 1, $toHtml = true)

The second argument indicates how depth will be the dump (other classes as properties, including collections) with default to 1 and the third if the output will be HTML friendly (encloses the output in a "pre" tag) or false for a console version with a default to true., (*18)

echo \AcploLog\Log\EntityLogger::dump($entity, 2);

and it will print:, (*19)

stdClass Object
(
    [__CLASS__] => User\Entity\User
    [name] => Admin
    [email] => admin@foo.com
    [password] => $2y$14$4n/JYSM7ZtSaZpg1/PgFZefzoblrmaMmMZga.nhf7TZNAd
    [id] => 1
    [access] => Array
        (
            [0] => User\Entity\Access
            [1] => User\Entity\Access
            [2] => User\Entity\Access
        )
    [created] => stdClass Object
        (
            [__CLASS__] => DateTime
            [date] => 2014-09-28T07:06:29-03:00
            [timezone] => America/Sao_Paulo
        )
)
echo \AcploLog\Log\EntityLogger::dump($entity, 3);

and it will print:, (*20)

stdClass Object
(
    [__CLASS__] => User\Entity\User
    [name] => Admin
    [email] => admin@foo.com
    [password] => $2y$14$4n/JYSM7ZtSaZpg1/PgFZefzoblrmaMmMZga.nhf7TZNAd
    [id] => 1
    [access] => Array
        (
            [0] => stdClass Object
                (
                    [__CLASS__] => User\Entity\Access
                    [ip] => 10.1.1.2
                    [agent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/32.0
                    [id] => 1
                    [created] => DateTime
                    [updated] => DateTime
                )

            [1] => stdClass Object
                (
                    [__CLASS__] => User\Entity\Access
                    [ip] => 10.1.1.3
                    [agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/6.1.3 Safari/537.75.14
                    [id] => 2
                    [created] => DateTime
                    [updated] => DateTime
                )
        )
    [created] => stdClass Object
        (
            [__CLASS__] => DateTime
            [date] => 2014-09-28T07:06:29-03:00
            [timezone] => America/Sao_Paulo
        )
)

The second usage of this class is to save database operations generated by your entities., (*21)

ATTENTION: This logger depends on DoctrineORMModule. Since its usage is optional, i did not put this requirement inside the composer.json, (*22)

To enable this logger, register inside your doctrine's config (e.g. config/autoload/global.php), (*23)

namespace App;
return array(
    // Doctrine config
    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
                )
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
                )
            )
        ),
        'eventmanager' => array(
            'orm_default' => array(
                'subscribers' => array(
                    'AcploLog\Log\EntityLogger'
                )
            )
        )
    )
);

will generate:, (*24)

2012-10-29T19:30:46-02:00 DEBUG (7): Inserting entity Application\Entity\Client. Fields: {"nome":[null,"ClientName"],"created_on":[null,{"date":"2012-10-29 19:30:46","timezone_type":3,"timezone":"America\/Sao_Paulo"}]}
2012-10-29T19:32:23-02:00 DEBUG (7): Updating entity Application\Entity\Client with id 3. Fields: {"nome":["ClientName","ClientName2"]}
2012-10-29T19:36:53-02:00 DEBUG (7): Deleting entity Application\Entity\Client with id 3.

The default logfile is data/log/entity.log, (*25)

SqlLogger

With this logger you can save all Doctrine database operations within your application., (*26)

ATTENTION: This logger depends on DoctrineModule. Since its usage is optional, i did not put this requirement inside the composer.json, (*27)

Edit the config/autoload/acplolog.global.php file to enable this logger., (*28)

The default logfile is data/log/sql.log, (*29)

StaticLogger

This logger is usually used to log development or debug messages, arrays and objects. Just call it statically anywhere in your code., (*30)

AcploLog\Log\StaticLogger::save("Test message");
AcploLog\Log\StaticLogger::save("Test message 2", 'test.log');

will generate, (*31)

2012-10-29T19:32:30-02:00 DEBUG (6): Test message

Or an object:, (*32)

AcploLog\Log\StaticLogger::save($myObj);

will generate, (*33)

2013-07-30T17:26:37-03:00 DEBUG (7): {"User\\Entity\\User":[],"nome":{"type":"string","content":"Leandro"},"sobrenome":{"type":"string","content":"Silva"},"permissao":{"type":"string","content":"usuario"},"email":{"type":"string","content":"leandro@leandrosilva.info"},"acessos":{"type":"object","class":"Doctrine\\ORM\\PersistentCollection"},"login":{"type":"NULL","content":null},"senha":{"type":"string","content":"admin"},"inputFilter":{"type":"NULL","content":null},"id":{"type":"integer","content":3},"cadastrado":{"type":"object","class":"DateTime"},"atualizado":{"type":"object","class":"DateTime"}}

Optionally, you can get it through Service Locator, (*34)

$logger = $sm->get('AcploLog\Log\StaticLogger');
$logger->debug("Test message");

The default logfile is data/log/static.log, (*35)

Z-Ray

Z-Ray is an awesome resource from Zend Server that provides several information about the request, errors and the framework. It also has the possibility to add your own informations, so i added the StaticLogger messages to it., (*36)

More information can be seen here., (*37)

Warning: The Z-Ray extensions works only on Zend Server 8 or greater., (*38)

Installation

To use the StaticLogger with Z-Ray, follow these steps:, (*39)

1- Locate the zray extension directory inside the zend server., (*40)

For example on Mac/Linux can be found on: /usr/local/zend/var/zray/extesions, (*41)

2- Create a directory called AcploLog, (*42)

3- Copy the zray.php and logo.png to this directory, (*43)

The final result should be:, (*44)

ls /usr/local/zend/var/zray/extensions/AcploLog/
logo.png    zray.php
Usage

Just use the StatticLogger and the messages will appear inside a AcploLog section of the Z-Ray bar., (*45)

Optionally, you can pass a "null" value to the file argument to use just the Z-Ray, without writing the message to a file:, (*46)

AcploLog\Log\StaticLogger::save("Test message", null);

The Versions

04/05 2015

dev-master

9999999-dev http://github.com/Acplo/AcploLog

AcploLog provides some log utility your ZF2 application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module acplo

26/04 2015

0.0.1

0.0.1.0 http://github.com/Acplo/AcploLog

AcploLog provides some log utility your ZF2 application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module acplo