Logging with Monolog | Symfony

Monolog

Install Monolog on Symfony

Separate logs files

monolog:
handlers:
main:
type:
stream
path: '%kernel.logs_dir%/%kernel.environment%.log'
level: debug
channels: ['!event']
handler_a:
type:
stream
path: '%kernel.logs_dir%/%kernel.environment%.yobit.log'
channels: [channel_a]
handler_b:
type:
stream
path: '%kernel.logs_dir%/%kernel.environment%.gate.log'
channels: [channel_b]
<?php

namespace
LoggerBundle\Logger;

use Monolog\Logger;

class BaseLogger
{
/**
*
@var Logger
*/
protected $logger;

public function __construct(Logger $logger)
{
$this->logger = $logger;
}

/**
*
@param string $level
*
@param string $message
*
@param array $context
*/
public function log(string $level, string $message, array $context)
{
$this->logger->addRecord($level, $message, $context);
}


/**
*
@param string $message
*
@param array $context
*/
public function logError(string $message, array $context = [])
{
$this->log(Logger::ERROR, $message, $context);
}

/**
*
@param string $message
*
@param array $context
*/
public function logWarning(string $message, array $context = [])
{
$this->log(Logger::WARNING, $message, $context);
}

/**
*
@param string $message
*
@param array $context
*/
public function logNotice(string $message, array $context = [])
{
$this->log(Logger::NOTICE, $message, $context);
}

/**
*
@param string $message
*
@param array $context
*/
public function logInfo(string $message, array $context = [])
{
$this->log(Logger::INFO, $message, $context);
}
}
<?php

namespace
LoggerBundle\Logger;

class ChannelALogger extends BaseLogger
{
}
<?php

namespace
LoggerBundle\Logger;

class ChannelBLogger extends BaseLogger
{
}
<service id="logger.logger.channel_a_logger" class="LoggerBundle\Logger\ChannelALogger" public="true">
<argument type="service" id="logger"/>
<tag name="monolog.logger" channel="channel_a"/>
</service>

<service id="logger.logger.channel_b_logger" class="LoggerBundle\Logger\ChannelBLogger" public="true">
<argument type="service" id="logger"/>
<tag name="monolog.logger" channel="channel_b"/>
</service>

Other articles

Thank you for your attention

Senior web developer, architect, cryptocurrencies trader, https://lytvynov-anton.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store