friendica/src/Core/Logger/Factory/Logger.php

68 lines
2 KiB
PHP
Raw Normal View History

2018-12-30 21:42:56 +01:00
<?php
2020-02-09 15:45:36 +01:00
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2018-12-30 21:42:56 +01:00
2021-10-23 12:22:27 +02:00
namespace Friendica\Core\Logger\Factory;
2018-12-30 21:42:56 +01:00
2021-10-26 21:44:29 +02:00
use Friendica\Core\Config\Capability\IManageConfigValues;
2023-07-23 03:21:41 +02:00
use Friendica\Core\Hooks\Capability\ICanCreateInstances;
use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Logger\Type\ProfilerLogger as ProfilerLoggerClass;
use Friendica\Util\Profiler;
2018-12-30 21:42:56 +01:00
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
2018-12-30 21:42:56 +01:00
/**
* The logger factory for the core logging instances
2018-12-30 21:42:56 +01:00
*/
2021-10-23 12:22:27 +02:00
class Logger
2018-12-30 21:42:56 +01:00
{
/** @var string The channel */
protected $channel;
2019-09-17 16:47:00 +02:00
public function __construct(string $channel = LogChannel::DEFAULT)
{
$this->channel = $channel;
}
public function create(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler): LoggerInterface
2018-12-30 21:42:56 +01:00
{
if (empty($config->get('system', 'debugging') ?? false)) {
return new NullLogger();
2019-03-03 20:32:27 +01:00
}
$name = $config->get('system', 'logger_config') ?? '';
2019-03-03 20:32:27 +01:00
try {
/** @var LoggerInterface $logger */
$logger = $instanceCreator->create(LoggerInterface::class, $name, [$this->channel]);
if ($config->get('system', 'profiling') ?? false) {
return new ProfilerLoggerClass($logger, $profiler);
} else {
return $logger;
}
} catch (Throwable $e) {
// No logger ...
return new NullLogger();
2019-02-03 22:22:04 +01:00
}
2018-12-30 21:42:56 +01:00
}
}