Loading...
Loading...
Covers PSR-3 logging in Bitrix — Bitrix\Main\Diag\Logger, FileLogger, SysLogger, NullLogger, LogFormatter, loggers section in .settings.php, named kernel loggers (main.Default, main.HttpClient, main.GeoIpManager, main.EventLog.*), integration with Monolog and third-party PSR-3 loggers. Applied when configuring module logs, debugging integrations, gathering errors from specific kernel components and log rotation. Key terms — Logger, FileLogger, SysLogger, LogFormatter, PSR-3, Monolog, loggers config, log level.
npx skill4agent add bxmaximum/bitrix-framework-skills bitrix-logger\Psr\Log\LoggerInterface.settings.phpAddMessage2Log\Bitrix\Main\Diag\| Class | Purpose |
|---|---|
| Abstract base class; |
| Into a file, with auto-rotation when |
| Into system |
| Into |
| Default formatter: interpolates |
| From 25.300.0; one JSON line per entry, convenient for ELK/Loki |
\Psr\Log\LogLevel::*emergencyalertcriticalerrorwarningnoticeinfodebug<?php declare(strict_types=1);
namespace Vendor\Module\Application\Service;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
final class PostService
{
public function __construct(
private readonly LoggerInterface $logger = new NullLogger(),
) {}
public function publish(int $postId): void
{
try
{
// ...
$this->logger->info('Post {id} published', ['id' => $postId]);
}
catch (\Throwable $e)
{
$this->logger->error('Publish failed for post {id}: {exception}', [
'id' => $postId,
'exception' => $e,
]);
throw $e;
}
}
}/local/modules/vendor.module/.settings.php'services' => [
'value' => [
\Vendor\Module\Application\Service\PostService::class => [
'constructor' => static fn (): \Vendor\Module\Application\Service\PostService =>
new \Vendor\Module\Application\Service\PostService(
new \Bitrix\Main\Diag\FileLogger('/var/log/bitrix/post-service.log'),
),
],
],
'readonly' => true,
],{key}$context$logger->warning('User {userId} tried {action} on post {postId}', [
'userId' => $uid, 'action' => 'delete', 'postId' => $pid,
]);LogFormatter{date}{host}{delimiter}{exception}\Throwable{trace}Diag\Helper::getBackTrace(6, DEBUG_BACKTRACE_IGNORE_ARGS, 3)$logger->setFormatter(new \Bitrix\Main\Diag\LogFormatter(showArguments: true, argMaxChars: 120));.settings.phploggersmain.HttpClientmain.Defaultmain.GeoIpManagerreturn [
'services' => [
'value' => [
'formatter.withArgs' => [
'className' => \Bitrix\Main\Diag\LogFormatter::class,
'constructorParams' => [true],
],
],
'readonly' => true,
],
'loggers' => [
'value' => [
'main.Default' => [
'constructor' => static fn () => new \Bitrix\Main\Diag\FileLogger(
'/var/log/bitrix/app.log', 10 * 1024 * 1024,
),
'level' => \Psr\Log\LogLevel::INFO,
'formatter' => 'formatter.withArgs',
],
'main.HttpClient' => [
'constructor' => static function (
\Bitrix\Main\Web\Http\DebugInterface $debug,
\Psr\Http\Message\RequestInterface $request,
) {
$debug->setDebugLevel(\Bitrix\Main\Web\HttpDebug::ALL);
return new \Bitrix\Main\Diag\FileLogger(
'/var/log/bitrix/http-' . spl_object_hash($request) . '.log',
);
},
'level' => \Psr\Log\LogLevel::DEBUG,
],
'vendor.module.myLogger' => [
'constructor' => static fn () => new \Bitrix\Main\Diag\FileLogger(
'/var/log/bitrix/vendor.module.log',
),
'level' => \Psr\Log\LogLevel::DEBUG,
],
],
'readonly' => true,
],
];constructor.settings.php.settings_extra.phplevelformatterservices$logger = \Bitrix\Main\Diag\Logger::create('vendor.module.myLogger');
$logger = \Bitrix\Main\Diag\Logger::create('vendor.module.myLogger', [$this, $extraArg]);| ID | Used In | Factory Parameters |
|---|---|---|
| | |
| | |
| | — |
| | — |
| | |
main.Mailmain.EngineconstructorFileLoggerclassNamesettingsbitrix-http-clientfinal class Indexer implements \Psr\Log\LoggerAwareInterface
{
use \Psr\Log\LoggerAwareTrait;
public function run(): void
{
$this->ensureLogger()->info('Indexing started');
}
private function ensureLogger(): \Psr\Log\LoggerInterface
{
if ($this->logger === null)
{
$this->setLogger(\Bitrix\Main\Diag\Logger::create('vendor.module.indexer', [$this]));
}
return $this->logger;
}
}composer require monolog/monolog.settings.php'loggers' => [
'value' => [
'vendor.module.external' => [
'constructor' => static function () {
$log = new \Monolog\Logger('vendor.module');
$log->pushHandler(new \Monolog\Handler\StreamHandler('/var/log/bitrix/monolog.log'));
return $log;
},
'level' => \Psr\Log\LogLevel::DEBUG,
],
],
],.settings.phplevelHttpClientJsonLinesFormatterDOCUMENT_ROOT.htaccessexception_handling.log.settings.phpbitrix-settings