idearia/php-logger
Composer 安装命令:
composer require idearia/php-logger
包简介
Simple logger class for PHP with time tracking
关键字:
README 文档
README
Simple logger class to:
- Keep track of log entries.
- Keep track of timings with
time()andtimeEnd()methods, Javascript style. - Optionally write log entries in real-time to file or to screen.
- Optionally dump the log to file in one go at any time.
Log entries can be added with any of the following methods:
debug( $message, $title = '' )> a diagnostic message intended for the developerinfo( $message, $title = '' )> an informational message intended for the userwarning( $message, $title = '' )> a warning that something might go wrongerror( $message, $title = '' )> explain why the program is going to crash
The $title argument is optional; if present, it will be prepended to the message: "$title => $message".
Quick example
The following code:
Logger::$log_level = 'debug'; Logger::debug( "variable x is false" ); Logger::info( "program started" ); Logger::warning( "variable not set, something bad might happen" ); Logger::error( "file not found, exiting" );
will print to STDOUT the following lines:
$> 2021-07-21T11:11:03+02:00 [DEBUG] : variable x is false
$> 2021-07-21T11:11:03+02:00 [INFO] : program started
$> 2021-07-21T11:11:03+02:00 [WARNING] : variable not set, something bad might happen
$> 2021-07-21T11:11:03+02:00 [ERROR] : file not found, exiting
Timing
You can keep track of elapsed time by using the time() and timeEnd() function.
Timing example
Logger::time(); sleep(1); Logger::timeEnd();
will print:
$> 2022-04-19T17:26:26+00:00 [DEBUG] : Elapsed time => 1.003163 seconds
Named timers
If you need to time different processes at the same time, you can use named timers.
For example:
Logger::time('outer timer'); sleep(1); Logger::time('inner timer'); sleep(1); Logger::timeEnd('inner timer'); Logger::timeEnd('outer timer');
will print:
$> 2022-04-19T17:32:15+00:00 [DEBUG] : Elapsed time for 'inner timer' => 1.002268 seconds
$> 2022-04-19T17:32:15+00:00 [DEBUG] : Elapsed time for 'outer timer' => 2.006117 seconds
Options
To customize the logger, you can either:
- extend the class and override the static properties or
- set the static properties at runtime.
In the following examples, we adopt the second approach.
Set the log level
By default, the logger will assume it runs in production and, therefore, will print only error-level messages.
Specify your desired log level in the following way:
Logger::$log_level = 'error'; // Show only errors Logger::$log_level = 'warning'; // Show warnings and errors Logger::$log_level = 'info'; // Show info messages, warnings and errors Logger::$log_level = 'debug'; // Show debug messages, info messages, warnings and errors
Write to file
To also write to file, set:
Logger::$write_log = true;
To customize the log file path:
Logger::$log_dir = 'logs'; Logger::$log_file_name = 'my-log'; Logger::$log_file_extension = 'log';
To overwrite the log file at every run of the script:
Logger::$log_file_append = false;
Do not print to screen
To prevent printing to STDOUT:
Logger::$print_log = false;
Parallel code caveat
The class uses the static methods and internal flags (e.g. $logger_ready) to keep its state. We do this to make the class work straight away, without any previous configuration or the need to instantiate it. This however can create race conditions if you are executing parallel code. Please let us know if this is a problem for you, if we receive enough feedback, we will switch to a more class-like approach.
Contributing 🙂
Feel free to fork the repository and make a pull request!
Before sending the request, please make sure your code passes all the tests:
composer run test
idearia/php-logger 适用场景与选型建议
idearia/php-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.24k 次下载、GitHub Stars 达 51, 最近一次更新时间为 2018 年 05 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「logging」 「time-tracking」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 idearia/php-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 idearia/php-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 idearia/php-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Zend Framework module that sets up Monolog for logging in applications.
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
Laravel 5.x.x library for integration Monolog Sentry
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
Pacote simplificado para persistência de logs
统计信息
- 总下载量: 3.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 53
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2018-05-04