Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
#196: update ElasticsearchHandlerFactory (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmi20 committed Jun 20, 2022
1 parent e637163 commit a1d229e
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ updates:
- "mimmi20"
labels:
- "dependencies"
versioning-strategy: "increase-if-necessary"
versioning-strategy: "widen"
commit-message:
include: "scope"
prefix: "composer"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -780,4 +780,4 @@ jobs:
verbose: false

- name: "Run mutation tests with infection/infection"
run: "infection -s --min-covered-msi=88 --min-msi=88 --coverage=.build/coverage --only-covered --logger-github"
run: "infection -s --min-covered-msi=91 --min-msi=91 --coverage=.build/coverage --only-covered --logger-github"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,8 @@ return [
'options' => [
'client' => 'my-service', // Elastica Client object. Must be a valid container service
'index' => 'log', // Optional: Elastic index name
'dateFormat' => \Mimmi20\LoggerFactory\Handler\ElasticsearchHandlerFactory::INDEX_PER_DAY, // Optional: possible Values are \Mimmi20\LoggerFactory\Handler\ElasticsearchHandlerFactory::INDEX_PER_DAY, \Mimmi20\LoggerFactory\Handler\ElasticsearchHandlerFactory::INDEX_PER_MONTH and \Mimmi20\LoggerFactory\Handler\ElasticsearchHandlerFactory::INDEX_PER_YEAR
'indexNameFormat' => '{indexname}', // Optional: a string which must contain the string '{indexname}' (which is a placeholder for the `index`) and may contain the string '{date}' (which is a placeholder for the actual date formatted by `dateFormat`)
'type' => 'record', // Optional: Elastic document type
'ignoreError' => false, // Optional: Suppress Elastica exceptions
'level' => \Psr\Log\LogLevel::DEBUG, // Optional: The minimum logging level at which this handler will be triggered
Expand Down
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,39 @@
"php": "^7.4.3 || ^8.0.0",
"ext-mbstring": "*",
"laminas/laminas-log": "^2.15.1",
"monolog/monolog": "^2.6.0"
"monolog/monolog": "^2.7.0"
},
"require-dev": {
"actived/microsoft-teams-notifier": "^1.2.0",
"aws/aws-sdk-php": "^3.224.2",
"aws/aws-sdk-php": "^3.227.1",
"bartlett/monolog-callbackfilterhandler": "^2.0.0",
"cmdisp/monolog-microsoft-teams": "^1.2.0",
"doctrine/couchdb": "1.0.0-beta4",
"elasticsearch/elasticsearch": "^v7.17.0 || ^v8.2.2",
"elasticsearch/elasticsearch": "^v7.17.0 || ^v8.2.3",
"graylog2/gelf-php": "^1.7.1",
"guzzlehttp/guzzle": "^7.4.3",
"guzzlehttp/psr7": "^2.2.1",
"guzzlehttp/guzzle": "^7.4.4",
"guzzlehttp/psr7": "^2.3.0",
"jk/monolog-request-header-processor": "^1.0.0",
"laminas/laminas-config": "^3.7.0",
"laminas/laminas-dependency-plugin": "^2.2.0",
"laminas/laminas-modulemanager": "^2.11.0",
"laminas/laminas-servicemanager": "^3.11.2",
"laminas/laminas-servicemanager": "^3.12.0",
"mikey179/vfsstream": "^1.6.10",
"mimmi20/coding-standard": "^2.8.1",
"mimmi20/coding-standard": "^2.9.0",
"mimmi20/monolog-streamformatter": "^1.0.0",
"php-amqplib/php-amqplib": "^3.2.0",
"php-console/php-console": "^3.1.8",
"phpstan/extension-installer": "^1.1.0",
"phpstan/phpstan": "^1.7.8",
"phpstan/phpstan": "^1.7.15",
"phpstan/phpstan-deprecation-rules": "^1.0.0",
"phpstan/phpstan-phpunit": "^1.1.1",
"phpunit/phpunit": "^9.5.20",
"predis/predis": "^1.1.10",
"phpunit/phpunit": "^9.5.21",
"predis/predis": "^1.1.10 || ^2.0.0",
"rollbar/rollbar": "^v2.1.0 || ^v3.1.3",
"ruflin/elastica": "^7.1.5",
"swiftmailer/swiftmailer": "^6.3.0",
"symfony/mailer": "^v5.4.8 || ^v6.1.0",
"symfony/mime": "^v5.4.9 || ^v6.1.0"
"symfony/mailer": "^v5.4.8 || ^v6.1.1",
"symfony/mime": "^v5.4.9 || ^v6.1.1"
},
"suggest": {
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
Expand Down
36 changes: 30 additions & 6 deletions src/Handler/ElasticsearchHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
use function array_key_exists;
use function assert;
use function class_exists;
use function date;
use function get_class;
use function gettype;
use function in_array;
use function is_array;
use function is_object;
use function is_string;
use function mb_strpos;
use function sprintf;
use function str_replace;

/**
* @phpstan-import-type Level from Logger
Expand All @@ -47,6 +51,10 @@ final class ElasticsearchHandlerFactory implements FactoryInterface
use AddFormatterTrait;
use AddProcessorTrait;

public const INDEX_PER_DAY = 'Y-m-d';
public const INDEX_PER_MONTH = 'Y-m';
public const INDEX_PER_YEAR = 'Y';

/**
* @param string $requestedName
* @param array<string, (string|int|bool|V7Client|V8Client)>|null $options
Expand Down Expand Up @@ -132,16 +140,32 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $
}
}

$index = 'monolog';
$type = 'record';
$ignoreError = false;
$level = LogLevel::DEBUG;
$bubble = true;
$index = 'monolog';
$dateFormat = self::INDEX_PER_DAY;
$indexNameFormat = '{indexname}';
$type = 'record';
$ignoreError = false;
$level = LogLevel::DEBUG;
$bubble = true;

if (array_key_exists('index', $options)) {
$index = $options['index'];
}

if (
array_key_exists('dateFormat', $options)
&& in_array($options['dateFormat'], [self::INDEX_PER_DAY, self::INDEX_PER_MONTH, self::INDEX_PER_YEAR], true)
) {
$dateFormat = $options['dateFormat'];
}

if (
array_key_exists('indexNameFormat', $options)
&& false !== mb_strpos($options['indexNameFormat'], '{indexname}')
) {
$indexNameFormat = $options['indexNameFormat'];
}

if (array_key_exists('type', $options)) {
$type = $options['type'];
}
Expand All @@ -161,7 +185,7 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $
$handler = new ElasticsearchHandler(
$client,
[
'index' => $index,
'index' => str_replace(['{indexname}', '{date}'], [$index, date($dateFormat)], $indexNameFormat),
'type' => $type,
'ignore_error' => $ignoreError,
],
Expand Down
Loading

0 comments on commit a1d229e

Please sign in to comment.