Latest release: v0.1.0

Monolog

Monolog integration into Nette/DI

See also Monolog documentation, this is only an integration.

Content

Installation

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

Configuration

monolog:
    tracy:
        hook: true # use monolog inside tracy (required to log exceptions with monolog)
    channel:
        default: # default channel is required
            handlers:
                - Monolog\Handlers\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
            processors:
                -  Monolog\Processor\MemoryPeakUsageProcessor()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Logging

Log message with injected logger (only default is autowired)

use Monolog\Logger;

class ExampleService
{

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

    public function injectLogger(Logger $logger): void
    {
        $this->logger = $logger;
        // or withName if you want change channel name
        $this->logger = $logger->withName('example');
    }

    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
20
21

LoggerManager

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

monolog:
    manager:
        enabled: false # disabled by default
        lazy: true # lazy by default
1
2
3
4
use Contributte\Monolog\ILoggerManager;

class ExampleService
{

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

    public function injectLoggerManager(ILoggerManager $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

Bridges

Bridges to other packages.

Doctrine

Log all queries from doctrine/dbal

# example for nettrine/dbal integration
dbal:
    configuration:
        sqlLogger: Contributte\Monolog\Bridges\DoctrineDbal\SqlLogger(@Monolog\Logger)
1
2
3
4