2017 © Pedro Peláez
 

library loslog

LosLog provides some log utility

image

los/loslog

LosLog provides some log utility

  • Monday, March 26, 2018
  • by Lansoweb
  • Repository
  • 5 Watchers
  • 16 Stars
  • 9,870 Installations
  • PHP
  • 6 Dependents
  • 13 Suggesters
  • 5 Forks
  • 1 Open issues
  • 30 Versions
  • 9 % Grown

The README.md

LosLog

Build Status Latest Stable Version Total Downloads Coverage Status Scrutinizer Code Quality SensioLabs Insight Dependency Status, (*1)

Introduction

This is the 2.0 documentation version. Please refer to the README-1.0 for 1.0 documentation., (*2)

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

  • LosLog = An error middleware for PSR-7 compatible frameworks/applications
  • HttpLog = Use to log request and response from a PSR-7 application
  • ErrorLogger = PHP error
  • ExceptionLogger = PHP Exception
  • StaticLogger = "Shortcut" to a generic file logger. Can be attached to the Z-Ray in Zend Server
  • Rollbar writer = A Rollbar writer. Uploads errors and exceptions to Rollbar service

Requirements

  • php >= 5.6.0
  • laminas/laminas-stratigility
  • laminas/laminas-diactoros
  • laminas/laminas-log

Instalation

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

php composer.phar require los/loslog

Usage

Copy the file loslog.global.php.dist to your config/autoload/ , rename it to loslog.global.php and change the default options, if needed., (*5)

LosLog Middleware

Zend Expressive

Expressive 2.0 introduced a new method to handle errors, using listeners to the ErrorHandler and delegator factories, so this is the preferable method., (*6)

Add the delegator factory to the ErrorHandler, like:, (*7)

return [
    'dependencies' => [
        'factories' => [
            LosMiddleware\LosLog\LosLog::class => LosMiddleware\LosLog\LosLogFactory::class,
        ],
        'delegators' => [
            ErrorHandler::class => [
                LosMiddleware\LosLog\ErrorHandlerListenerDelegatorFactory::class,
            ],
        ],
    ],
];

General use

If using other framework, you can add the LosLogFactory to your factory system, manually create a LosLog instance or call the LosLogFactory directly., (*8)

HttpLog Middleware

It will log requests and responses in compact or full mode. It will include X-Request-Id and X-Response-Time headers if present., (*9)

Zend Expressive

Add the middleware as the first middleware in your pipeline, like:, (*10)

return [
    'middleware_pipeline' => [
        'before' => [
            'middleware' => [
                LosMiddleware\LosLog\HttpLog::class,
            ],
            'priority' => 10000,
        ],
    ],
];

Set the desired options in loslog.global.php (or loslog.local.php):, (*11)

'http_logger_file' => 'http.log',
'log_request' => true,
'log_response' => true,
'full' => false,

You can integrate with los/request-id and los/response-time. The order is important, use as bellow:, (*12)

return [
    'middleware_pipeline' => [
        'before' => [
            'middleware' => [
                LosMiddleware\RequestId\RequestId::class,
                LosMiddleware\LosLog\HttpLog::class,
                LosMiddleware\ResponseTime\ResponseTime::class
            ],
            'priority' => 10000,
        ],
    ],
];

This will produce:, (*13)

2015-11-20T14:04:51+00:00 INFO (6): Request: GET /shop/v1/item/1 RequestId: 12dbf2d2-52c5-4954-b573-3aa2fee58612
2015-11-20T14:04:51+00:00 INFO (6): Response: 200 OK RequestId: 12dbf2d2-52c5-4954-b573-3aa2fee58612 ResponseTime: 14.90ms

ErrorLogger

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

Zend Framework 2

chdir(dirname(__DIR__));

require 'init_autoloader.php';

\LosMiddleware\LosLog\ErrorLogger::registerHandlers();

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

Zend Expressive

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

/** @var \Interop\Container\ContainerInterface $container */
$container = require 'config/container.php';

\LosMiddleware\LosLog\ErrorLogger::registerHandlers('error.log', '/tmp');

/* @var \Mezzio\Application $api */
$app = $container->get('Mezzio\Application');

$app->run();

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

\LosLog\Log\ErrorLogger::registerHandlers();

Output example

2015-10-30T17:58:10-02:00 ERR (3): Error: Call to a member function format() on a non-object in <filename> on line <line>

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

ExceptionLogger

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

Zend Framework 2

chdir(dirname(__DIR__));

require 'init_autoloader.php';

\LosMiddleware\LosLog\ExceptionLogger::registerHandlers('exception.log', '/tmp');

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

Zend Expressive

chdir(dirname(__DIR__));
require 'vendor/autoload.php';

/** @var \Interop\Container\ContainerInterface $container */
$container = require 'config/container.php';

\LosMiddleware\LosLog\ExceptionLogger::registerHandlers('exception.log', '/tmp');

/* @var \Mezzio\Application $api */
$app = $container->get('Mezzio\Application');

$app->run();

Output example

2015-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): Laminas\ServiceManager\ServiceManager->createServiceViaCallback(Object(Closure), 'teste', 'Application\Service\Tes...')
#1 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(487): Laminas\ServiceManager\ServiceManager->createFromFactory('teste', 'Application\Service\Tes...')
#2 <dir>/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php(442): Laminas\ServiceManager\ServiceManager->create(Array)
#3 <dir>/src/Application/Module.php(29): Laminas\ServiceManager\ServiceManager->get('Application\Service\Tes...')
#4 [internal function]: Application\Module->onBootstrap(Object(Laminas\Mvc\MvcEvent))
#5 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Laminas\Mvc\MvcEvent))
#6 <dir>/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(208): Laminas\EventManager\EventManager->triggerListeners('bootstrap', Object(Laminas\Mvc\MvcEvent), Array)
#7 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(146): Laminas\EventManager\EventManager->trigger('bootstrap', Object(Laminas\Mvc\MvcEvent))
#8 <dir>/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(243): Laminas\Mvc\Application->bootstrap()
#9 <dir>/public/index.php(23): Laminas\Mvc\Application::init(Array)
#10 {main}

The default logfile is data/log/exception.log, (*18)

StaticLogger

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

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

will generate, (*20)

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

Or an object:, (*21)

\LosMiddleware\LosLog\StaticLogger::save($myObj);

will generate, (*22)

2015-10-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"}}

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

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., (*24)

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

Installation

The LosLog module is available via the Official Z-Ray plugin system, just access the tab from your Zend Server UI and install it., (*26)

Usage

Just use the StaticLogger and the messages will appear inside a LosLog section of the Z-Ray bar., (*27)

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

\LosMiddleware\LosLog\StaticLogger::save("Test message", null);

The Versions

03/03 2018
04/04 2017

2.1.1

2.1.1.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module expressive los

26/03 2017

2.1.0

2.1.0.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module expressive los

22/11 2016
20/11 2015
20/11 2015
02/11 2015
02/11 2015

dev-2.0-dev

dev-2.0-dev http://github.com/Lansoweb/LosLog

LosLog provides some log utility

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module expressive los

26/06 2015

1.0.15

1.0.15.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module los

01/04 2015

1.0.14

1.0.14.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module los

01/04 2015

1.0.13

1.0.13.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

log zf2 module los

09/02 2015

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

The Development Requires

log zf2 module los

28/01 2015

1.0.12

1.0.12.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

09/11 2014

1.0.11

1.0.11.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

28/09 2014

1.0.10

1.0.10.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

17/07 2014

1.0.9

1.0.9.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

18/06 2014

1.0.8

1.0.8.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

23/09 2013

1.0.6

1.0.6.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

23/09 2013

1.0.7

1.0.7.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

30/07 2013

1.0.5

1.0.5.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

30/07 2013

1.0.4

1.0.4.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

30/07 2013

1.0.3

1.0.3.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

22/05 2013

1.0.2

1.0.2.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

14/02 2013

1.0.1

1.0.1.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

BSD-3

The Requires

 

log zf2 module los

01/11 2012

1.0.0

1.0.0.0 http://github.com/Lansoweb/LosLog

LosLog provides some log utility your ZF2 application

  Sources   Download

MIT

The Requires

 

log zf2 module los