Skip to content

Commit b7a65ba

Browse files
Merge pull request #24 from pagevamp/use-as-custom-driver
try as custom driver
2 parents 6081bbc + 7841ade commit b7a65ba

File tree

5 files changed

+44
-96
lines changed

5 files changed

+44
-96
lines changed

composer.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@
2323
"maxbanton/cwh": "^1.1.14 || ^2.0",
2424
"illuminate/support": "^5.1 || ^6.0 || ^7.0 || ^8.0"
2525
},
26-
"extra": {
27-
"laravel": {
28-
"providers": [
29-
"Pagevamp\\Providers\\CloudWatchServiceProvider"
30-
]
31-
}
32-
},
3326
"require-dev": {
3427
"friendsofphp/php-cs-fixer": "^2.12 || ^2.16",
3528
"phpunit/phpunit": "^6.5 || ^8.4 || ^9.0",

config/logging.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
return [
44
'cloudwatch' => [
5+
'driver' => 'custom',
56
'name' => env('CLOUDWATCH_LOG_NAME', ''),
67
'region' => env('CLOUDWATCH_LOG_REGION', ''),
78
'credentials' => [
@@ -12,7 +13,7 @@
1213
'retention' => env('CLOUDWATCH_LOG_RETENTION_DAYS', 14),
1314
'group_name' => env('CLOUDWATCH_LOG_GROUP_NAME', 'laravel_app'),
1415
'version' => env('CLOUDWATCH_LOG_VERSION', 'latest'),
15-
'disabled' => env('DISABLE_CLOUDWATCH_LOG', false),
16+
'batch_size' => env('CLOUDWATCH_LOG_BATCH_SIZE', 10000),
1617
'formatter' => function ($configs) {
1718
return new \Monolog\Formatter\LineFormatter(
1819
'%channel%: %level_name%: %message% %context% %extra%',
@@ -21,5 +22,6 @@
2122
true
2223
);
2324
},
25+
'via' => \Pagevamp\Logger::class
2426
],
2527
];

readme.md

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## Logger for Aws Cloud Watch
22

3+
### Breaking Change for version 1.0
4+
5+
When this package started it started as a listener for log event and would only work with another channel.
6+
This package would listen to log events and just add extra log to cloud watch. So you did not need to add mention this as `channel`.
7+
But after `1.0` it works as a custom driver.
8+
So, you MUST add a `LOG_CHANNEL` as `cloudwatch` for logging in your config for this to work going forward
9+
310
### Installation
411

512
`composer require pagevamp/laravel-cloudwatch-logs`
@@ -28,34 +35,17 @@ Config for logging is defined at `config/logging.php`. Add `cloudwatch` to the `
2835
'retention' => env('CLOUDWATCH_LOG_RETENTION_DAYS', 14),
2936
'group_name' => env('CLOUDWATCH_LOG_GROUP_NAME', 'laravel_app'),
3037
'version' => env('CLOUDWATCH_LOG_VERSION', 'latest'),
31-
'formatter' => \Monolog\Formatter\JsonFormatter::class,
32-
'disabled' => env('DISABLE_CLOUDWATCH_LOG', false),
38+
'formatter' => \Monolog\Formatter\JsonFormatter::class,
39+
'batch_size' => env('CLOUDWATCH_LOG_BATCH_SIZE', 10000),
40+
'via' => \Pagevamp\Logger::class,
3341
],
3442
]
3543
```
3644

37-
Add correct values to keys in your `.env` file. And it should work.
45+
And set the `LOG_CHANNEL` in your environment variable to `cloudwatch`.
3846

3947
If the role of your AWS EC2 instance has access to Cloudwatch logs, `CLOUDWATCH_LOG_KEY` and `CLOUDWATCH_LOG_SECRET` need not be defined in your `.env` file.
4048

41-
### Add To Project
42-
43-
#### Laravel 5.5 or Higher
44-
45-
This package uses laravel's [Package discovery](https://laravel.com/docs/5.6/packages#package-discovery). To disable this package by default you can add `DISABLE_CLOUDWATCH_LOG=true` to you local `.env` file and this package will be disabled.
46-
47-
#### Laravel 5.4 or Lower
48-
49-
Add to the `providers` array in `config/app.php`:
50-
51-
```
52-
Pagevamp\Providers\CloudWatchServiceProvider::class
53-
```
54-
55-
### Concept
56-
57-
This package relies on laravel's listener for log events. This package DOES NOT replace the default logging, instead adds additional log to AWS CLoud Watch. Hence you do not have to change the default log driver to make this work.
58-
5949
### Contribution
6050

6151
I have added a `pre-commit` hook to run `php-cs-fixer` whenever you make a commit. To enable this run `sh hooks.sh`.

src/Providers/CloudWatchServiceProvider.php renamed to src/Logger.php

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,38 @@
11
<?php
22

3-
namespace Pagevamp\Providers;
3+
namespace Pagevamp;
44

55
use Aws\CloudWatchLogs\CloudWatchLogsClient;
6-
use Illuminate\Support\ServiceProvider;
76
use Maxbanton\Cwh\Handler\CloudWatch;
87
use Monolog\Formatter\LineFormatter;
9-
use Monolog\Logger;
108
use Pagevamp\Exceptions\IncompleteCloudWatchConfig;
119

12-
class CloudWatchServiceProvider extends ServiceProvider
10+
class Logger
1311
{
14-
public function boot()
12+
13+
private $app;
14+
15+
public function __construct($app = null)
1516
{
16-
$isCloudWatchDisabled = $this->app->make('config')->get('logging.channels.cloudwatch.disabled');
17-
if (!$isCloudWatchDisabled) {
18-
$app = $this->app;
19-
$app['log']->listen(function () use ($app) {
20-
$args = \func_get_args();
21-
22-
// Laravel 5.4 returns a MessageLogged instance only
23-
if (1 == \count($args)) {
24-
$level = $args[0]->level;
25-
$message = $args[0]->message;
26-
$context = $args[0]->context;
27-
} else {
28-
$level = $args[0];
29-
$message = $args[1];
30-
$context = $args[2];
31-
}
32-
33-
if ($message instanceof \ErrorException) {
34-
return $this->getLogger()->log($level, $message, $context);
35-
}
36-
37-
if ($app['cloudwatch.logger'] instanceof Logger) {
38-
$app['cloudwatch.logger']->log($level, $message, $context);
39-
}
40-
});
41-
}
17+
$this->app = $app;
4218
}
4319

44-
public function getLogger()
20+
public function __invoke(array $config)
4521
{
22+
if($this->app === null) {
23+
$this->app = \app();
24+
}
25+
26+
$loggingConfig = $config;
4627
$cwClient = new CloudWatchLogsClient($this->getCredentials());
47-
$loggingConfig = $this->app->make('config')->get('logging.channels.cloudwatch');
4828

4929
$streamName = $loggingConfig['stream_name'];
5030
$retentionDays = $loggingConfig['retention'];
5131
$groupName = $loggingConfig['group_name'];
5232
$batchSize = isset($loggingConfig['batch_size']) ? $loggingConfig['batch_size'] : 10000;
5333

5434
$logHandler = new CloudWatch($cwClient, $groupName, $streamName, $retentionDays, $batchSize);
55-
$logger = new Logger($loggingConfig['name']);
35+
$logger = new \Monolog\Logger($loggingConfig['name']);
5636

5737
$formatter = $this->resolveFormatter($loggingConfig);
5838
$logHandler->setFormatter($formatter);
@@ -61,30 +41,12 @@ public function getLogger()
6141
return $logger;
6242
}
6343

64-
/**
65-
* Code "inspired" from here
66-
* https://aws.amazon.com/blogs/developer/php-application-logging-with-amazon-cloudwatch-logs-and-monolog
67-
* Laravel installation mentioned here did not work but PHP with Monolog worked, hence this package.
68-
*/
69-
public function register()
70-
{
71-
$this->mergeConfigFrom(
72-
__DIR__.'/../../config/logging.php',
73-
'logging.channels'
74-
);
75-
76-
if (!$this->app->make('config')->get('logging.channels.cloudwatch.disabled')) {
77-
$this->app->singleton('cloudwatch.logger', function () {
78-
return $this->getLogger();
79-
});
80-
}
81-
}
82-
8344
/**
8445
* This is the way config should be defined in config/logging.php
8546
* in key cloudwatch.
8647
*
8748
* 'cloudwatch' => [
49+
* 'driver' => 'custom',
8850
* 'name' => env('CLOUDWATCH_LOG_NAME', ''),
8951
* 'region' => env('CLOUDWATCH_LOG_REGION', ''),
9052
* 'credentials' => [
@@ -95,6 +57,7 @@ public function register()
9557
* 'retention' => env('CLOUDWATCH_LOG_RETENTION_DAYS', 14),
9658
* 'group_name' => env('CLOUDWATCH_LOG_GROUP_NAME', 'laravel_app'),
9759
* 'version' => env('CLOUDWATCH_LOG_VERSION', 'latest'),
60+
* 'via' => \Pagevamp\Logger::class,
9861
* ]
9962
*
10063
* @return array

tests/Providers/CloudWatchServiceProviderTest.php renamed to tests/LoggerTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?php
22

3-
namespace Tests\Providers;
3+
namespace Tests;
44

55
use Illuminate\Contracts\Config\Repository;
66
use Illuminate\Contracts\Foundation\Application;
7-
use Mockery;
87
use Monolog\Formatter\JsonFormatter;
98
use Monolog\Formatter\LineFormatter;
109
use Monolog\Formatter\LogglyFormatter;
1110
use Monolog\Logger;
1211
use Pagevamp\Exceptions\IncompleteCloudWatchConfig;
13-
use Pagevamp\Providers\CloudWatchServiceProvider;
1412
use PHPUnit\Framework\TestCase;
13+
use \Mockery;
1514

16-
class CloudWatchServiceProviderTest extends TestCase
15+
class LoggerTest extends TestCase
1716
{
17+
1818
public function testGetLoggerShouldResolveCustomFormatterInstanceFromConfiguration()
1919
{
2020
$cloudwatchConfigs = [
@@ -55,8 +55,8 @@ public function testGetLoggerShouldResolveCustomFormatterInstanceFromConfigurati
5555
->with(JsonFormatter::class)
5656
->andReturn($formatter);
5757

58-
$provider = new CloudWatchServiceProvider($app);
59-
$logger = $provider->getLogger();
58+
$provider = new \Pagevamp\Logger($app);
59+
$logger = $provider($cloudwatchConfigs);
6060

6161
$this->assertInstanceOf(Logger::class, $logger);
6262
$this->assertNotEmpty($logger->getHandlers());
@@ -106,8 +106,8 @@ public function testGetLoggerShouldResolveDefaultFormatterInstanceWhenConfigIsNu
106106
->with(JsonFormatter::class)
107107
->andReturn($formatter);
108108

109-
$provider = new CloudWatchServiceProvider($app);
110-
$logger = $provider->getLogger();
109+
$provider = new \Pagevamp\Logger($app);
110+
$logger = $provider($cloudwatchConfigs);
111111

112112
$this->assertInstanceOf(Logger::class, $logger);
113113
$this->assertNotEmpty($logger->getHandlers());
@@ -156,8 +156,8 @@ public function testGetLoggerShouldResolveDefaultFormatterInstanceWhenConfigIsNo
156156
->with(LineFormatter::class)
157157
->andReturn($formatter);
158158

159-
$provider = new CloudWatchServiceProvider($app);
160-
$logger = $provider->getLogger();
159+
$provider = new \Pagevamp\Logger($app);
160+
$logger = $provider($cloudwatchConfigs);
161161

162162
$this->assertInstanceOf(Logger::class, $logger);
163163
$this->assertNotEmpty($logger->getHandlers());
@@ -209,8 +209,8 @@ public function testGetLoggerShouldResolveCallableFormatter()
209209
->with(LogglyFormatter::class)
210210
->andReturn($formatter);
211211

212-
$provider = new CloudWatchServiceProvider($app);
213-
$logger = $provider->getLogger();
212+
$provider = new \Pagevamp\Logger($app);
213+
$logger = $provider($cloudwatchConfigs);
214214

215215
$this->assertInstanceOf(Logger::class, $logger);
216216
$this->assertNotEmpty($logger->getHandlers());
@@ -255,7 +255,7 @@ public function testInvalidFormatterWillThrowException()
255255
->andReturn($config);
256256

257257
$this->expectException(IncompleteCloudWatchConfig::class);
258-
$provider = new CloudWatchServiceProvider($app);
259-
$provider->getLogger();
258+
$provider = new \Pagevamp\Logger($app);
259+
$provider($cloudwatchConfigs);
260260
}
261261
}

0 commit comments

Comments
 (0)