atk14/logger
Composer 安装命令:
composer require atk14/logger
包简介
ATK14 Logger provides logging infrastructure for the ATK14 Framework
README 文档
README
A lightweight logging library for PHP applications. Supports writing to files, stdout, and in-memory buffers, with configurable log levels and optional email notifications on errors.
Installation
composer require atk14/logger
Basic usage
define("LOGGER_DEFAULT_LOG_FILE", "/path/to/app/log/app.log"); $logger = new Logger("import_articles"); $logger->start(); $logger->info("Starting import"); $logger->info("Total articles to import: " . count($articles)); // ... $logger->warn("Article #42 is missing a thumbnail"); $logger->error("Database connection lost"); // Writes buffered messages to the log file and sends an email notification // if a message at or above the notify_level was logged. $logger->stop();
Output format:
2024-05-15 14:23:00 import_articles[12345]: START
2024-05-15 14:23:01 import_articles[12345]: Starting import
2024-05-15 14:23:01 import_articles[12345]: Total articles to import: 100
2024-05-15 14:23:02 import_articles[12345]: WARN: Article #42 is missing a thumbnail
2024-05-15 14:23:03 import_articles[12345]: ERROR: Database connection lost
2024-05-15 14:23:03 import_articles[12345]: STOP, running time: 0 min 3.00 sec
Log levels
| Method | Level | Int |
|---|---|---|
$logger->debug($msg) |
debug | -1 |
$logger->info($msg) |
info | 0 |
$logger->warn($msg) |
warn | 2 |
$logger->error($msg) |
error | 4 |
$logger->security($msg) |
security | 5 |
For a custom level use put_log():
$logger->put_log("Custom message", 3); // warn++
Output destinations
By default, messages are written to a file. Multiple destinations can be combined:
// File only (default) $logger = new Logger("my_app"); // Stdout only $logger = new Logger("my_app", ["log_to_stdout" => true]); // Both file and stdout $logger = new Logger("my_app", ["log_to_file" => true, "log_to_stdout" => true]); // In-memory buffer (accessible via $logger->buffer) $logger = new Logger("my_app", ["log_to_buffer" => true]); $logger->info("Hello"); $logger->flush(); echo $logger->buffer; // "2024-05-15 14:23:01 my_app[12345]: Hello\n" // Also output to stdout when running in a terminal $logger = new Logger("my_app", ["automatically_log_to_stdout_on_terminal" => true]);
Email notifications
When flush_all() (or stop()) is called and at least one message at or above notify_level was logged, an email with the full log is sent.
define("LOGGER_DEFAULT_NOTIFY_EMAIL", "ops@example.com"); // Trigger email on warn (level 2) and above: define("LOGGER_MIN_LEVEL_FOR_EMAIL_NOTIFICATION", 2);
Or configure per prefix (see Prefix-based configuration).
Prefix-based configuration
Different scripts or tasks can be routed to separate log files with individual settings via the global $LOGGER_CONFIGURATION array. Wildcard patterns are supported.
$LOGGER_CONFIGURATION = [ // Exact match "cache_remover" => [ "log_file" => "/var/log/app/cache_remover.log", "notify_email" => "ops@example.com", "notify_level" => "error", // send email on error and above "no_log_level" => "debug", // suppress debug messages from output ], // Wildcard: matches "import_articles", "import_data", etc. "import_*" => [ "log_file" => "/var/log/app/import.log", "notify_email" => "imports@example.com", "notify_level" => "warn", ], ];
More specific keys take precedence over wildcards.
Constructor options
$logger = new Logger($prefix, $options);
| Option | Default | Description |
|---|---|---|
prefix |
"" |
Application mark shown in every log line |
default_log_file |
LOGGER_DEFAULT_LOG_FILE |
Path to the log file |
log_to_file |
true (unless log_to_stdout is true) |
Write to log file |
log_to_stdout |
false |
Write to stdout |
log_to_buffer |
false |
Write to $logger->buffer (StringBuffer) |
automatically_log_to_stdout_on_terminal |
false |
Also write to stdout when running in a terminal |
disable_start_and_stop_marks |
false |
Suppress START/STOP lines |
default_notify_email |
LOGGER_DEFAULT_NOTIFY_EMAIL |
Recipient for email notifications |
Constants
| Constant | Default | Description |
|---|---|---|
LOGGER_DEFAULT_LOG_FILE |
/tmp/logger.log |
Default log file path |
LOGGER_DEFAULT_NOTIFY_EMAIL |
"" |
Default notification email (empty = disabled) |
LOGGER_MIN_LEVEL_FOR_EMAIL_NOTIFICATION |
99 |
Minimum level to trigger email (99 = disabled) |
LOGGER_NO_LOG_LEVEL |
-99 |
Messages at or below this level are suppressed |
License
MIT
atk14/logger 适用场景与选型建议
atk14/logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.69k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logger」 「atk14」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 atk14/logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 atk14/logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 atk14/logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pupiq client for ATK14 applications
A panel for Tracy Debugger with DbMole statistics
ATK14 form field for entering phone numbers
A panel for Tracy Debugger in an ATK14 project. Displays nested list of all rendered templates.
Extended PHP Markdown parser tuned for usage in ATK14 projects
ConfirmationField is a form field for Atk14 applications. It's like the BooleanField (checkbox) but the ConfirmationField must be ticked.
统计信息
- 总下载量: 1.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-01