sevenecks / xlog
psr3 兼容的日志记录器
0.0.4
2018-04-11 15:15 UTC
Requires
- psr/log: ^1.0
- sevenecks/ansi: ^1.0
- sevenecks/string-utils: ^0.0.12
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-13 10:50:48 UTC
README
一个符合psr-3的日志模块,支持不同日志级别显示不同颜色。
安装
通过Composer
composer require sevenecks/xlog
使用方法
require_once __DIR__ . '/vendor/autoload.php'; require('src/Logger.php'); use SevenEcks\Xlog\Logger; $xlog = new Logger;; $xlog->clearLog(); $xlog->emergency('test'); $xlog->alert('test'); $xlog->critical('test'); $xlog->error('test'); $xlog->warning('test'); $xlog->notice('test'); $xlog->info('test'); $xlog->debug('test');
Bash 使用
记录一些信息后,你可以用 cat 查看日志文件,并看到所有漂亮的颜色。
cat xlog.log
颜色
所有颜色都是使用 Colorizer 模块中的 ANSI 颜色代码创建的,Colorizer 是这个 ANSI 模块的一部分。
Emergency => Red
Alert => Light Red
Critical => Purple
Error => Light Purple
Warning => Yellow
Notice => Light Gray
Info => White
Debug => Cyan
API
PSR-3 Logger Interface 定义了此日志包使用的 API 的核心。
/** * System is unusable. * * @param string $message * @param array $context * @return void */ public function emergency($message, array $context = array()); /** * Action must be taken immediately. * * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * * @param string $message * @param array $context * @return void */ public function alert($message, array $context = array()); /** * Critical conditions. * * Example: Application component unavailable, unexpected exception. * * @param string $message * @param array $context * @return void */ public function critical($message, array $context = array()); /** * Runtime errors that do not require immediate action but should typically * be logged and monitored. * * @param string $message * @param array $context * @return void */ public function error($message, array $context = array()); /** * Exceptional occurrences that are not errors. * * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * * @param string $message * @param array $context * @return void */ public function warning($message, array $context = array()); /** * Normal but significant events. * * @param string $message * @param array $context * @return void */ public function notice($message, array $context = array()); /** * Interesting events. * * Example: User logs in, SQL logs. * * @param string $message * @param array $context * @return void */ public function info($message, array $context = array()); /** * Detailed debug information. * * @param string $message * @param array $context * @return void */ public function debug($message, array $context = array()); /** * Logs with an arbitrary level. * * @param mixed $level * @param string $message * @param array $context * @return void */ public function log($level, $message, array $context = array());
除此之外,还可以使用依赖注入通过构造函数注入来配置日志对象。
public function __construct($file_name = 'xlog.log', $append_to_file = true, $string_utils = null, ColorInterface $colorize = null)
构造函数接受文件名,$append_to_file(可能始终开启),$string_utils 对象,以及 ColorInterface 对象。如果没有提供这些,构造函数将基于默认值创建它们。如果你对模块现状满意,不需要传递任何构造函数参数,除非可能是 $file_name。
/** * Clear the log file * * @return int */ public function clearLog()
这将清除日志文件。如果你想在对象实例化后有一个新的日志,或者在任何时候,都可以调用它。
待办事项
- 添加日志容量限制功能,日志在达到一定行数后自动删除旧项
贡献
请参阅 CONTRIBUTING 和 CODE_OF_CONDUCT 以获取详细信息。
安全
如果你发现任何安全相关的问题,请通过电子邮件 bbutts@stormcode.net 而不是使用问题跟踪器。
鸣谢
变更日志
请参阅 变更日志 以获取更多信息。
许可证
MIT 许可证(MIT)。请参阅 许可证文件 以获取更多信息。