Skip to content

Commit

Permalink
Merge pull request #2 from peckadesign/monolog-2
Browse files Browse the repository at this point in the history
Monolog 2 a Tracy 2.6
  • Loading branch information
MilanPala authored Feb 25, 2019
2 parents 19241ef + 7546ece commit 0ca0217
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 175 deletions.
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php
php:
- 7.1
- 7.2
- 7.3

script:
- composer validate
- vendor/bin/phpcs src/ --standard=vendor/pd/coding-standard/src/PeckaCodingStandard/ruleset.xml
- vendor/bin/phpcs src/ --standard=vendor/pd/coding-standard/src/PeckaCodingStandardStrict/ruleset.xml
- vendor/bin/phpstan analyse src/ --level 2 --no-progress

before_script:
- composer install --no-interaction --prefer-dist

sudo: false

cache:
directories:
- $HOME/.composer/cache
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# MonologModule

- Vytváří `DayFileHandler`, který loguje výstup Monologu do struktury `log/kanál/YYYY-MM/YYYY-MM-DD.log`
- Umožňuje zobrazit v presenteru všechen výstup Monologu prostřednictvím FlashMessage. Vhodné např. pro administraci, kdy se zobrazí všechen výstup ze synchroních operací prováděných při požadavku obsluhy (e-maily, výstupy importů/exportů, atd.)
- Vytváří `BlueScreenHandler`, který ukládá výjimky z Tracy do `log/exception/YYYY-MM/`
- Napojuje logování z Tracy do Monologu


## Instalace

Expand All @@ -15,17 +17,38 @@ $ composer require pd/monolog-module
# common.neon
extensions:
pd.monolog: Pd\MonologModule\DI\Extension
pd.monolog: \Pd\MonologModule\DI\Extension
monolog:
pd.monolog:
name: projekt
handlers:
- Pd\MonologModule\Handlers\DayFileHandler("projekt", %logDir%)
pd.monolog:
# Povolené typy prosenterů pro zobrazení výstupu Monologu jako FlashMessage
allowedTypes:
- Pd\AdminModule\BasePresenter
services:
myService:
arguments:
logger: @\Pd\MonologModule\ChannelLoggerFactory::create('myChannel')
-
factory: \Monolog\Processor\WebProcessor
-
factory: \Pd\MonologModule\Handlers\DayFileHandler
arguments:
appName: myProjectName
logDir: %logDir%
-
factory: \Pd\MonologModule\Handlers\BlueScreenHandler
arguments:
logDir: %logDir%
pd.monolog.logger:
setup:
- pushProcessor(@\Monolog\Processor\WebProcessor)
- pushHandler(@\Pd\MonologModule\Handlers\DayFileHandler)
- pushHandler(@\Pd\CoreModule\LogModule\Handlers\NewRelicHandler)
- pushHandler(@\Pd\MonologModule\Handlers\BlueScreenHandler)
```
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{
"name": "pd/monolog-module",
"description": "Integrace Monologu do projektů PeckaDesign",
"license": "MIT",
"require": {
"kdyby/monolog": "1.3.*",
"monolog/monolog": "2.0.*@beta",
"nette/di": "2.4.*",
"nette/application": "2.4.*"
"nette/application": "2.4.*",
"nette/security": "2.4.*",
"tracy/tracy": "^2.6.0",
"kdyby/clock": "1.0.*"
},
"require-dev": {
"pd/coding-standard": "1.17.*",
"phpstan/phpstan-strict-rules": "0.11.*"
},
"autoload": {
"psr-4": {
Expand Down
85 changes: 85 additions & 0 deletions src/ChannelLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types = 1);

namespace Pd\MonologModule;

final class ChannelLogger implements \Psr\Log\LoggerInterface
{

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;

/**
* @var string
*/
private $channel;


public function __construct(\Psr\Log\LoggerInterface $logger, string $channel)
{
$this->logger = $logger;
$this->channel = $channel;
}


private function getContext(array $context): array
{
return ['channel' => $this->channel] + $context;
}


public function emergency($message, array $context = [])
{
$this->logger->emergency($message, $this->getContext($context));
}


public function alert($message, array $context = [])
{
$this->logger->alert($message, $this->getContext($context));
}


public function critical($message, array $context = [])
{
$this->logger->critical($message, $this->getContext($context));
}


public function error($message, array $context = [])
{
$this->logger->error($message, $this->getContext($context));
}


public function warning($message, array $context = [])
{
$this->logger->warning($message, $this->getContext($context));
}


public function notice($message, array $context = [])
{
$this->logger->notice($message, $this->getContext($context));
}


public function info($message, array $context = [])
{
$this->logger->info($message, $this->getContext($context));
}


public function debug($message, array $context = [])
{
$this->logger->debug($message, $this->getContext($context));
}


public function log($level, $message, array $context = [])
{
$this->logger->log($message, $this->getContext($context));
}

}
27 changes: 27 additions & 0 deletions src/ChannelLoggerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Pd\MonologModule;

final class ChannelLoggerFactory
{

/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;


public function __construct(\Psr\Log\LoggerInterface $logger)
{
$this->logger = $logger;
}


public function create(string $channel): \Psr\Log\LoggerInterface
{
$logger = new ChannelLogger($this->logger, $channel);

return $logger;
}

}
39 changes: 18 additions & 21 deletions src/DI/Extension.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
<?php
<?php declare(strict_types = 1);

namespace Pd\MonologModule\DI;

use Kdyby;
use Nette;
use Pd;


class Extension extends Nette\DI\CompilerExtension
final class Extension extends \Nette\DI\CompilerExtension
{

private $defaults = [
'allowedTypes' => [
],
'name' => '',
];


public function beforeCompile()
public function loadConfiguration()
{
parent::loadConfiguration();

$containerBuilder = $this->getContainerBuilder();

$config = $this->validateConfig($this->defaults);

$presenterBridge = $containerBuilder
->addDefinition($this->prefix('presenterBridge'))
->setClass(PresenterBridge::class, [$config['allowedTypes']])
$containerBuilder
->addDefinition($this->prefix('channelLoggerFactory'))
->setFactory(\Pd\MonologModule\ChannelLoggerFactory::class)
;

$application = $containerBuilder->getDefinition($containerBuilder->getByType(Nette\Application\Application::class));
$application->addSetup('?->onPresenter[] = ?', ['@self', [$presenterBridge, 'onPresenter']]);
$containerBuilder
->addDefinition($this->prefix('logger'))
->setType(\Monolog\Logger::class)
->setFactory(\Monolog\Logger::class, ['name' => $config['name']])
;
}


public function setCompiler(Nette\DI\Compiler $compiler, $name)
public function afterCompile(\Nette\PhpGenerator\ClassType $class)
{
$parent = parent::setCompiler($compiler, $name);

$monologExtension = new Kdyby\Monolog\DI\MonologExtension();
$compiler->addExtension('monolog', $monologExtension);
$initialize = $class->getMethod('initialize');

return $parent;
$initialize->addBody('$tracyLogger = new \Tracy\Bridges\Psr\PsrToTracyLoggerAdapter($this->getByType(\Psr\Log\LoggerInterface::class));');
$initialize->addBody('\Tracy\Debugger::setLogger($tracyLogger);');
}

}
56 changes: 0 additions & 56 deletions src/DI/PresenterBridge.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Handlers/BlueScreenHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace Pd\MonologModule\Handlers;

final class BlueScreenHandler extends \Monolog\Handler\AbstractProcessingHandler
{

/**
* @var \Tracy\BlueScreen
*/
private $blueScreenRenderer;

/**
* @var string
*/
private $logDirectory;


public function __construct(string $logDir, $level = \Monolog\Logger::DEBUG)
{
parent::__construct($level, TRUE);

$this->blueScreenRenderer = new \Tracy\BlueScreen();
$this->logDirectory = $logDir;
}


private function getLogDirectory(\DateTimeInterface $dateTime)
{
$pathParts = [
$this->logDirectory,
'exception',
$dateTime->format('Y-m'),
];

return \implode('/', $pathParts);
}


protected function write(array $record): void
{
if ( ! isset($record['context']['exception'])) {
return;
}

$logDirectory = $this->getLogDirectory($record['datetime']);
\Nette\Utils\FileSystem::createDir($logDirectory);

$extensionLogger = new \Tracy\Logger($logDirectory);

$exception = $record['context']['exception'];
$exceptionFile = $extensionLogger->getExceptionFile($exception);

$this->blueScreenRenderer->renderToFile($exception, $exceptionFile);
}

}
Loading

0 comments on commit 0ca0217

Please sign in to comment.