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

68 lines
2.0 KiB
PHP
Raw Normal View History

2018-12-30 20:42:56 +00:00
<?php
2020-02-09 14:45:36 +00:00
/**
2023-01-01 14:36:24 +00:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2020-02-09 14:45:36 +00: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 20:42:56 +00:00
2021-10-23 10:22:27 +00:00
namespace Friendica\Core\Logger\Factory;
2018-12-30 20:42:56 +00:00
2021-10-26 19:44:29 +00:00
use Friendica\Core\Config\Capability\IManageConfigValues;
2023-07-23 01:21:41 +00: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 20:42:56 +00:00
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
2018-12-30 20:42:56 +00:00
/**
* The logger factory for the core logging instances
2018-12-30 20:42:56 +00:00
*/
2021-10-23 10:22:27 +00:00
class Logger
2018-12-30 20:42:56 +00:00
{
/** @var string The channel */
protected $channel;
2019-09-17 14:47:00 +00:00
public function __construct(string $channel = LogChannel::DEFAULT)
{
$this->channel = $channel;
}
public function create(ICanCreateInstances $instanceCreator, IManageConfigValues $config, Profiler $profiler): LoggerInterface
2018-12-30 20:42:56 +00:00
{
if (empty($config->get('system', 'debugging') ?? false)) {
return new NullLogger();
2019-03-03 19:32:27 +00:00
}
$name = $config->get('system', 'logger_config') ?? '';
2019-03-03 19:32:27 +00: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 21:22:04 +00:00
}
2018-12-30 20:42:56 +00:00
}
}