Skip to content

Commit be0ead1

Browse files
committed
First commit
0 parents  commit be0ead1

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/.idea
2+
/vendor
3+
/node_modules
4+
package-lock.json
5+
composer.phar
6+
composer.lock
7+
phpunit.xml
8+
.phpunit.result.cache
9+
.DS_Store
10+
Thumbs.db

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Laravel Custom Logging
2+
3+
This package provides us with an additional `origin` variable for logs to determine the source of the log.
4+
5+
## Installation
6+
7+
You can install the package via composer:
8+
9+
```bash
10+
composer require coreproc/laravel-custom-logging
11+
```
12+
13+
Publish the config file:
14+
15+
```bash
16+
php artisan vendor:publish --provider="Coreproc\LaravelCustomLogging\CustomLoggingServiceProvider"
17+
```
18+
19+
## Usage
20+
21+
Typically, we use this in the `stderr` channel. Here is an example of the configuration:
22+
23+
```php
24+
'channels' => [
25+
'stderr' => [
26+
'driver' => 'monolog',
27+
'level' => env('LOG_LEVEL', 'debug'),
28+
'handler' => StreamHandler::class,
29+
'formatter' => Monolog\Formatter\JsonFormatter::class,
30+
'formatter_with' => [
31+
'includeStacktraces' => true,
32+
'batchMode' => Monolog\Formatter\JsonFormatter::BATCH_MODE_JSON,
33+
'appendNewline' => true,
34+
],
35+
'with' => [
36+
'stream' => 'php://stderr',
37+
],
38+
'processors' => [\Coreproc\LaravelCustomLogging\AddOriginProcessor::class],
39+
],
40+
],
41+
```

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "coreproc/laravel-custom-logging",
3+
"description": "Custom logging for AWS cloudwatch containers",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Coreproc\\LaravelCustomLogging\\": "src/"
9+
}
10+
},
11+
"extra": {
12+
"laravel": {
13+
"providers": [
14+
"Coreproc\\LaravelCustomLogging\\CustomLoggingServiceProvider"
15+
]
16+
}
17+
},
18+
"authors": [
19+
{
20+
"name": "Chris Bautista",
21+
"email": "[email protected]"
22+
}
23+
],
24+
"require": {}
25+
}

config/custom-logging.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
5+
'container_role' => env('CONTAINER_ROLE', 'web'),
6+
7+
];

src/AddOriginProcessor.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Coreproc\LaravelCustomLogging;
4+
5+
use Monolog\LogRecord;
6+
use Monolog\Processor\ProcessorInterface;
7+
8+
class AddOriginProcessor implements ProcessorInterface
9+
{
10+
public function __invoke(LogRecord $record): array|LogRecord
11+
{
12+
return new \Coreproc\LaravelCustomLogging\LogRecord(
13+
$record->datetime,
14+
$record->channel,
15+
$record->level,
16+
$record->message,
17+
$record->context,
18+
$record->extra
19+
);
20+
}
21+
}

src/CustomLoggingServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Coreproc\LaravelCustomLogging;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class CustomLoggingServiceProvider extends ServiceProvider
8+
{
9+
public function boot(): void
10+
{
11+
// Publish config file
12+
$this->publishes([
13+
__DIR__ . '/../config/custom-logging.php' => config_path('custom-logging.php'),
14+
], 'custom-logging-config');
15+
}
16+
}

src/LogRecord.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Coreproc\LaravelCustomLogging;
4+
5+
class LogRecord extends \Monolog\LogRecord
6+
{
7+
public function toArray(): array
8+
{
9+
$array = parent::toArray();
10+
return array_merge(['origin' => 'app.' . config('custom-logging.container_role')], $array);
11+
}
12+
}

0 commit comments

Comments
 (0)