diff --git a/symfony-server/config/packages/monolog.yaml b/symfony-server/config/packages/monolog.yaml index b0ab62d..762d135 100644 --- a/symfony-server/config/packages/monolog.yaml +++ b/symfony-server/config/packages/monolog.yaml @@ -40,15 +40,16 @@ when@test: when@prod: monolog: handlers: + filter_cli: + type: service + id: App\Services\MonologCliFilterHandler + handler: file file: type: rotating_file path: "%kernel.logs_dir%/%kernel.environment%.log" level: info channels: ["!deprecation"] max_files: 30 - filter_cli: - type: non_sapi_handler - handler: slack slack: type: slack token: '%slack.botToken%' diff --git a/symfony-server/src/MonologNonSAPIHandler.php b/symfony-server/src/MonologNonSAPIHandler.php deleted file mode 100644 index aa13564..0000000 --- a/symfony-server/src/MonologNonSAPIHandler.php +++ /dev/null @@ -1,40 +0,0 @@ -isRunningFromCli(); - } - - public function handle(LogRecord $record): bool { - if ( !$this->isRunningFromCli() ) { - $this->getHandler($record)->handle($record); - } - return $this->bubble; - } - - private function isRunningFromCli(): bool { - return PHP_SAPI == "cli"; - } - - public function getHandler(LogRecord $record = null): HandlerInterface { - if (!$this->handler instanceof HandlerInterface) { - $handler = ($this->handler)($record, $this); - if (!$handler instanceof HandlerInterface) { - throw new \RuntimeException("The factory Closure should return a HandlerInterface"); - } - $this->handler = $handler; - } - - return $this->handler; - } -} diff --git a/symfony-server/src/Services/MonologCliFilterHandler.php b/symfony-server/src/Services/MonologCliFilterHandler.php new file mode 100644 index 0000000..6d32b69 --- /dev/null +++ b/symfony-server/src/Services/MonologCliFilterHandler.php @@ -0,0 +1,73 @@ +. +*/ + +namespace App\Services; + +use Monolog\Level; +use Monolog\Logger; +use Monolog\LogRecord; +use Monolog\Handler\Handler; +use Monolog\Handler\HandlerInterface; +use Monolog\Handler\ProcessableHandlerInterface; +use Monolog\Handler\ProcessableHandlerTrait; + + +class MonologCliFilterHandler extends Handler implements ProcessableHandlerInterface +{ + use ProcessableHandlerTrait; + + public function __construct(private bool $bubble = true) {} + public function isHandling(LogRecord $record): bool + { + //return !$this->isRunningFromCli(); + return true; + } + + public function handle(LogRecord $record): bool { + //if ( !$this->isRunningFromCli() ) { + // $this->getHandler($record)->handle($record); + //} + $r = new LogRecord( + $record->datetime, + $record->channel, + $record->level, + "tempGT: from cli: " . $this->isRunningFromCli() . ": " . $record->message, + $record->context, + $record->extra, + $record->formatted + ); + $this->getHandler($r)->handle($r); + return $this->bubble; + } + + private function isRunningFromCli(): bool { + return PHP_SAPI == "cli"; + } + + public function getHandler(LogRecord $record = null): HandlerInterface { + //if (!$this->handler instanceof HandlerInterface) { + // $handler = ($this->handler)($record, $this); + // if (!$handler instanceof HandlerInterface) { + // throw new \RuntimeException("The factory Closure should return a HandlerInterface"); + // } + // $this->handler = $handler; + //} + + return $this->processors[0]; + } +}