larsgmortensen/litelog
Composer 安装命令:
composer require larsgmortensen/litelog
包简介
Ultra-fast, lightweight JSON logger for PHP with atomic append and safe log rotation.
README 文档
README
LiteLog is a fast, minimal, efficient, single-file JSON logger focused on robustness and runtime performance.
It provides atomic appends, safe log rotation, and strict error handling with zero dependencies.
Designed for high-traffic PHP applications that require reliable logging without external bloat (PHP 8.0+).
🚀 Features that make LiteLog stand out
- Blazing-fast writes – Appends JSON lines with atomic
LOCK_EXsafety. - Safe concurrency – Multiple processes can write concurrently without losing lines.
- Log rotation – Automatically rotates logs when file size exceeds threshold.
- Retention policy – Configurable pruning of old rotated logs.
- JSON by default – Structured, machine-readable logs with
timestamp,category,message, and optionalcontext. - Error-hardening – Fails fast if directory is missing, not writable, or JSON encoding fails.
- Minimal overhead – One file, static API, no runtime dependencies.
- Secure by default – Filenames are sanitized to prevent path traversal attacks.
- Production-ready – Verified by functional, benchmark, and concurrency tests.
📦 Installation
Via Composer (recommended):
composer require larsgmortensen/litelog
Or manually include the class file (e.g. src/LiteLog/LiteLog.php) with Composer’s autoloader or require_once:
use LiteLog\LiteLog; require_once __DIR__.'/src/LiteLog/LiteLog.php';
Tip: Always place your log directory outside webroot, or protect it with
.htaccess.
🚀 Quick Start
use LiteLog\LiteLog; // Configure once at bootstrap LiteLog::setDefaultDir('/path/to/logs'); LiteLog::setMaxFileSize(10 * 1024 * 1024); // 10 MB LiteLog::setMaxRotatedFiles(5); // keep 5 rotated files // Write a log line LiteLog::log( 'app.json', // Log filename (relative to log dir) 'info', // Category 'Coffee supply is critically low', // Message (string or array) ['cups_left' => 1, 'developer' => 'Sleepy'] // Context (optional) );
Example log output (JSON line)
{
"timestamp": "2025-09-18 02:53:18",
"category": "warning",
"message": "Coffee supply is critically low",
"context": {
"cups_left": 1,
"developer": "Sleepy"
}
}
🧰 API
LiteLog::setDefaultDir(string $dir, bool $createIfMissing = false): void LiteLog::setMaxFileSize(int $bytes): void LiteLog::setMaxRotatedFiles(?int $count): void LiteLog::log(string $file, string $category, string|array|object $message, array $context = []): void
Parameters
- $dir: Log directory (must exist unless
$createIfMissing=true). - $file: Filename (e.g.
app.json). Must be a clean name, no path traversal. - $category: Category string (
error,info, etc.). - $message: String, array, or object. Arrays/objects are JSON-encoded automatically.
- $context: Optional associative array with extra metadata (e.g., request id, ip).
- $bytes: Maximum file size before rotation. Default: 10 MB.
- $count: Number of rotated logs to keep.
nulldisables pruning.
📖 Usage Notes
- All log entries are one JSON line per entry → easy to parse with tools like
jqor ELK. - Rotation creates files like
app_1695030618123.json. - If retention is set, only the latest N rotated logs are kept; older are pruned.
- Fail-fast philosophy: if directory is missing or not writable, LiteLog throws immediately.
🧪 Tests
LiteLog is verified by three test suites:
-
Sanity tests (
test_litelog_new.php) Validates JSON encoding, rotation, retention, and path sanitization. -
Benchmarks (
bench_litelog.php,bench_litelog_web.php) Measures performance vs. old implementation. Results (shared hosting, 100k lines):- OLD: ~1643 ops/s
- NEW: ~1476 ops/s (≈10% overhead for safe locking)
-
Concurrency test (
concurrency_run.php) Spawns multiple parallel PHP processes writing to the same log file. ✅ 400 000 lines written without loss or duplication under rotation.
💡 Why LiteLog?
LiteLog exists for developers who want reliable structured logging without dragging in heavy frameworks.
✨ Fast – 1.5–2k ops/s even on modest hardware.
✨ Safe – Atomic writes, no lost lines under concurrency.
✨ Structured – JSON lines are machine-readable.
✨ Lightweight – One file, static API, no dependencies.
✨ Robust – Strict error handling, sanitization, rotation + retention built-in.
📦 Packagist
LiteLog on Packagist: 👉 https://packagist.org/packages/larsgmortensen/litelog
📜 License
LiteLog is released under the GNU General Public License v3.0. See LICENSE for details.
🤝 Contributing
Contributions are welcome! Fork, submit issues, or open a pull request.
✍️ Author
Developed by Lars Grove Mortensen © 2025. Feel free to reach out or contribute!
LiteLog – the lightweight JSON logger you can trust 🚀
🌟 If you find this library useful, give it a star on GitHub! 🌟
larsgmortensen/litelog 适用场景与选型建议
larsgmortensen/litelog 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「php」 「json」 「logger」 「lightweight」 「concurrency」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 larsgmortensen/litelog 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 larsgmortensen/litelog 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 larsgmortensen/litelog 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
A Zend Framework module that sets up Monolog for logging in applications.
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-09-18