aporat / laravel-cloudwatch-logger
A Laravel logging driver for AWS CloudWatch Logs integration
Fund package maintenance!
aporat
Requires
- php: ^8.3
- ext-json: *
- illuminate/log: ^10.0 || ^11.0 || ^12.0
- illuminate/support: ^10.0 || ^11.0 || ^12.0
- phpnexus/cwh: ^2.0 || ^3.0
Requires (Dev)
- laravel/pint: ^1.21
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0 || ^9.0 || ^10.0
- phpunit/phpunit: ^11.0 || ^12.0
README
A Laravel logging driver for seamless integration with AWS CloudWatch Logs.
Features
- Custom Monolog channel for sending logs to CloudWatch
- Configurable AWS credentials, log group, stream, retention, and batch size
- Supports string-based, class-based, and callable custom log formatters
- Compatible with Laravel's
Log
facade and channel system - Laravel config publishing and environment-based setup
Requirements
- PHP: 8.4 or higher
- Laravel: 10.x, 11.x, 12.x
- AWS SDK: Provided via
phpnexus/cwh
Installation
composer require aporat/laravel-cloudwatch-logger
If auto-discovery is disabled, add the provider manually to config/app.php
:
'providers' => [ Aporat\CloudWatchLogger\CloudWatchLoggerServiceProvider::class, ],
Then publish the config:
php artisan vendor:publish --provider="Aporat\CloudWatchLogger\CloudWatchLoggerServiceProvider" --tag="config"
Configuration
Step 1: Add the CloudWatch Channel
Add this to your config/logging.php
:
'channels' => [ 'cloudwatch' => require config_path('cloudwatch-logger.php'), ],
Step 2: Configure .env
LOG_CHANNEL=cloudwatch AWS_ACCESS_KEY_ID=your-aws-key AWS_SECRET_ACCESS_KEY=your-aws-secret AWS_DEFAULT_REGION=us-east-1 CLOUDWATCH_LOG_GROUP_NAME=myapp-prod CLOUDWATCH_LOG_STREAM=app-logs CLOUDWATCH_LOG_NAME=myapp CLOUDWATCH_LOG_RETENTION=14 CLOUDWATCH_LOG_LEVEL=error CLOUDWATCH_LOG_BATCH_SIZE=10000 # Optional: formatter as a string CLOUDWATCH_LOG_FORMAT="%channel%: %level_name%: %message% %context% %extra%"
Step 3: Custom Formatters
You can customize the formatter in cloudwatch-logger.php
:
// LineFormatter as format string 'formatter' => '%channel%: %level_name%: %message% %context% %extra%', // Or as a class 'formatter' => Monolog\Formatter\JsonFormatter::class, // Or as a callable 'formatter' => function (array $config) { return new Monolog\Formatter\LineFormatter( format: '%channel%: %level_name%: %message% %context% %extra%', dateFormat: null, allowInlineLineBreaks: false, ignoreEmptyContextAndExtra: true ); },
Usage
use Illuminate\Support\Facades\Log; Log::info('User login', ['user_id' => 123]);
Testing
composer test
composer test-coverage
License
This package is licensed under the MIT License.