承接 activecollab/logger 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

activecollab/logger

Composer 安装命令:

composer require activecollab/logger

包简介

PSR-3 logger that is easy to plug in Active Collab components

README 文档

README

This package implements some of our internal conventions on top of PSR-3. Logger that it publishes is fully PSR-3 comptabile with some extra functionality (optional), as well as a factory that makes logger creation easy:

$factory = new LoggerFactory();
$logger = $factory->create(
    'ActiveCollab', 
    '1.0.0', 
    'development', 
    LoggerInterface::LOG_FOR_DEBUG, 
    LoggerInterface::FILE, 
    '/path/to/logs/dir'
);

Once logger is set, you can use it like any other PSR-3 logger:

$logger->info('Something interesting happened: {what}', [
    'what' => 'really interesting event'
]);

Special loggers are:

  1. Event logging using LoggerInterface::event() method. This will log a named event on info level, and set event context attribute to event name,
  2. Request summary logging using LoggerInterface::requestSummary() method. This will log some interesting request data, like executing time, total queries and query count etc.

Loggers

Packages comes with following backends implemented:

LoggerInterface::FILE - Log to files in log directory. Log directory is required as first logger argument when creating a logger:

$logger = $factory->create(
    'ActiveCollab', 
    '1.0.0', 
    'development', 
    LoggerInterface::LOG_FOR_DEBUG, 
    LoggerInterface::FILE, 
    '/path/to/logs/dir', 
    'my-awesome-logs.txt', 
    0777
);

Second argument is log file name, and it is optional. When skipped, system will log to log.txt file in the specified folder. Third argument is file permissions level. Default is 0644 when skipped, but you can specify any value (in octal notation).

Note that we set rotating file logging, where only past 7 days of logs are kept.

LoggerInterface::GRAYLOG - Log messages are sent to Graylog2 server using GELF formatter. Additional arguments are Graylog2 server host and port. If they are skipped, 127.0.0.1 and are used:

$logger = $factory->create(
    'ActiveCollab', 
    '1.0.0', 
    'development', 
    LoggerInterface::LOG_FOR_DEBUG, 
    LoggerInterface::GRAYLOG, 
    'graylog.company.com', 
    12201
);

LoggerInterface::BLACKHOLE - Messages are not logged anywhere.

Message Buffering

Logger is built to buffer messages until request details are set (using setAppRequest() method). Reason why we delay writing to log is to be able to add request details to all messages, so we can connect the dots alter on:

// Set HTTP request from PSR-7 ServerRequestInterface
$logger->setAppRequest(new \ActiveCollab\Logger\AppRequest\HttpRequest($request));

// Set CLI request from arguments
$logger->setAppRequest(new \ActiveCollab\Logger\AppRequest\CliRequest('session ID', $_SERVER['argv']));

If request is not set, buffer will not be flushed unless you flush it yourself, or register a shutdown function:

$logger->flushBufferOnShutdown();

Application Details

This package always logs application name, version and environemnt. These arguments are required and they need to be provided to FactoryInterface::create() method, when creating new logger instance:

$logger = $factory->create('ActiveCollab', '1.0.0', 'development', LoggerInterface::LOG_FOR_DEBUG, LoggerInterface::FILE, '/path/to/logs/dir');

Environment arguments are sent as context arguments with all messages captured via logger instance. User can specify additional environment arguments, using FactoryInterface::setAdditionalEnvArguments() method:

// Additional environment arguments can be set on factory level, and factory will pass them to all loggers that it produces
$factory->setAdditionalEnvArguments([
    'account_id' => 123,
    'extra_argument' => 'with extra value',
]);

// Or you can specify them on logger level
$logger->setAdditionalEnvArguments([
    'account_id' => 123,
    'extra_argument' => 'with extra value',
]);

Exception Serialization

When exceptions are passed as context arguments, package will "explode" them to a group of relevant arguments: message, file, line, code, and trace. Previous exception is also extracted, when available:

try {
    // Something risky
} catch (ExceptioN $e) {
    $logger->error('An {exception} happened :(', [
        'exception' => $e,
    ]);
}

If you have special exceptions that collect more info than message, code, file, line, trace and previous, you can register a callback that will extract that data as well:

$logger->addExceptionSerializer(function ($argument_name, $exception, array &$context) {
    if ($exception instanceof \SpecialError) {
        foreach ($exception->getParams() as $k => $v) {
            $context["{$argument_name}_extra_param_{$k}"] = $v;
        }
    }
});

Callback gets three arguments:

  1. $argument_name - contenxt argument name under which we found the exception,
  2. $exception_name - exception itself,
  3. $context - access to resulting log message context arguments.

As with additional environment variables, exception serializers can be added to factory, and factory will pass it on to all loggers that it produces:

$factory->addExceptionSerializer(function ($argument_name, $exception, array &$context) {
    if ($exception instanceof \SpecialError) {
        foreach ($exception->getParams() as $k => $v) {
            $context["{$argument_name}_extra_param_{$k}"] = $v;
        }
    }
});

Error Handling

Logger comes equiped with a class that can register error and exception handlers and direct them to the log. Quick setup:

$handler = new ErrorHandler($logger);
$handler->initialize();

To restore error and exception handled, simply call restore() method:

$handler->restore();

Handler can be configured to do different things for diffent error levels. For example, you can configure it to throw an exception on PHP warning, or to silence an event all together:

$handler->setHowToHandleError(E_STRICT, ErrorHandlerInterface::SILENCE);
$handler->setHowToHandleError(E_DEPRECATED, ErrorHandlerInterface::LOG_NOTICE);
$handler->setHowToHandleError(E_NOTICE, ErrorHandlerInterface::LOG_ERROR);
$handler->setHowToHandleError(E_USER_ERROR, ErrorHandlerInterface::THROW_EXCEPTION);

By default, exceptions are logged and re-thrown. This behavior can be turned off:

$handler->setReThrowException(false);

activecollab/logger 适用场景与选型建议

activecollab/logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69.08k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 07 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 69.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 26
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-03