Skip to content

Commit 1c6121c

Browse files
committed
v0.1
0 parents  commit 1c6121c

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
.phpunit.result.cache

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "micro/plugin-logger",
3+
"description": "Micro Framework: Logger plugin",
4+
"type": "library",
5+
"version": "0.1",
6+
"license": "MIT",
7+
"autoload": {
8+
"psr-4": {
9+
"Micro\\Plugin\\Logger\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Stanislau.Komar",
15+
"email": "stanislau_komar@epam.com"
16+
}
17+
],
18+
"minimum-stability": "stable",
19+
"require": {
20+
"monolog/monolog": "2.3.*"
21+
}
22+
}

src/Impl/LoggerFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Logger\Impl;
4+
5+
use Micro\Plugin\Logger\LoggerFactoryInterface;
6+
use Micro\Plugin\Logger\LoggerPluginConfiguration;
7+
use Monolog\Handler\AbstractProcessingHandler;
8+
use Monolog\Handler\StreamHandler;
9+
use Monolog\Logger;
10+
use Psr\Log\LoggerInterface;
11+
12+
class LoggerFactory implements LoggerFactoryInterface
13+
{
14+
public function __construct(private LoggerPluginConfiguration $configuration)
15+
{
16+
}
17+
18+
/**
19+
* {@inheritDoc}
20+
*/
21+
public function create(): LoggerInterface
22+
{
23+
$logger = new Logger($this->configuration->getLoggerDefaultName());
24+
25+
$logger->pushHandler($this->createHandler());
26+
27+
return $logger;
28+
}
29+
30+
/**
31+
* @return AbstractProcessingHandler
32+
*/
33+
protected function createHandler(): AbstractProcessingHandler
34+
{
35+
return new StreamHandler(
36+
$this->configuration->getLogFile(),
37+
$this->configuration->getLogLevel()
38+
);
39+
}
40+
}

src/LoggerFactoryInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Logger;
4+
5+
use Psr\Log\LoggerInterface;
6+
7+
interface LoggerFactoryInterface
8+
{
9+
/**
10+
* @return LoggerInterface
11+
*/
12+
public function create(): LoggerInterface;
13+
}

src/LoggerPlugin.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Logger;
4+
5+
use Micro\Component\DependencyInjection\Container;
6+
use Micro\Framework\Kernel\Plugin\AbstractPlugin;
7+
use Micro\Plugin\Logger\Impl\LoggerFactory;
8+
use \Closure;
9+
10+
class LoggerPlugin extends AbstractPlugin
11+
{
12+
public const SERVICE_LOGGER = 'logger';
13+
14+
/**
15+
* {@inheritDoc}
16+
*/
17+
public function provideDependencies(Container $container): void
18+
{
19+
$container->register(self::SERVICE_LOGGER, $this->createLoggerServiceCallback());
20+
}
21+
22+
/**
23+
* @return Closure
24+
*/
25+
private function createLoggerServiceCallback(): Closure
26+
{
27+
return function (Container $container)
28+
{
29+
return $this->createLoggerFactory()->create();
30+
};
31+
}
32+
33+
/**
34+
* @return LoggerFactoryInterface
35+
*/
36+
private function createLoggerFactory(): LoggerFactoryInterface
37+
{
38+
return new LoggerFactory($this->configuration);
39+
}
40+
}

src/LoggerPluginConfiguration.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Micro\Plugin\Logger;
4+
5+
use Micro\Framework\Kernel\Configuration\PluginConfiguration;
6+
use Monolog\Logger;
7+
8+
class LoggerPluginConfiguration extends PluginConfiguration
9+
{
10+
const LOGGER_NAME_DEFAULT = 'default';
11+
12+
const LOGGER_LOG_LEVEL_DEFAULT = 'debug';
13+
14+
const LOGGER_LOG_LEVEL = 'LOGGER_LOG_LEVEL';
15+
16+
const LOGGER_LOG_FILE = 'LOGGER_FILE';
17+
18+
const LOG_LEVEL_VALUES = [
19+
'DEBUG' => Logger::DEBUG,
20+
'CRITICAL' => Logger::CRITICAL,
21+
'EMERGENCY' => Logger::EMERGENCY,
22+
'ALERT' => Logger::ALERT,
23+
'ERROR' => Logger::ERROR,
24+
'INFO' => Logger::INFO,
25+
'WARNING' => Logger::WARNING,
26+
'NOTICE' => Logger::NOTICE,
27+
];
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getLogFile(): string
33+
{
34+
return $this->configuration->get(self::LOGGER_LOG_FILE);
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function getLoggerDefaultName(): string
41+
{
42+
return self::LOGGER_NAME_DEFAULT;
43+
}
44+
45+
/**
46+
* @return int
47+
*/
48+
public function getLogLevel(): int
49+
{
50+
$levelString = $this->configuration->get(self::LOGGER_LOG_LEVEL, self::LOGGER_LOG_LEVEL_DEFAULT);
51+
52+
return self::LOG_LEVEL_VALUES[mb_strtoupper($levelString)];
53+
}
54+
}

0 commit comments

Comments
 (0)