atk14/logger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

atk14/logger

Composer 安装命令:

composer require atk14/logger

包简介

ATK14 Logger provides logging infrastructure for the ATK14 Framework

关键字:

README 文档

README

Tests

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-01