PHP code example of harbor / monolog-manager

1. Go to this page and download the library: Download harbor/monolog-manager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

harbor / monolog-manager example snippets


use Monolog\Formatter\HtmlFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NativeMailerHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\PsrLogMessageProcessor;
use Psr\Log\LogLevel;

$manager = new Harbor\MonologManager\Manager();

// Setup the default logger
$manager->add('app', [
    'default' => true,
    'handlers' => [
        'name' => StreamHandler::class,
        'params' => [
            'stream' => 'path/to/your.log',
            'level' => LogLevel::WARNING,
        ],
        'formatter' => [
            'name' => LineFormatter::class,
            'params' => [
                'allowInlineLineBreaks' => false,
                'ignoreEmptyContextAndExtra' => true,
            ],
        ],
    ],
    'processors' => [
        [
            'name' => PsrLogMessageProcessor::class,
        ]
    ],
]);

// Add an additional logging channel
$manager->add('alert', [
    'handlers' => [
        [
            'name' => NativeMailerHandler::class,
            'params' => [
                'to' => '[email protected]',
                'subject' => 'Application Alert',
                'from' => '[email protected]',
                'level' => Logger::ALERT,
            ],
            'formatter' => [
                'name' => HtmlFormatter::class,
            ],
        ],
    ],
    'processors' => [
        [
            'name' => PsrLogMessageProcessor::class,
        ]
    ],
]);

// These logs be sent to the `app` channel we configured above.
$manager->emergency('test emergency');
$manager->alert('test alert');
$manager->critical('test critical');
$manager->error('test error');
$manager->warning('test warning');
$manager->notice('test notice');
$manager->info('test info');
$manager->debug('test debug');
$manager->log(LogLevel::INFO, 'test log');

$manager->getName(); // returns "app"

/** @var \Mongolog\Logger */
$logger = $manager->channel('alert');
$logger->alert('Something important happened!');

$manager->add(Manager::EMERGENCY_CHANNEL, [
    'handlers' => [
        [
            'name' => \Monolog\Handler\StreamHandler::class,
            'params' => [
                'stream' => 'php://stderr',
                'level' => \Psr\Log\LogLevel::DEBUG,
            ],
        ],
    ],
]);

$manager->useEmergencyChannel(false);

use Harbor\MonologManager\Factory;
use Harbor\MonologManager\Manager;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface */
$container = getContainer();

// Create a new Factory, providing the container
$factory = new Factory($container);

// Provide your factory to the Manager
$manager = new Manager($factory);

$manager->add('useless-logger', [
    'handlers' => [
        HandlerFactory::class,
        NoopHandler::class,
        new NoopHandler(),
        static fn () => new NoopHandler(),
        [
            'name' => ErrorLogHandler::class,
            'formatter' => LineFormatter::class,
        ],
        [
            'name' => ErrorLogHandler::class,
            'formatter' => new LineFormatter(),
        ],
    ],
    'processors' => [
        PsrLogMessageProcessor::class,
        new MemoryUsageProcessor(),
    ],
]);