承接 joebengalen/logger 相关项目开发

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

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

joebengalen/logger

Composer 安装命令:

composer require joebengalen/logger

包简介

Lightweight psr-3 logger library.

README 文档

README

Build Status

Lightweight psr-3 logger library.

Getting started

Installation

It is recommended to install the package with composer:

require: {
    "joebengalen/logger": "*"
}

Usage

use JoeBengalen\Logger\Logger;
use JoeBengalen\Logger\Handler;

// basic instantiation of the logger
$logger = new Logger([
    // callable handlers
]);

// basic usage
$logger->emergency('emergency message');    // System is unusable
$logger->alert('alert message');            // Action must be taken immediately (Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.)
$logger->critical('critical message');      // Critical conditions (Example: Application component unavailable, unexpected exception.)
$logger->error('error message');            // Runtime errors that do not require immediate action but should typically be logged and monitored.
$logger->warning('warning message');        // Exceptional occurrences that are not errors (Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.)
$logger->notice('notice message');          // Normal but significant events.
$logger->info('info message');              // Interesting events (Example: User logs in, SQL logs.)
$logger->debug('debug message');            // Detailed debug information.

Context

Along with a message also a second argument can be passed. This context is an array which could contain anything.

The $context can be used to replace placeholders in the message.

$logger->info('User {username} created.', ['username' => 'John Doe']);
// -> User John Doe created.

The context has a special key exception which could be used to pass an \Exception. The handler can recognize the \Exception and act upon it.

$logger->critical("Unexpected Exception occurred.", ['exception' => new \Exception('Something went horribly wrong :(')]);

Aside from the named functionality the context array can be used to pass any data that may be useful for the message.

Handlers

Handers are callables registered to the logger. It is up to the user which handler(s) to register. Note that there is no default handler. So if none is registered nothing happens. The registered handlers will be called in the order they are given.

Shipped handlers

All shipped handlers process every message. There if no filter based on the log level.

FileHandler

The FileHandler is given a file in its initialization and logs all messages into that file.

$logger = new Logger([
    new Handler\FileHandler('default.log')
]);
DatabaseHandler

The DatabaseHandler is given \PDO instance in its initialization and logs all messages into a table.

$logger = new Logger([
    new Handler\DatabaseHander(new \PDO(...));
]);

Custom handlers

A handler is, simply put, a callable, which is given an instance of JoeBengalen\Logger\MessageInterface.

function (\JoeBengalen\Logger\MessageInterface $message) { }

All shipped handlers are invokable objects, but a handler could as well be an anonymous function, a static class method or any other valid callable.

use JoeBengalen\Logger\MessageInterface;
use Psr\Log\LogLevel;

$logger = new Logger([
    
    // anonymous function
    function (MessageInterface $message) {
        if ($message->getLevel() === LogLevel::EMERGENCY) {
            // send an email
        }
    },

    // static class method
    ['ClassName', 'staticMethod'] // declared somewhere else
]);

Note that it is up to the handler to handle the context array properly. (For example, replacing placeholders in the message and recognizing an \Exception)

Custom MessageInterface factory

The factory to create a MessageInterface instance is another callable, registered as $option message.factory. This factory is given three arguments. The $level, $message and array $context and should return an instance that implements the JoeBengalen\Logger\MessageInterface. Apart from that the factory is completely free to return any object (as long as it implements the right interface) it wants and format the given arguments as it pleases.

By default an instance of Message is returned.

use JoeBengalen\Logger\Logger;
use JoeBengalen\Logger\Message;

$logger = new Logger([
    // handlers
], [

    // MessageInterface factory
    'message.factory' => function($level, $message, $context) {
        return new Message($level, $message, $context);
    }

]);

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-10