Skip to content

Commit

Permalink
Merge pull request #6 from neighborhoods/feature/OPS-981
Browse files Browse the repository at this point in the history
Feature/OPS-981 | Add configurable log formatter with JSON support
  • Loading branch information
alexberryman committed Jun 29, 2018
2 parents 98a6798 + 41df82b commit 464e2d9
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Environment/Parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ parameters:
neighborhoods.kojo.environment.parameters.database_password: ''
neighborhoods.kojo.environment.parameters.database_adapter: ''
neighborhoods.kojo.environment.parameters.database_host: ''
neighborhoods.kojo.environment.parameters.database_name: ''
neighborhoods.kojo.environment.parameters.database_name: ''
70 changes: 34 additions & 36 deletions src/Process/Pool/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,87 @@

namespace Neighborhoods\Kojo\Process\Pool;

use Psr\Log;
use Neighborhoods\Pylon\Time;
use Neighborhoods\Kojo\Process\Pool\Logger\FormatterInterface;
use Neighborhoods\Kojo\ProcessInterface;
use Neighborhoods\Pylon\Data\Property\Defensive;
use Neighborhoods\Pylon\Time;
use Psr\Log;

class Logger extends Log\AbstractLogger implements LoggerInterface
{
use Time\AwareTrait;
use Logger\Message\Factory\AwareTrait;
use Defensive\AwareTrait;
const PAD_PID = 6;
const PAD_PATH = 50;
const PROP_IS_ENABLED = 'is_enabled';
const PROP_PROCESS_PATH_PADDING = 'process_path_padding';
const PROP_PROCESS_ID_PADDING = 'process_id_padding';
const PROP_IS_ENABLED = 'is_enabled';

public function setProcess(ProcessInterface $process): LoggerInterface
protected $log_formatter;

public function setProcess(ProcessInterface $process) : LoggerInterface
{
$this->_createOrUpdate(ProcessInterface::class, $process);

return $this;
}

protected function _getProcess(): ProcessInterface
protected function _getProcess() : ProcessInterface
{
return $this->_read(ProcessInterface::class);
}

public function log($level, $message, array $context = [])
{
if ($this->_isEnabled() === true) {
$processIdPadding = $this->_getProcessIdPadding();
$processPathPadding = $this->_getProcessPathPadding();

if ($this->_exists(ProcessInterface::class)) {
$processId = (string)$this->_getProcess()->getProcessId();
$paddedProcessId = str_pad($processId, $processIdPadding, ' ', STR_PAD_LEFT);
$typeCode = str_pad($this->_getProcess()->getPath(), $processPathPadding, ' ');
}else {
$paddedProcessId = str_pad('', $processIdPadding, '?', STR_PAD_LEFT);
$typeCode = str_pad('', $processPathPadding, '?');
} else {
$processId = '?';
}

$level = str_pad($level, 12, ' ');
$referenceTime = $this->_getTime()->getUnixReferenceTimeNow();
$format = "%s | %s | %s | %s | %s\n";
fwrite(STDOUT, sprintf($format, $referenceTime, $level, $paddedProcessId, $typeCode, $message));

$logMessage = $this->getProcessPoolLoggerMessageFactory()->create();
$logMessage->setTime($referenceTime);
$logMessage->setLevel($level);
$logMessage->setProcessId($processId);
$logMessage->setTypeCode($this->_getProcess()->getPath());
$logMessage->setMessage($message);

fwrite(STDOUT, $this->getLogFormatter()->getFormattedMessage($logMessage) . "\n");
}

return;
}

protected function _isEnabled(): bool
protected function _isEnabled() : bool
{
return $this->_read(self::PROP_IS_ENABLED);
}

public function setIsEnabled(bool $isEnabled): LoggerInterface
public function setIsEnabled(bool $isEnabled) : LoggerInterface
{
$this->_create(self::PROP_IS_ENABLED, $isEnabled);

return $this;
}

public function setProcessPathPadding(int $processPathPadding): LoggerInterface
public function getLogFormatter() : FormatterInterface
{
$this->_create(self::PROP_PROCESS_PATH_PADDING, $processPathPadding);
if ($this->log_formatter === null) {
throw new \LogicException('Logger log_formatter has not been set.');
}

return $this;
return $this->log_formatter;
}

protected function _getProcessPathPadding(): int
public function setLogFormatter(FormatterInterface $log_formatter) : LoggerInterface
{
return $this->_read(self::PROP_PROCESS_PATH_PADDING);
}
if ($this->log_formatter !== null) {
throw new \LogicException('Logger log_formatter already set.');
}

public function setProcessIdPadding(int $processIdPadding): LoggerInterface
{
$this->_create(self::PROP_PROCESS_ID_PADDING, $processIdPadding);
$this->log_formatter = $log_formatter;

return $this;
}

protected function _getProcessIdPadding(): int
{
return $this->_read(self::PROP_PROCESS_ID_PADDING);
}
}
}
8 changes: 3 additions & 5 deletions src/Process/Pool/Logger.yml
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
87 changes: 87 additions & 0 deletions src/Process/Pool/Logger/Formatter.php
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);
}
}
11 changes: 11 additions & 0 deletions src/Process/Pool/Logger/Formatter.yml
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%']]
17 changes: 17 additions & 0 deletions src/Process/Pool/Logger/FormatterInterface.php
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);
}
Loading

0 comments on commit 464e2d9

Please sign in to comment.