los/loslog 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

los/loslog

Composer 安装命令:

composer require los/loslog

包简介

LosLog provides some log utility

README 文档

README

Build Status Latest Stable Version Total Downloads Coverage Status Scrutinizer Code Quality SensioLabs Insight Dependency Status

Introduction

This is the 2.0 documentation version. Please refer to the README-1.0 for 1.0 documentation.

This module provides some useful log classes:

  • 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.

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.

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.

Add the delegator factory to the ErrorHandler, like:

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.

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.

Zend Expressive

Add the middleware as the first middleware in your pipeline, like:

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

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

'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:

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

This will produce:

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

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:

\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

ExceptionLogger

To enable the ExceptionLogger just add the registerHandlers inside your public/index.php

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

StaticLogger

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

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

will generate

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

Or an object:

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

will generate

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

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.

More information can be seen here.

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.

Usage

Just use the StaticLogger and the messages will appear inside a LosLog section of the Z-Ray bar.

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

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

los/loslog 适用场景与选型建议

los/loslog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.2k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2012 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「zf2」 「module」 「los」 「zf3」 「expressive」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 los/loslog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 los/loslog 我们能提供哪些服务?
定制开发 / 二次开发

基于 los/loslog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 51.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 10
  • 依赖项目数: 6
  • 推荐数: 14

GitHub 信息

  • Stars: 16
  • Watchers: 5
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-11-01