aporat/laravel-cloudwatch-logger

A Laravel logging driver for AWS CloudWatch Logs integration

v1.1.0 2025-04-07 00:34 UTC

This package is auto-updated.

Last update: 2025-05-07 17:48:02 UTC


README

Latest Stable Version Downloads Codecov Laravel Version GitHub Actions Workflow Status License

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.