Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel logs refactor #376

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
"open-telemetry/transport-grpc": "^1.0.0",
"open-telemetry/exporter-otlp": "^1.0.0",
"nesbot/carbon": "^2.71",
"moneyphp/money": "^4.1.0"
"moneyphp/money": "^4.1.0",
"timacdonald/log-fake": "^2.0"
},
"conflict": {
"symfony/doctrine-messenger": ">7.0.5 < 7.1.0"
Expand Down
5 changes: 3 additions & 2 deletions packages/Laravel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"symfony/expression-language": "^6.0|^7.0",
"nesbot/carbon": "^2.71",
"moneyphp/money": "^4.1.0",
"ecotone/dbal": "~1.230.1"
"ecotone/dbal": "~1.230.1",
"timacdonald/log-fake": "^2.0"
},
"extra": {
"laravel": {
Expand Down Expand Up @@ -96,4 +97,4 @@
"wikimedia/composer-merge-plugin": true
}
}
}
}
119 changes: 119 additions & 0 deletions packages/Laravel/config/logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/

'default' => env('LOG_CHANNEL', 'stack'),

/*
|--------------------------------------------------------------------------
| Deprecations Log Channel
|--------------------------------------------------------------------------
|
| This option controls the log channel that should be used to log warnings
| regarding deprecated PHP and library features. This allows you to get
| your application ready for upcoming major versions of dependencies.
|
*/

'deprecations' => env('LOG_DEPRECATIONS_CHANNEL', 'null'),

/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog",
| "custom", "stack"
|
*/

'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],

'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],

'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],

'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => env('LOG_LEVEL', 'critical'),
],

'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class),
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
'connectionString' => 'tls://'.env('PAPERTRAIL_URL').':'.env('PAPERTRAIL_PORT'),
],
],

'stderr' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],

'syslog' => [
'driver' => 'syslog',
'level' => env('LOG_LEVEL', 'debug'),
],

'errorlog' => [
'driver' => 'errorlog',
'level' => env('LOG_LEVEL', 'debug'),
],

'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],

'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],

];
28 changes: 0 additions & 28 deletions packages/Laravel/src/CombinedLogger.php

This file was deleted.

6 changes: 1 addition & 5 deletions packages/Laravel/src/EcotoneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ public function boot()
$this->app->singleton(
LoggingHandlerBuilder::LOGGER_REFERENCE,
function (Application $app) {
if ($app->runningInConsole()) {
return new CombinedLogger($app->get('log'), new EchoLogger());
}

return $app->get('log');
return new LaravelLogger();
}
);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/Laravel/src/LaravelLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Ecotone\Laravel;

use Illuminate\Support\Facades\Log;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;

/**
* licence Apache-2.0
*/
class LaravelLogger extends AbstractLogger implements LoggerInterface
{
public function log($level, $message, array $context = []): void
{
Log::log($level, (string) $message, $context);
}
}
32 changes: 32 additions & 0 deletions packages/Laravel/tests/Application/Execution/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
use Ecotone\Modelling\QueryBus;
use Illuminate\Foundation\Http\Kernel;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\Log;
use Test\Ecotone\Laravel\Fixture\User\User;
use Test\Ecotone\Laravel\Fixture\User\UserRepository;
use TiMacDonald\Log\LogEntry;
use TiMacDonald\Log\LogFake;

/**
* @internal
Expand Down Expand Up @@ -86,4 +89,33 @@ public function test_it_boots_messaging_system_with_test_support(): void
$messagingSystem->getGatewayByName(MessagingTestSupport::class)
);
}

public function test_logs_are_collected()
{
$app = $this->createApplication();
/** @var CommandBus $commandBus */
$commandBus = $app->get(CommandBus::class);
/** @var QueryBus $queryBus */
$queryBus = $app->get(QueryBus::class);

$amount = 123;
$commandBus->sendWithRouting('setAmount', ['amount' => $amount]);

$logFake = LogFake::bind();

$this->assertEquals(
$amount,
$queryBus->sendWithRouting('getAmount')
);

$logs = $logFake->allLogs();
$this->assertStringContainsString(
'Sending Query Message',
$logs[0]->message
);
$this->assertStringContainsString(
'Executing Query Handler',
$logs[1]->message
);
}
}
37 changes: 0 additions & 37 deletions packages/Laravel/tests/CombinedLoggerTest.php

This file was deleted.

Loading