-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from neighborhoods/feature/OPS-981
Feature/OPS-981 | Add configurable log formatter with JSON support
- Loading branch information
Showing
15 changed files
with
448 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
parameters: | ||
process.pool.logger.process_id_padding: 6 | ||
process.pool.logger.path_padding: 80 | ||
process.pool.logger.is_enabled: true | ||
services: | ||
neighborhoods.kojo.process.pool.logger: | ||
class: Neighborhoods\Kojo\Process\Pool\Logger | ||
calls: | ||
- [setIsEnabled, ['%process.pool.logger.is_enabled%']] | ||
- [setTime, ['@neighborhoods.pylon.time']] | ||
- [setProcessIdPadding, ['%process.pool.logger.process_id_padding%']] | ||
- [setProcessPathPadding, ['%process.pool.logger.path_padding%']] | ||
- [setLogFormatter, ['@neighborhoods.kojo.process.log_formatter']] | ||
- [setProcessPoolLoggerMessageFactory, ['@neighborhoods.kojo.process.pool.logger.message.factory']] | ||
process.pool.logger: | ||
alias: neighborhoods.kojo.process.pool.logger | ||
alias: neighborhoods.kojo.process.pool.logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Neighborhoods\Kojo\Process\Pool\Logger; | ||
|
||
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'; | ||
const LOG_FORMAT_JSON = 'json'; | ||
|
||
public function getFormattedMessage(MessageInterface $message) : string | ||
{ | ||
if ($this->hasLogFormat() && $this->getLogFormat() === self::LOG_FORMAT_PIPES) { | ||
return $this->formatPipes($message); | ||
} else { | ||
return $this->formatJson($message); | ||
} | ||
} | ||
|
||
protected function formatPipes(MessageInterface $message) : string | ||
{ | ||
$processIdPaddingLength = $this->getProcessIdPadding(); | ||
$processPathPaddingLength = $this->getProcessPathPadding(); | ||
|
||
$processID = str_pad($message->getProcessId(), $processIdPaddingLength, ' ', STR_PAD_LEFT); | ||
$typeCode = str_pad($message->getTypeCode(), $processPathPaddingLength, ' '); | ||
$level = str_pad($message->getLevel(), 12, ' '); | ||
|
||
return implode(' | ', [$message->getTime(), $level, $processID, $typeCode, $message->getMessage()]); | ||
} | ||
|
||
protected function formatJson(MessageInterface $message) : string | ||
{ | ||
return json_encode($message); | ||
} | ||
|
||
public function setProcessPathPadding(int $processPathPadding) : FormatterInterface | ||
{ | ||
$this->_create(self::PROP_PROCESS_PATH_PADDING, $processPathPadding); | ||
|
||
return $this; | ||
} | ||
|
||
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); | ||
|
||
return $this; | ||
} | ||
|
||
protected function getLogFormat() : string | ||
{ | ||
return $this->_read(self::PROP_LOG_FORMAT); | ||
} | ||
|
||
protected function hasLogFormat() : bool | ||
{ | ||
return $this->_exists(self::PROP_LOG_FORMAT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
parameters: | ||
process.pool.logger.formatter.process_id_padding: 6 | ||
process.pool.logger.formatter.path_padding: 80 | ||
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%']] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neighborhoods\Kojo\Process\Pool\Logger; | ||
|
||
|
||
interface FormatterInterface | ||
{ | ||
public function getFormattedMessage(MessageInterface $message) : string; | ||
|
||
public function setProcessPathPadding(int $processPathPadding) : FormatterInterface; | ||
|
||
public function setProcessIdPadding(int $processIdPadding) : FormatterInterface; | ||
|
||
public function setLogFormat(string $logFormat); | ||
} |
Oops, something went wrong.