Updated config so that shlink logger dynamically uses standard output when running with swoole

This commit is contained in:
Alejandro Celaya 2018-11-24 09:29:51 +01:00
parent 16590b2dbb
commit 2fc2ad98aa
6 changed files with 37 additions and 4 deletions

View File

@ -3,8 +3,11 @@
declare(strict_types=1);
use Interop\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Exec\ExecutionContext;
use Symfony\Component\Console\Application as CliApp;
/** @var ContainerInterface $container */
$container = include __DIR__ . '/../config/container.php';
putenv(sprintf('CURRENT_SHLINK_CONTEXT=%s', ExecutionContext::CLI));
$container->get(CliApp::class)->run();

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor;
use const PHP_EOL;
@ -19,12 +20,17 @@ return [
],
'handlers' => [
'rotating_file_handler' => [
'shlink_log_handler' => Common\Exec\ExecutionContext::currentContextIsSwoole() ? [
'class' => StreamHandler::class,
'level' => Logger::INFO,
'stream' => 'php://stdout',
'formatter' => 'dashed',
] : [
'class' => RotatingFileHandler::class,
'level' => Logger::INFO,
'filename' => 'data/log/shlink_log.log',
'max_files' => 30,
'formatter' => 'dashed',
'max_files' => 30,
],
],
@ -39,7 +45,7 @@ return [
'loggers' => [
'Shlink' => [
'handlers' => ['rotating_file_handler'],
'handlers' => ['shlink_log_handler'],
'processors' => ['exception_with_new_line', 'psr3'],
],
],

View File

@ -1,11 +1,13 @@
<?php
declare(strict_types=1);
use Monolog\Logger;
return [
'logger' => [
'handlers' => [
'rotating_file_handler' => [
'shlink_log_handler' => [
'level' => Logger::DEBUG,
],
],

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Common\Exec;
use const PHP_SAPI;
use function Shlinkio\Shlink\Common\env;
abstract class ExecutionContext
{
public const WEB = 'shlink_web';
public const CLI = 'shlink_cli';
public static function currentContextIsSwoole(): bool
{
return PHP_SAPI === 'cli' && env('CURRENT_SHLINK_CONTEXT', self::WEB) === self::WEB;
}
}

View File

@ -56,4 +56,5 @@
<file>config</file>
<file>public/index.php</file>
<exclude-pattern>config/params/*</exclude-pattern>
<exclude-pattern>public/index.php</exclude-pattern>
</ruleset>

View File

@ -2,8 +2,11 @@
declare(strict_types=1);
use Psr\Container\ContainerInterface;
use Shlinkio\Shlink\Common\Exec\ExecutionContext;
use Zend\Expressive\Application;
/** @var ContainerInterface $container */
$container = include __DIR__ . '/../config/container.php';
putenv(sprintf('CURRENT_SHLINK_CONTEXT=%s', ExecutionContext::WEB));
$container->get(Application::class)->run();