# php高性能多存储日志类 **Repository Path**: web/phplog ## Basic Information - **Project Name**: php高性能多存储日志类 - **Description**: 一个功能强大的PHP日志系统,支持多种存储方式,具有自动清理、分表规则、大并发优化等特性。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-23 - **Last Updated**: 2026-03-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Logger System A powerful PHP logging system that supports multiple storage methods, with features like automatic cleanup, table sharding rules, and high-concurrency optimization. ## Features - **Multiple Storage Support**: Supports MySQL, SQLite, and file storage - **Log Retention Management**: Configurable log retention days, 0 means disable automatic cleanup - **Automatic Cleanup**: Automatically clears expired logs, uses batch deletion to avoid program crashes - **File Storage Structure**: Creates directories by month, names files by date - **Table Sharding Rules**: Supports year, month, and day-based table sharding - **High Concurrency Optimization**: Implements batch insertion mechanism to improve writing performance - **Custom Content**: Supports adding custom context information - **Complete Log Content**: Ensures complete log content at the end ## System Requirements - PHP 5.6+ - PDO extension (for MySQL and SQLite storage) - MySQL or SQLite (optional, for corresponding storage methods) ## Installation 1. Download or clone the code to your project directory 2. Include the `Logger.php` file in your project ## Usage Examples ### 1. File Storage ```php // Include logger class require_once 'Logger.php'; // Create logger instance $logger = new Logger([ 'storage' => 'file', 'file_path' => './logs', // Log storage path 'keep_days' => 7 // Keep logs for 7 days ]); // Record different levels of logs $logger->debug('Debug message', ['user_id' => 1, 'action' => 'login']); $logger->info('Info message', ['page' => 'index', 'ip' => '127.0.0.1']); $logger->warning('Warning message', ['module' => 'payment', 'amount' => 100]); $logger->error('Error message', ['error' => 'Database connection failed']); $logger->fatal('Fatal error message', ['error' => 'System crash']); // Manual flush (optional, destructor will call it automatically) $logger->flush(); ``` ### 2. SQLite Storage ```php // Create SQLite logger instance $logger = new Logger([ 'storage' => 'sqlite', 'sqlite_path' => './logs.db', // SQLite file path 'table_prefix' => 'log_', // Table prefix 'table_rule' => 'month', // Table sharding rule: year/month/day 'keep_days' => 30 // Keep logs for 30 days ]); // Record log $logger->info('SQLite test log', ['test' => 'success']); ``` ### 3. MySQL Storage ```php // Create MySQL logger instance $logger = new Logger([ 'storage' => 'mysql', 'host' => 'localhost', // MySQL host 'port' => 3306, // MySQL port 'username' => 'root', // MySQL username 'password' => '', // MySQL password 'database' => 'log', // MySQL database 'table_prefix' => 'log_', // Table prefix 'table_rule' => 'month', // Table sharding rule: year/month/day 'keep_days' => 60 // Keep logs for 60 days ]); // Record log $logger->info('MySQL test log', ['test' => 'success']); ``` ## Configuration Options | Option | Type | Default | Description | |--------|------|--------|-------------| | storage | string | 'file' | Storage type: file/mysql/sqlite | | host | string | 'localhost' | MySQL host | | port | int | 3306 | MySQL port | | username | string | 'root' | MySQL username | | password | string | '' | MySQL password | | database | string | 'log' | MySQL database | | sqlite_path | string | './logs.db' | SQLite file path | | file_path | string | './logs' | File storage path | | table_prefix | string | 'log_' | Table prefix | | table_rule | string | 'month' | Table sharding rule: year/month/day | | keep_days | int | 30 | Log retention days, 0 means disable automatic cleanup | | batch_size | int | 100 | Batch insertion size | | max_delete_count | int | 1000 | Maximum deletion count per operation | ## Method Description ### Log Recording Methods - `log($level, $message, $context = [])` - General log recording method - `debug($message, $context = [])` - Record debug log - `info($message, $context = [])` - Record info log - `warning($message, $context = [])` - Record warning log - `error($message, $context = [])` - Record error log - `fatal($message, $context = [])` - Record fatal error log ### Other Methods - `flush()` - Manually flush log queue - `cleanExpiredLogs()` - Manually clean expired logs ## Performance Optimization 1. **Batch Writing**: Use `batch_size` configuration to batch insert logs, reducing I/O operations 2. **Caching Mechanism**: Cache table names, file paths, etc. to reduce repeated calculations 3. **Batch Deletion**: Use batch deletion when cleaning logs to avoid program crashes from deleting too much at once 4. **Intelligent Judgment**: Judge whether tables need cleaning based on table name rules, reducing unnecessary database operations ## Run Demo Run the `demo.php` file to see the complete functionality demo: ```bash php demo.php ``` ## Storage Structure ### File Storage ``` logs/ ├── 2026_03/ # Month directory │ ├── 01.log # Date file │ ├── 02.log │ └── ... └── 2026_04/ └── ... ``` ### Database Storage - **Year-based table**: `log_2026` - **Month-based table**: `log_2026_03` - **Day-based table**: `log_2026_03_23` ## Notes 1. When using MySQL storage, ensure the database is created and the user has sufficient permissions 2. When using file storage, ensure the directory has write permissions 3. It is recommended to adjust `batch_size` and `max_delete_count` parameters according to actual needs 4. For high-concurrency scenarios, it is recommended to use file storage or SQLite storage ## License This project is licensed under the MIT License.