diff --git a/src/Data/Job.php b/src/Data/Job.php index ad533981..c7a7556c 100644 --- a/src/Data/Job.php +++ b/src/Data/Job.php @@ -6,7 +6,7 @@ use Neighborhoods\Kojo\Db\Model; use Neighborhoods\Pylon\TimeInterface; -class Job extends Model implements JobInterface +class Job extends Model implements JobInterface, \JsonSerializable { public function __construct() { @@ -324,4 +324,9 @@ public function getDeleteAfterDateTime(): \DateTime return new \DateTime($deleteAfterDateTimeString); } + + public function jsonSerialize() + { + return $this->_persistentProperties; + } } diff --git a/src/Data/JobInterface.php b/src/Data/JobInterface.php index 8481db14..171a59f6 100644 --- a/src/Data/JobInterface.php +++ b/src/Data/JobInterface.php @@ -119,4 +119,6 @@ public function getCompletedAtDateTime(): \DateTime; public function setDeleteAfterDateTime(\DateTime $deleteAfterDateTime): JobInterface; public function getDeleteAfterDateTime(): \DateTime; + + public function jsonSerialize(); } diff --git a/src/Foreman.php b/src/Foreman.php index 04a4dea4..75dba47a 100644 --- a/src/Foreman.php +++ b/src/Foreman.php @@ -43,6 +43,7 @@ protected function _workWorker(): ForemanInterface $this->setJob($this->_getSelector()->getWorkableJob()); $this->_getLocator()->setJob($this->_getJob()); try { + $this->_getLogger()->setJob($this->_getJob()); $this->_updateJobAsWorking(); $this->_runWorker(); $this->_updateJobAfterWork(); diff --git a/src/Process/Pool/Logger.php b/src/Process/Pool/Logger.php index ff152b67..57545ac8 100644 --- a/src/Process/Pool/Logger.php +++ b/src/Process/Pool/Logger.php @@ -4,6 +4,7 @@ namespace Neighborhoods\Kojo\Process\Pool; use Monolog\Formatter\NormalizerFormatter; +use Neighborhoods\Kojo\Data\JobInterface; use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface; use Neighborhoods\Kojo\ProcessInterface; use Neighborhoods\Pylon\Data\Property\Defensive; @@ -24,6 +25,8 @@ class Logger extends Log\AbstractLogger implements LoggerInterface protected $log_formatter; protected $level_filter_mask; + /** @var JobInterface */ + protected $job; public function setProcess(ProcessInterface $process): LoggerInterface { @@ -32,6 +35,31 @@ public function setProcess(ProcessInterface $process): LoggerInterface return $this; } + public function hasJob(): bool + { + return isset($this->job); + } + + public function getJob() : JobInterface + { + if ($this->job === null) { + throw new \LogicException('Logger job has not been set.'); + } + + return $this->job; + } + + public function setJob(JobInterface $job) : LoggerInterface + { + if ($this->job !== null) { + throw new \LogicException('Logger job is already set.'); + } + + $this->job = $job; + + return $this; + } + protected function _getProcess(): ProcessInterface { return $this->_read(ProcessInterface::class); @@ -53,6 +81,11 @@ public function log($level, $message, array $context = []) $logMessage->setLevel($level); $logMessage->setProcessId($processId); $logMessage->setProcessPath($this->_getProcess()->getPath()); + + if ($this->hasJob()){ + $logMessage->setKojoJob($this->getJob()); + } + $logMessage->setMessage($message); if (array_key_exists(self::CONTEXT_KEY_EXCEPTION, $context) && $context[self::CONTEXT_KEY_EXCEPTION] diff --git a/src/Process/Pool/Logger/Formatter.php b/src/Process/Pool/Logger/Formatter.php index 91711587..b579fe42 100644 --- a/src/Process/Pool/Logger/Formatter.php +++ b/src/Process/Pool/Logger/Formatter.php @@ -3,16 +3,14 @@ namespace Neighborhoods\Kojo\Process\Pool\Logger; +use DateTime; use Neighborhoods\Pylon\Data\Property\Defensive; class Formatter implements FormatterInterface { use Defensive\AwareTrait; - const PAD_PID = 6; - const PAD_PATH = 50; const PROP_PROCESS_PATH_PADDING = 'process_path_padding'; - const PROP_PROCESS_ID_PADDING = 'process_id_padding'; const PROP_LOG_FORMAT = 'log_format'; const LOG_FORMAT_PIPES = 'pipes'; @@ -46,14 +44,29 @@ public function getFormattedThrowableMessage(\Throwable $throwable): string protected function formatPipes(MessageInterface $message) : string { - $processIdPaddingLength = $this->getProcessIdPadding(); $processPathPaddingLength = $this->getProcessPathPadding(); - $processID = str_pad($message->getProcessId(), $processIdPaddingLength, ' ', STR_PAD_LEFT); $processPath = str_pad($message->getProcessPath(), $processPathPaddingLength, ' '); - $level = str_pad($message->getLevel(), 12, ' '); - - return implode(' | ', [$message->getTime(), $level, $processID, $processPath, $message->getMessage()]); + $level = str_pad($message->getLevel(), 8, ' '); + $kojoJobTypeCode = str_pad($message->hasKojoJob() ? $message->getKojoJob()->getTypeCode() : '', 26, ' '); + $kojoJobAssignedState = str_pad( + $message->hasKojoJob() ? $message->getKojoJob()->getAssignedState() : '', + 20, + ' ' + ); + + + return implode( + ' | ', + [ + DateTime::createFromFormat('D, d M y H:i:s.u T', $message->getTime())->format('H:i:s.u'), + $level, + $processPath, + $kojoJobTypeCode, + $kojoJobAssignedState, + $message->getMessage(), + ] + ); } protected function formatJson(MessageInterface $message) : string @@ -78,18 +91,6 @@ protected function getProcessPathPadding() : int return $this->_read(self::PROP_PROCESS_PATH_PADDING); } - public function setProcessIdPadding(int $processIdPadding) : FormatterInterface - { - $this->_create(self::PROP_PROCESS_ID_PADDING, $processIdPadding); - - return $this; - } - - protected function getProcessIdPadding() : int - { - return $this->_read(self::PROP_PROCESS_ID_PADDING); - } - public function setLogFormat(string $logFormat) { $this->_create(self::PROP_LOG_FORMAT, $logFormat); diff --git a/src/Process/Pool/Logger/Formatter.yml b/src/Process/Pool/Logger/Formatter.yml index ffcc8b5b..db59737b 100644 --- a/src/Process/Pool/Logger/Formatter.yml +++ b/src/Process/Pool/Logger/Formatter.yml @@ -1,11 +1,9 @@ parameters: - process.pool.logger.formatter.process_id_padding: 6 - process.pool.logger.formatter.path_padding: 80 + process.pool.logger.formatter.path_padding: 65 process.pool.logger.formatter.log_format: !php/const \Neighborhoods\Kojo\Process\Pool\Logger\Formatter::LOG_FORMAT_JSON services: neighborhoods.kojo.process.log_formatter: class: Neighborhoods\Kojo\Process\Pool\Logger\Formatter calls: - - [setProcessIdPadding, ['%process.pool.logger.formatter.process_id_padding%']] - [setProcessPathPadding, ['%process.pool.logger.formatter.path_padding%']] - [setLogFormat, ['%process.pool.logger.formatter.log_format%']] diff --git a/src/Process/Pool/Logger/FormatterInterface.php b/src/Process/Pool/Logger/FormatterInterface.php index a6760fcb..e1752f2f 100644 --- a/src/Process/Pool/Logger/FormatterInterface.php +++ b/src/Process/Pool/Logger/FormatterInterface.php @@ -11,8 +11,6 @@ public function getFormattedMessage(MessageInterface $message): string; public function setProcessPathPadding(int $processPathPadding): FormatterInterface; - public function setProcessIdPadding(int $processIdPadding): FormatterInterface; - public function setLogFormat(string $logFormat); /** diff --git a/src/Process/Pool/Logger/Message.php b/src/Process/Pool/Logger/Message.php index 1fb0dcf4..1baf4d90 100644 --- a/src/Process/Pool/Logger/Message.php +++ b/src/Process/Pool/Logger/Message.php @@ -4,6 +4,8 @@ namespace Neighborhoods\Kojo\Process\Pool\Logger; +use Neighborhoods\Kojo\Data\JobInterface; + class Message implements MessageInterface, \JsonSerializable { const KEY_TIME = 'time'; @@ -16,6 +18,7 @@ class Message implements MessageInterface, \JsonSerializable protected $level; protected $process_id; protected $process_path; + protected $kojo_job; protected $message; protected $context; protected $context_json_last_error; @@ -164,4 +167,29 @@ public function setContextJsonLastError(int $context_json_last_error): MessageIn return $this; } + + public function hasKojoJob(): bool + { + return isset($this->kojo_job); + } + + public function getKojoJob() : JobInterface + { + if ($this->kojo_job === null) { + throw new \LogicException('Message kojo_job has not been set.'); + } + + return $this->kojo_job; + } + + public function setKojoJob(JobInterface $kojo_job) : MessageInterface + { + if ($this->kojo_job !== null) { + throw new \LogicException('Message kojo_job is already set.'); + } + + $this->kojo_job = $kojo_job; + + return $this; + } } diff --git a/src/Process/Pool/Logger/MessageInterface.php b/src/Process/Pool/Logger/MessageInterface.php index 56dcc81b..6b4137b9 100644 --- a/src/Process/Pool/Logger/MessageInterface.php +++ b/src/Process/Pool/Logger/MessageInterface.php @@ -4,6 +4,8 @@ namespace Neighborhoods\Kojo\Process\Pool\Logger; +use Neighborhoods\Kojo\Data\JobInterface; + interface MessageInterface { public function getTime(): string; @@ -33,4 +35,10 @@ public function getContext(): array; public function getContextJsonLastError(): int; public function setContextJsonLastError(int $context_json_last_error): MessageInterface; + + public function getKojoJob() : JobInterface; + + public function setKojoJob(JobInterface $kojo_job) : MessageInterface; + + public function hasKojoJob() : bool; } diff --git a/src/Process/Pool/LoggerInterface.php b/src/Process/Pool/LoggerInterface.php index 6b768ec3..5067b3b0 100644 --- a/src/Process/Pool/LoggerInterface.php +++ b/src/Process/Pool/LoggerInterface.php @@ -3,6 +3,7 @@ namespace Neighborhoods\Kojo\Process\Pool; +use Neighborhoods\Kojo\Data\JobInterface; use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface; use Neighborhoods\Kojo\ProcessInterface; use Psr\Log; @@ -18,4 +19,6 @@ public function getLogFormatter() : FormatterInterface; public function setLogFormatter(FormatterInterface $log_formatter) : LoggerInterface; public function setLevelFilterMask(array $level_filter_mask): LoggerInterface; + + public function setJob(JobInterface $job) : LoggerInterface; }