Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/matteomeloni/aws-cloudwatch-logs

Eloquent syntax for store and retrieve logs from Aws CloudWatch Logs.
https://github.com/matteomeloni/aws-cloudwatch-logs

aws aws-cloudwatch-logs laravel log php74

Last synced: about 2 hours ago
JSON representation

Eloquent syntax for store and retrieve logs from Aws CloudWatch Logs.

Awesome Lists containing this project

README

        

# AwsCloudwatchLogs

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Total Downloads][ico-downloads]][link-downloads]
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

Eloquent syntax for store and retrieve logs from Aws CloudWatch Logs.

* [Installation](#Installation)

* [Usage](#Usage)

* [Mutators and Casting](#mutators-and-casting)

* [Defining An Accessor](#defining-an-accessor)

* [Defining A Mutator](#defining-a-mutator)

* [Attribute Casting](#attribute-casting)

* [Retrieving Models](#retrieving-models)

* [Building Queries](#building-queries)

* [Select](#select)

* [Conditions](#conditions)

* [Logical Grouping](#logical-grouping)

* [Ordering](#ordering)

* [Limit](#limit)

* [Aggregates](#aggregates)

* [Retrieving Single Model](#retrieving-single-model)

* [Retrieving Single Model](#retrieving-single-model)

* [Store New Log](#store-new-log)

* [Requirements](#requirements)

## Installation

---

Via Composer

```bash
$ composer require matteomeloni/aws-cloudwatch-logs
```

Run

```bash
$ php artisan vendor:publish --provider 'Matteomeloni\CloudwatchLogs\CloudwatchLogsServiceProvider'
```

Update AWS CloudWatch Logs config in `config/aws-cloudwatch-logs.php`

## Usage

---

Extends your model with `Matteomeloni\CloudwatchLogs\CloudWatchLogs`

```php
attributes['foo'] = strtolower($value);
}
}
```

#### Attribute Casting

Your model's $casts property provides a convenient method of converting attributes to common data types.

The $casts property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to.

See the [Laravel documentation](https://laravel.com/docs/8.x/eloquent-mutators#attribute-casting) to learn more.

```php
'boolean',
];
}
```

### Retrieving Models

The model's `all` method will retrieve all of the logs from the model's associated logGroupName and logStreamName.

By default, all logs generated on the current day will be extracted.

If you want to change the time interval, then just use the `whereBetween` method

```php
attributeOne;
}

// All logs generated on custom time interval...
$logs = Logs::whereBetween('timestamp', ['Y-m-d H:i:s', 'Y-m-d H:i:s'])->get();
```

#### Building Queries

This package support the "CloudWatch Logs Insights" feature.

For more information of this feature, you can see the [Aws CloudWatch Logs Insight Documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)

To start a new query you can use the `query` method and the result of this operation is the `queryId` string, to be used to retrieve the query results:

```php
where('foo','bar')
->get();

// Retrieve the query results...
$logs = Log::query($queryId)->get();

// Retrieve last started query...
$logs = Log::queries();
```

##### Select

The `select` method allows you to specify which columns to show in the query result.

```php
select(['column1', 'column2'])
->get();

$logs = Log::query()
->select('column1', 'column2')
->get();
```

##### Conditions

Avaialable comparison operators: `=` `!=` `<` `<=` `>` `>=`

```php
where('column', 'operator', 'value')
->where('column', 'operator', 'value')
->get();

// Chainable for 'OR'.
Log::query()
->where('column', 'operator', 'value')
->orWhere('column', 'operator', 'value')
->get();

// Other types of conditions
Log::whereIn('column', [1, 2, 3])->get();
Log::orWhereIn('column', [1, 2, 3])->get();

Log::whereNotIn('column', [1, 2, 3])->get();
Log::orWhereNotIn('column', [1, 2, 3])->get();

Log::whereBetween('column', [1, 100])->get();
Log::orWhereBetween('column', [1, 100])->get();

Log::whereNull('column')->get();
Log::orWhereNull('column')->get();

Log::whereNotNull('column')->get();
Log::orWhereNotNull('column')->get();
```

##### Logical Grouping

Sometimes it may be necessary to group several "where" clauses in parentheses to achieve logical grouping in the query.

To accomplish this, you may pass a closure to the `where` method:

```php
where('column', 'operator', 'value')
->where(function ($query) {
$query->where('column', 'operator', 'value')
->orWhere('column', 'operator', 'value');
})
->get();
```

##### Ordering

The `orderBy` method allows sorting of the query by a given column.

The first argument accepted by this metod, should be the column you wish to sort by and the second argument, determines the direction of the sort, `asc` or `desc`

```php
orderBy('column')->get();

//Ordering log by column desc...
Log::query()->orderBy('column','desc')->get();

//Ordering log by column desc...
Log::query()->orderByDesc('column')->get();
```

##### Limit

The `take` or `limit` method allows to limit the number of results returned from the query.

```php
take(10)->get();

Log::query()->limit(10)->get();
```

##### Aggregates

This library also provides a variety of methods for retrieving aggregate values like `count`, `min`, `max`, `sum`, and `avg`.

For more information of this feature, you can see the [Aws CloudWatch Logs Insigiht Documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html)

You may call any of these methods after constructing your query:

```php
where('level_code', 500)
->count();

//Other types of aggregates
Log::query()->min('column');
Log::query()->max('column');
Log::query()->sum('column');
Log::query()->avg('column');
Log::query()->average('column'); //Alias for the "avg" method
```

It is possible to group and aggregate the results through the `groupBy` method.

In this case the result of the operation will be a collection.

In addition to a specific column, it is also possible to group for all the functions offered by the AWS CloudWatch Logs Insights service.

```php
groupBy('column')
->groupBy('bin (1m)')
->count();
```

### Retrieving Single Model

In addition to retrieving all of the records matching a given query, you may also retrieve single records using the `find` or `first` methods.

```php
first();

// Find a model by its ptr or throw an exception...
$log = Log::findOrFail($ptr);

// Find multiple models by their ptr.
$logs = Log::findMany([$ptr1,$ptr2,$ptr3]);
```

### Store new log

To insert a new record into AWS CloudWatch Log, you should instantiate a new model instance and set attributes on the model.

Then, call the save method on the model instance:

```php
attributeOne = 'foo';
$log->attributeTwo = 'bar';
$log->save();
```

Alternatively, you may use the create method to "save" a new log using a single PHP statement.
The inserted log instance will be returned to you by the create method:

```php
'foo',
'attributeTwo' => 'bar'
]);
```

## Requirements

- `php 7.4 or later`

- `Laravel 7 or later`

## Change log

Please see the [changelog][link-changelog] for more information on what has changed recently.

## Testing

```bash
$ composer test
```

## Contributing

Please see [contributing.md][link-contributors] for details and a todolist.

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Credits

- [Matteo Meloni][link-author]
- [All Contributors][link-contributors]

## License

The MIT License (MIT). Please see [License File][link-license] for more information.

[ico-version]: https://img.shields.io/packagist/v/matteomeloni/cloudwatch-logs?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/matteomeloni/cloudwatch-logs?style=flat-square
[link-packagist]: https://packagist.org/packages/matteomeloni/cloudwatch-logs
[link-downloads]: https://packagist.org/packages/matteomeloni/cloudwatch-logs
[link-author]: https://github.com/matteomeloni
[link-contributors]: CONTRIBUTING.md
[link-changelog]: CHANGELOG.md
[link-license]: LICENSE.md