A Laravel logging driver for seamless integration with AWS CloudWatch Logs.
- 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
- PHP: 8.4 or higher
- Laravel: 10.x, 11.x, 12.x
- AWS SDK: Provided via
phpnexus/cwh
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"
Add this to your config/logging.php
:
'channels' => [
'cloudwatch' => require config_path('cloudwatch-logger.php'),
],
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%"
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
);
},
use Illuminate\Support\Facades\Log;
Log::info('User login', ['user_id' => 123]);
composer test
composer test-coverage
This package is licensed under the MIT License.