joebengalen/logger
Composer 安装命令:
composer require joebengalen/logger
包简介
Lightweight psr-3 logger library.
README 文档
README
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 joebengalen/logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Zend Framework module that sets up Monolog for logging in applications.
Workflow logger
HTTP request logger middleware for Laravel
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
统计信息
- 总下载量: 18
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-10