|
| 1 | +# hejunjie/lazylog |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | + <a href="./README.md">English</a>|<a href="./README.zh-CN.md">简体中文</a> |
| 5 | + <hr width="50%"/> |
| 6 | +</div> |
| 7 | + |
| 8 | +A lightweight PHP logging library providing **safe local log writing** and **remote exception reporting** (both synchronous and asynchronous). |
| 9 | + |
| 10 | +> Why this library exists: |
| 11 | +> While working on my Go project [oh-shit-logger](https://github.com/zxc7563598/oh-shit-logger), I needed a centralized way to collect error information. |
| 12 | +> Some friends were concerned about the performance overhead of PHP error reporting, so I wrapped my usual approach into a Composer package for easier reuse. |
| 13 | +
|
| 14 | +**This project has been parsed and summarized by Zread. For a quick overview, check here:** [Project Overview](https://zread.ai/zxc7563598/php-milvus) |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +composer require hejunjie/lazylog |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +`lazylog` provides three core methods: |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +### Write Local Logs |
| 33 | + |
| 34 | +```php |
| 35 | +use Hejunjie\Lazylog\Logger; |
| 36 | + |
| 37 | +/** |
| 38 | + * Write logs safely to local files (thread-safe + auto file rotation) |
| 39 | + * |
| 40 | + * @param string $basePath Base log directory (e.g. /var/logs or runtime/logs) |
| 41 | + * @param string $fileName Log filename (can include subpath like "error/app.log") |
| 42 | + * @param string $title Log title (e.g. "Error in Task #12") |
| 43 | + * @param string|array|object $content Log content (array, object, or string) |
| 44 | + * @param int $maxLines Max lines per file before rotation (default: 10,000) |
| 45 | + * @param int $maxSizeKB Max file size in KB before rotation (default: 2048 = 2MB) |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | +Logger::write( |
| 50 | + '/var/logs', |
| 51 | + 'error/app.log', |
| 52 | + 'Task Failed', |
| 53 | + ['message' => 'Something went wrong'] |
| 54 | +); |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +### Asynchronous Exception Reporting |
| 60 | + |
| 61 | +- Uses `exec()` / `proc_open()` to spawn a background PHP process that sends a POST request |
| 62 | +- Non-blocking for the main process |
| 63 | +- **Not recommended** in long-running frameworks (Webman / Swoole) |
| 64 | + since they keep workers alive for a long time — frequent forking may accumulate resources (zombie processes, memory leaks, etc.) |
| 65 | + |
| 66 | +```php |
| 67 | +try { |
| 68 | + // Code that may throw |
| 69 | +} catch (\Throwable $exception) { |
| 70 | + /** |
| 71 | + * Report exception asynchronously to a remote endpoint. |
| 72 | + * |
| 73 | + * @param Throwable $exception The captured exception |
| 74 | + * @param string $url Remote endpoint URL |
| 75 | + * @param string $project Project identifier (default: unknown-project) |
| 76 | + * @param array $context Additional context data (e.g. request info, env vars) |
| 77 | + * @param string $phpBinary PHP binary path for subprocess (default: php) |
| 78 | + * |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + Logger::reportAsync($exception, 'https://error.example.com/collect', 'my-project'); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +> For low-frequency error reporting, the performance cost of forking a PHP subprocess is negligible. |
| 86 | +
|
| 87 | +--- |
| 88 | + |
| 89 | +### Synchronous Exception Reporting |
| 90 | + |
| 91 | +- Recommended for long-running frameworks or when immediate reporting is needed |
| 92 | +- Avoids creating subprocesses, preventing potential zombie or leaked processes |
| 93 | + Long-running workers in frameworks like Webman/Swoole are reused between requests — even if synchronous calls block, they only affect the current worker, not others. |
| 94 | + |
| 95 | +```php |
| 96 | +try { |
| 97 | + // Code that may throw |
| 98 | +} catch (\Throwable $exception) { |
| 99 | + /** |
| 100 | + * Report exception synchronously to a remote endpoint. |
| 101 | + * |
| 102 | + * @param Throwable $exception The captured exception |
| 103 | + * @param string $url Remote endpoint URL |
| 104 | + * @param string $project Project identifier (default: unknown-project) |
| 105 | + * @param array $context Additional context data |
| 106 | + * @param int $timeout Timeout in seconds |
| 107 | + * |
| 108 | + * @return void |
| 109 | + */ |
| 110 | + Logger::reportSync($exception, 'https://error.example.com/collect', 'my-project'); |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +#### Optimization Suggestion |
| 115 | + |
| 116 | +For long-running frameworks, it’s often better to enqueue exceptions and handle reporting via a background worker: |
| 117 | + |
| 118 | +```php |
| 119 | +try { |
| 120 | + // Code that may throw |
| 121 | +} catch (\Throwable $exception) { |
| 122 | + /** |
| 123 | + * Format exception data before sending |
| 124 | + * |
| 125 | + * @param Throwable $exception The captured exception |
| 126 | + * @param string $project Project name |
| 127 | + * @param array $context Additional context info (default: empty) |
| 128 | + * |
| 129 | + * @return array |
| 130 | + */ |
| 131 | + $formatted = Logger::formatThrowable($exception, 'my-project'); |
| 132 | + // Push $formatted into a queue |
| 133 | + // Then let your worker consume and send it |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Motivation |
| 140 | + |
| 141 | +- To provide a **lightweight, unified logging solution** for quickly sending PHP exception data to my Go project **[oh-shit-logger](https://github.com/zxc7563598/oh-shit-logger)** |
| 142 | +- To avoid writing repetitive error reporting logic across multiple projects |
| 143 | +- Supports **safe local logging + async/sync remote reporting**, suitable for PHP-FPM, CLI, and long-running frameworks |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Additional Notes |
| 148 | + |
| 149 | +- Asynchronous reporting works by forking a PHP CLI subprocess |
| 150 | +- For low-frequency errors, overhead is minimal and generally safe |
| 151 | +- Under high concurrency (thousands per second), consider using a queue + worker or coroutine async (Though realistically, error reporting shouldn’t reach that volume 😅) |
| 152 | +- Local logging includes automatic rotation to prevent oversized files during long-term use |
| 153 | + |
| 154 | +--- |
| 155 | + |
| 156 | +## Issues & Contributions |
| 157 | + |
| 158 | +Have ideas, feedback, or bug reports? |
| 159 | +Feel free to open an Issue or Pull Request on GitHub! 🚀 |
0 commit comments