Skip to content

Commit

Permalink
See the discussion thread for this release for
Browse files Browse the repository at this point in the history
changes.
Signed-off-by: Joey Smith <jsmith@webinertia.net>

Signed-off-by: Joey Smith <jsmith@webinertia.net>
  • Loading branch information
tyrsson committed Jun 27, 2023
1 parent 304e9e3 commit 447a1a4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webinertia/webinertia-log",
"description": "Webinertia Log",
"description": "Webinertia Log provides event driven logging for Laminas MVC applications",
"license": "BSD-3-Clause",
"keywords": [
"webinertia",
Expand Down
28 changes: 8 additions & 20 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Webinertia\Log;

use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\Logger;
use Psr\Log\LoggerInterface;

Expand All @@ -19,10 +18,15 @@ public function getDependencyConfig(): array
];
}

public function getDbAdapterConfig(): array
public function getLogSettings(): array
{
return [
'driver' => 'pdo_mysql',
'log_settings' => [
'log_errors' => true,
'log_exceptions' => true,
'log_table_name' => 'log',
'log_time_format' => 'm-d-Y H:i:s',
],
];
}

Expand All @@ -49,26 +53,10 @@ public function getPsrLogConfig(): array
{
return [
LoggerInterface::class => [
'writers' => [
'db' => [
'name' => 'db',
'priority' => Logger::INFO,
'options' => [
'table' => 'log',
'db' => AdapterInterface::class,
'formatter' => [
'name' => 'db',
'options' => [
'dateTimeFormat' => 'm-d-Y H:i:s',
],
],
],
],
],
'processors' => [
'psrplaceholder' => [
'name' => Processors\PsrPlaceholder::class,
'priority' => Logger::INFO,
'priority' => Logger::DEBUG,
],
],
],
Expand Down
42 changes: 40 additions & 2 deletions src/LogListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,59 @@

namespace Webinertia\Log;

use Webinertia\Log\LogListener;
use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Log\Formatter\Db as Formatter;
use Laminas\Log\Formatter\Json;
use Laminas\Log\Writer\Db;
use Laminas\Log\Writer\Stream;
use Laminas\Mvc\I18n\Translator;
use Laminas\ServiceManager\Factory\FactoryInterface;
use SplFileInfo;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Webinertia\Log\LogListener;

class LogListenerFactory implements FactoryInterface
{
private const LOG_FILE = __DIR__ . '/../../../../data/log/app.log';
/** @inheritDoc */
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): LogListener
{
$config = $container->get('config');
$logSettings = $config['app_settings']['log_settings'];
// we need the Laminas Logger instance to setup the writers
/** @var \Laminas\Log\Logger $logger */
$logger = $container->get(LoggerInterface::class)->getLogger();
if (isset($config['db']) && $config['db'] !== [] && $container->has(AdapterInterface::class)) {
$dbAdapter = $container->get(AdapterInterface::class);
if ($dbAdapter instanceof Adapter) { // if this passes we have a configured connection
$dbConfig = [
'db' => $dbAdapter,
'table' => $logSettings['log_table_name'] ?? 'log',
];
$dbWriter = new Db($dbConfig);
$dbFormatter = new Formatter($logSettings['log_time_format'] ?? null);
$dbWriter->setFormatter($dbFormatter);
$logger->addWriter($dbWriter);
}
} else {
$logFileInfo = new SplFileInfo(self::LOG_FILE);
$streamConfig = [
'stream' => $logFileInfo->getRealPath(),
'mode' => 'a+',
'chmod' => 0644,
];
$stream = new Stream($streamConfig);
$jsonFormatter = new Json();
$jsonFormatter->setDateTimeFormat($logSettings['log_time_format']);
$stream->setFormatter($jsonFormatter);
$logger->addWriter($stream);
}
return new $requestedName(
$container->get(LoggerInterface::class),
$container->get(Translator::class),
$container->get('config')['app_settings']
$container->get('config')
);
}
}
3 changes: 1 addition & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public function getConfig(): array
{
$configProvider = new ConfigProvider();
return [
'app_settings' => [], // this key is here to prevent errors when running stand alone
'db' => $configProvider->getDbAdapterConfig(),
'app_settings' => $configProvider->getLogSettings(),
'listeners' => $configProvider->getListenerConfig(),
'log_processors' => $configProvider->getLogProcessorConfig(),
'psr_log' => $configProvider->getPsrLogConfig(),
Expand Down
3 changes: 1 addition & 2 deletions src/Processors/PsrPlaceholder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Laminas\Log\Processor\PsrPlaceholder as Placeholder;
use Laminas\I18n\Translator\TranslatorAwareInterface;
use Laminas\I18n\Translator\TranslatorAwareTrait;
use Webinertia\User\Service\UserService;

use function array_merge;

Expand All @@ -18,7 +17,7 @@ final class PsrPlaceholder extends Placeholder implements TranslatorAwareInterfa
/** @var UserServiceInterface $userService */
protected $userService;

public function __construct(?UserService $userService = null)
public function __construct(?UserServiceInterface $userService = null)
{
$this->userService = $userService;
}
Expand Down

0 comments on commit 447a1a4

Please sign in to comment.