# Contributte Monolog

Monolog integration into Nette/DI

See also Monolog documentation, this is only an integration.

# Content

# Setup

Install package

composer require contributte/monolog
1

Register extension

extensions:
    monolog: Contributte\Monolog\DI\MonologExtension
1
2

# Configuration

monolog:
    channel:
        default: # default channel is required
            handlers:
                - Monolog\Handler\RotatingFileHandler(%appDir%/../log/syslog.log, 30, Monolog\Logger::WARNING)
                # you can use same configuration as in services section (with setup, type, arguments, etc.)
                -
                    type: Monolog\Handler\RotatingFileHandler
                    arguments:
                        - %appDir%/../log/syslog.log
                        - 30
                        - Monolog\Logger::WARNING
                - @serviceName # or reference an existing service
            processors:
                -  Monolog\Processor\MemoryPeakUsageProcessor()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Tracy

monolog:
    hook:
      fromTracy: true # enabled by default, log through Tracy into Monolog
      toTracy: true # enabled by default, log through Monolog into Tracy
1
2
3
4

You may also want configure remote storage for Tracy bluescreens. In this case use mangoweb-backend/monolog-tracy-handler

# Logging

Log message with injected logger (only default is autowired)

use Psr\Log\LoggerInterface;

class ExampleService
{

    /** @var LoggerInterface **/
    private $logger;

    public function injectLogger(LoggerInterface $logger): void
    {
        $this->logger = $logger;
    }

    public function doSomething(): void
    {
        $this->logger->info('Log that application did something');
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# LoggerManager

You could also use logger manager in case you need to use multiple logger at once.

monolog:
    manager:
        enabled: false # disabled by default
1
2
3
use Contributte\Monolog\LoggerManager;

class ExampleService
{

    /** @var LoggerManager **/
    private $loggerManager;

    public function injectLoggerManager(LoggerManager $loggerManager): void
    {
        $this->loggerManager = $loggerManager;
    }

    public function doSomething(): void
    {
        $this->loggerManager->get('default')->info('Log that application did something');
        $this->loggerManager->get('specialLogger')->info('Log something very special');
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# LoggerHolder

Allow you get default logger statically in case that DIC is not available.

It add into message info about which class (or file) called LoggerHolder for easier debugging.

monolog:
    holder:
        enabled: false # disabled by default
1
2
3
use Contributte\Monolog\LoggerHolder;

class VerySpecialClassWithoutDependencyInjectionContainerAvailable
{

    public function doSomething(): void
    {
        LoggerHolder::getInstance()->getLogger()->info('Log that application did something');
    }

}
1
2
3
4
5
6
7
8
9
10
11