Download the PHP package ermetix/laravel-logger without Composer
On this page you can find all versions of the php package ermetix/laravel-logger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ermetix/laravel-logger
More information about ermetix/laravel-logger
Files in ermetix/laravel-logger
Package laravel-logger
Short Description Advanced logging package for Laravel with OpenSearch, Kafka, and typed logging support
License MIT
Homepage https://github.com/ermetix/laravel-logger
Informations about the package laravel-logger
Laravel Logger Package
Advanced logging package for Laravel with OpenSearch, Kafka, and typed logging support.
Requirements
- PHP: >= 8.2
- Laravel: >= 12.0
- Extensions:
json(built-in)curl(for HTTP requests to OpenSearch/Kafka)
- Optional (for code coverage):
pcovorxdebug(see Testing section)
Quick Start
Get up and running in 3 steps:
That's it! The package is now configured and ready to use. See Installation for detailed setup instructions.
For OpenSearch setup, see the OpenSearch Setup section below.
Features
- ✅ Typed Logging: Type-safe logging with dedicated LogObject classes (Usage Examples)
- ✅ OpenSearch Integration: Dynamic index routing with strict mappings (OpenSearch Setup)
- ✅ Kafka Support: Log to Kafka via REST Proxy (Kafka Setup)
- ✅ Index File Channel: Log to JSONL files via
index_filechannel - ✅ Deferred Logging: Non-blocking in-memory log accumulation (Deferred Logging Guide)
- ✅ Multiple Indices: api_log, general_log, job_log, integration_log, orm_log, error_log (Index Schema)
- ✅ Request Correlation: Automatic request_id propagation
- ✅ Error Handling: Automatic error logging with fatal error support
- ✅ JSON Pretty Printing: Automatic formatting of JSON fields (request_body, response_body, headers)
Installation
Step 1: Install the Package
For Local Development (Path Repository):
Add to your composer.json:
Then run:
For Production (Packagist):
Step 2: Automatic Installation (Recommended)
Run the installation command to automatically configure everything:
This command will automatically:
- ✅ Publish configuration file (
config/laravel-logger.php) - ✅ Add logging channels to
config/logging.php(opensearch,kafka,index_file) - ✅ Add middleware to
bootstrap/app.php(RequestId, ApiAccessLog, FlushDeferredLogs) - ✅ Add exception handling to
bootstrap/app.php(automatic error logging)
Force overwrite existing configuration:
Step 3: Verify Installation
Check that all components are correctly installed:
This command verifies:
- Configuration file exists
- Logging channels are configured
- Middleware is registered
- Exception handling is configured
📖 Need more details? See Installation Guide for comprehensive installation instructions, including manual configuration steps.
Configuration
Environment Variables
Add the following variables to your .env file:
Note: For local Docker development, set OPENSEARCH_VERIFY_TLS=false since Docker doesn't use HTTPS.
See env.example for all available configuration options.
📖 Want to learn more? See Configuration Guide for detailed configuration options and advanced settings.
Manual Configuration (Alternative)
If you prefer to configure manually:
Then manually add the configuration as shown in the stubs:
packages/laravel-logger/stubs/logging-channels.stub- Logging channelspackages/laravel-logger/stubs/bootstrap-app.stub- Middleware configurationpackages/laravel-logger/stubs/bootstrap-exceptions.stub- Exception handling
📖 Need help? See Installation Guide for step-by-step manual installation instructions.
OpenSearch Setup
Prerequisites
You need Docker and Docker Compose installed on your system.
📖 Detailed guide: See OpenSearch Setup Guide for comprehensive OpenSearch configuration and troubleshooting.
Step 1: Start OpenSearch
The package includes a Docker Compose configuration. You can use it in two ways:
Option A: Copy to project root (Recommended)
Option B: Use directly from package directory
Step 2: Verify OpenSearch is Running
You should see a JSON response with cluster information.
Step 3: Apply Index Templates and Policies
⚠️ IMPORTANT: Before logging, you must apply the index templates. Without this step, logs will not be indexed correctly!
Setup with OpenSearch Dashboards (Recommended):
This command will:
- ✅ Apply 6 index templates (api_log, general_log, job_log, integration_log, orm_log, error_log)
- ✅ Apply ISM retention policy
- ✅ Create index patterns in OpenSearch Dashboards
- ✅ Configure default sort order (timestamp descending)
- ✅ Create test documents for field discovery
Setup without Dashboards:
Nota: Lo script PHP è cross-platform e funziona su Windows, Linux e macOS.
Step 4: Access OpenSearch Dashboards
After running the setup script:
- Open OpenSearch Dashboards: http://localhost:5601
- Go to Discover in the left menu
- Select an index pattern (e.g.,
api_log*) - Configure visible columns manually (see below)
Note: OpenSearch Dashboards doesn't allow setting default visible columns via API. You need to configure them manually:
- In Discover, click the "+" button next to fields in the "Available fields" sidebar
- Add these recommended fields:
- **api_log***:
@timestamp,method,path,route_name,status,duration_ms,user_id,ip,request_id - **general_log***:
@timestamp,message,event,entity_type,entity_id,action_type,user_id,level,request_id - **job_log***:
@timestamp,job,command,status,duration_ms,exit_code,frequency,output,level,request_id - **integration_log***:
@timestamp,integration_name,url,method,status,duration_ms,level,request_id - **orm_log***:
@timestamp,model,action,query_type,table,duration_ms,is_slow_query,user_id,request_id - **error_log***:
@timestamp,exception_class,code,level,context_route,context_method,context_url,context_user_id,request_id
- **api_log***:
- (Optional) Save the view: Click "Save" in the top right to reuse this configuration
📖 Detailed instructions: See Configure Discover Columns for step-by-step guide on setting up visible columns in OpenSearch Dashboards.
Step 5: Verify Templates
Check that templates were applied correctly:
OpenSearch Commands Reference
Start OpenSearch:
Stop OpenSearch:
Stop and remove volumes (delete all data):
View logs:
Restart OpenSearch:
Regenerate OpenSearch completely (fresh start):
Check OpenSearch health:
Search logs:
Delete old indices:
Manual Template Application (Alternative)
If you prefer to apply templates manually:
📖 Note: For most users, the automated setup script is recommended. See OpenSearch Setup Guide for more details.
Usage
Basic Usage
Available Log Types
Log::general(GeneralLogObject)- General application eventsLog::api(ApiLogObject)- API requests/responses (auto-logged via middleware)Log::job(JobLogObject)- Jobs and scheduled tasks (auto-logged when enabled, or setfrequencyfor cron jobs)Log::cron(JobLogObject)- Deprecated: useLog::job()instead (kept for backward compatibility)Log::integration(IntegrationLogObject)- External API integrationsLog::orm(OrmLogObject)- Database/ORM operations (if enabled)Log::error(ErrorLogObject)- Errors and exceptions (auto-logged)
📖 More examples: See Usage Examples for comprehensive code examples and usage patterns for all log types.
Example: API Logging
API requests are automatically logged by the ApiAccessLog middleware. No manual logging needed.
📖 Learn more: See Usage Examples - API Logging for details on how API logging works and how to customize it.
Example: Integration Logging
Example: ORM Logging
Enable ORM logging in .env:
ORM operations are automatically logged when enabled.
📖 Learn more: See Usage Examples - ORM Logging for details on ORM logging configuration and what gets logged.
Example: Job Logging
Job execution events are automatically logged when enabled (default: enabled). The LogJobEvents listener tracks:
- Job name and ID
- Duration and memory usage
- Status (success/failed)
- Attempts and queue name
- Whether it's a cron job (scheduled command)
- Error messages for failed jobs
Enable/disable in .env:
You can also manually log job events using Log::job():
Example: Error Logging
Errors and exceptions are automatically logged by the exception handler. No manual logging needed.
📖 Learn more: See Usage Examples - Error Logging for details on automatic error logging and how to log custom errors.
Testing
Run the test suite:
Or with Pest directly:
Code Coverage
To check code coverage, you need to install a coverage driver first:
Option 1: Install PCOV (Recommended - faster)
Option 2: Install Xdebug
Verify installation:
Run coverage:
The coverage report will show:
- Percentage of code covered
- Files and classes covered
- Lines covered/uncovered
- Methods covered/uncovered
📖 Current coverage: The package maintains 99.95% code coverage (1884/1885 lines). See the generated HTML report in
coverage/index.htmlfor detailed coverage information.
Troubleshooting
Logs not appearing in OpenSearch
-
Verify OpenSearch is running:
-
Check templates are applied:
-
Verify configuration:
-
Check Laravel logs:
- Test logging manually:
📖 More help: See OpenSearch Setup Guide for detailed troubleshooting steps.
OpenSearch connection errors
- Check
OPENSEARCH_URLin.env - Verify OpenSearch is accessible:
curl http://localhost:9200 - For Docker: ensure containers are running:
docker-compose ps - Check timeout settings: increase
OPENSEARCH_TIMEOUTif needed
📖 Configuration help: See Configuration Guide for all available environment variables.
Fields not visible in OpenSearch Dashboards
- Run the setup script:
php packages/laravel-logger/docker/opensearch/setup.php --with-dashboards - Refresh field list in Dashboards: Go to Management → Index Patterns → Refresh
- Manually add columns in Discover (see OpenSearch Setup - Step 4 section above)
📖 Detailed guide: See Configure Discover Columns for step-by-step instructions on setting up visible columns.
Documentation
Getting Started
- Installation Guide - Detailed step-by-step installation instructions
- Quick Start - Get up and running in 3 steps (see above)
- Configuration Guide - Advanced configuration options
Usage & Examples
- Usage Examples - Comprehensive code examples and patterns for all log types
- Logging Usage Guide - Detailed usage patterns and best practices
- Deferred Logging - Understanding deferred logging and auto-flush mechanism
OpenSearch
- OpenSearch Setup Guide - Complete OpenSearch setup and configuration
- OpenSearch Index Schema - Understanding index structure and mappings
- Configure Discover Columns - Setting up visible columns in Dashboards
- OpenSearch Design - Architecture and design decisions
Migration & Advanced
- Migration Guide - Migrating from other logging solutions
- Environment Variables - All available configuration options
- Full Documentation Index - Complete documentation index
Architecture & Design
- OpenSearch Logging Design - Overall architecture and design decisions
- OpenSearch Diagrams - Visual architecture diagrams
- OpenSearch Indices Diagram - Detailed flow diagram including deferred logging
- Docker Structure - Docker setup and structure
Additional Resources
- Kafka Setup - Kafka REST Proxy setup and configuration
- Package Summary - Package structure overview
License
MIT