Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CloudWatchLoggerFactory | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Logging; |
| 4 | |
| 5 | use Aws\CloudWatchLogs\CloudWatchLogsClient; |
| 6 | use Monolog\Logger; |
| 7 | use Monolog\Level; |
| 8 | use Monolog\Handler\FilterHandler; |
| 9 | use PhpNexus\Cwh\Handler\CloudWatch; |
| 10 | |
| 11 | class CloudWatchLoggerFactory |
| 12 | { |
| 13 | /** |
| 14 | * Create a custom Monolog instance. |
| 15 | * |
| 16 | * @param array $config |
| 17 | * @return \Monolog\Logger |
| 18 | */ |
| 19 | public function __invoke(array $config) |
| 20 | { |
| 21 | $sdkParams = $config["sdk"]; |
| 22 | $tags = $config["tags"] ?? []; |
| 23 | $name = $config["name"] ?? 'cloudwatch'; |
| 24 | |
| 25 | // Instantiate AWS SDK CloudWatch Logs Client |
| 26 | $client = new CloudWatchLogsClient($sdkParams); |
| 27 | |
| 28 | // Log group name, will be created if none |
| 29 | $env = config('app.env'); |
| 30 | $groupInfoName = config('app.name') . ($env !== 'production' ? '-' . $env : '') . '-info'; |
| 31 | $groupErrorName = config('app.name') . ($env !== 'production' ? '-' . $env : '') . '-error'; |
| 32 | |
| 33 | // Log stream names for info and error |
| 34 | $infoName = $config["stream"] . date('Y-m-d-H-a'); |
| 35 | |
| 36 | // Days to keep logs, 14 by default. Set to `null` to allow indefinite retention. |
| 37 | $retentionDays = $config["retention"]; |
| 38 | |
| 39 | // Instantiate handlers for info and error (tags are optional) |
| 40 | $infoHandler = new CloudWatch($client, $groupInfoName, $infoName, $retentionDays, 10000, $tags); |
| 41 | $errorHandler = new CloudWatch($client, $groupErrorName, $infoName, $retentionDays, 10000, $tags); |
| 42 | |
| 43 | $infoHandler = new FilterHandler($infoHandler, Level::Info, Level::Notice); |
| 44 | $errorHandler = new FilterHandler($errorHandler, Level::Error, Level::Critical); |
| 45 | |
| 46 | // Create a log channel |
| 47 | $logger = new Logger($name); |
| 48 | // Set handlers for different log levels |
| 49 | $logger->pushHandler($infoHandler); |
| 50 | $logger->pushHandler($errorHandler); |
| 51 | |
| 52 | return $logger; |
| 53 | } |
| 54 | } |