Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbjr committed Mar 24, 2024
0 parents commit be0ead1
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Laravel Custom Logging

This package provides us with an additional `origin` variable for logs to determine the source of the log.

## Installation

You can install the package via composer:

```bash
composer require coreproc/laravel-custom-logging
```

Publish the config file:

```bash
php artisan vendor:publish --provider="Coreproc\LaravelCustomLogging\CustomLoggingServiceProvider"
```

## Usage

Typically, we use this in the `stderr` channel. Here is an example of the configuration:

```php
'channels' => [
'stderr' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => StreamHandler::class,
'formatter' => Monolog\Formatter\JsonFormatter::class,
'formatter_with' => [
'includeStacktraces' => true,
'batchMode' => Monolog\Formatter\JsonFormatter::BATCH_MODE_JSON,
'appendNewline' => true,
],
'with' => [
'stream' => 'php://stderr',
],
'processors' => [\Coreproc\LaravelCustomLogging\AddOriginProcessor::class],
],
],
```
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "coreproc/laravel-custom-logging",
"description": "Custom logging for AWS cloudwatch containers",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Coreproc\\LaravelCustomLogging\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Coreproc\\LaravelCustomLogging\\CustomLoggingServiceProvider"
]
}
},
"authors": [
{
"name": "Chris Bautista",
"email": "[email protected]"
}
],
"require": {}
}
7 changes: 7 additions & 0 deletions config/custom-logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'container_role' => env('CONTAINER_ROLE', 'web'),

];
21 changes: 21 additions & 0 deletions src/AddOriginProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Coreproc\LaravelCustomLogging;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

class AddOriginProcessor implements ProcessorInterface
{
public function __invoke(LogRecord $record): array|LogRecord
{
return new \Coreproc\LaravelCustomLogging\LogRecord(
$record->datetime,
$record->channel,
$record->level,
$record->message,
$record->context,
$record->extra
);
}
}
16 changes: 16 additions & 0 deletions src/CustomLoggingServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Coreproc\LaravelCustomLogging;

use Illuminate\Support\ServiceProvider;

class CustomLoggingServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Publish config file
$this->publishes([
__DIR__ . '/../config/custom-logging.php' => config_path('custom-logging.php'),
], 'custom-logging-config');
}
}
12 changes: 12 additions & 0 deletions src/LogRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Coreproc\LaravelCustomLogging;

class LogRecord extends \Monolog\LogRecord
{
public function toArray(): array
{
$array = parent::toArray();
return array_merge(['origin' => 'app.' . config('custom-logging.container_role')], $array);
}
}

0 comments on commit be0ead1

Please sign in to comment.