### Install Monolog via Composer Source: https://github.com/seldaek/monolog/blob/main/README.md Use this command to add the Monolog package to your project dependencies. ```bash composer require monolog/monolog ``` -------------------------------- ### Initialize and Use Monolog Source: https://github.com/seldaek/monolog/blob/main/README.md Create a logger instance, attach a stream handler, and log messages at different severity levels. ```php pushHandler(new StreamHandler('path/to/your.log', Level::Warning)); // add records to the log $log->warning('Foo'); $log->error('Bar'); ``` -------------------------------- ### Configure a Logger with Handlers Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Initializes a logger instance and attaches StreamHandler and FirePHPHandler to the stack. ```php pushHandler(new StreamHandler(__DIR__.'/my_app.log', Level::Debug)); $logger->pushHandler(new FirePHPHandler()); // You can now use your logger $logger->info('My logger is now ready'); ``` -------------------------------- ### Initialize and use SocketHandler in PHP Source: https://github.com/seldaek/monolog/blob/main/doc/sockets.md Configures a logger with a persistent Unix socket handler. Ensure the socket path is accessible by the process user. ```php setPersistent(true); // Now add the handler $logger->pushHandler($handler, Level::Debug); // You can now use your logger $logger->info('My logger is now ready'); ``` -------------------------------- ### Configuring a LineFormatter Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Create a custom log format by instantiating a LineFormatter with a template string and date format, then attaching it to a handler. ```php %level_name% > %message% %context% %extra%\n"; // finally, create a formatter $formatter = new LineFormatter($output, $dateFormat); // Create a handler $stream = new StreamHandler(__DIR__.'/my_app.log', Level::Debug); $stream->setFormatter($formatter); // bind it to a logger object $securityLogger = new Logger('security'); $securityLogger->pushHandler($stream); ``` -------------------------------- ### Configure log channels Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Create separate logger instances with unique names to identify the source of log records. ```php pushHandler($stream); $logger->pushHandler($firephp); // Create a logger for the security-related stuff with a different channel $securityLogger = new Logger('security'); $securityLogger->pushHandler($stream); $securityLogger->pushHandler($firephp); // Or clone the first one to only change the channel $securityLogger = $logger->withName('security'); ``` -------------------------------- ### Create a custom PDOHandler Source: https://github.com/seldaek/monolog/blob/main/doc/04-extending.md Implement a custom handler by extending AbstractProcessingHandler to log records into a database. The write method handles the persistence logic using the formatted record data. ```php pdo = $pdo; parent::__construct($level, $bubble); } protected function write(LogRecord $record): void { if (!$this->initialized) { $this->initialize(); } $this->statement->execute(array( 'channel' => $record->channel, 'level' => $record->level, 'message' => $record->formatted, 'time' => $record->datetime->format('U'), )); } private function initialize() { $this->pdo->exec( 'CREATE TABLE IF NOT EXISTS monolog ' .'(channel VARCHAR(255), level INTEGER, message LONGTEXT, time INTEGER UNSIGNED)' ); $this->statement = $this->pdo->prepare( 'INSERT INTO monolog (channel, level, message, time) VALUES (:channel, :level, :message, :time)' ); $this->initialized = true; } } ``` -------------------------------- ### Register the custom handler Source: https://github.com/seldaek/monolog/blob/main/doc/04-extending.md Add the custom handler to a logger instance using pushHandler. ```php pushHandler(new PDOHandler(new PDO('sqlite:logs.sqlite'))); // You can now use your logger $logger->info('My logger is now ready'); ``` -------------------------------- ### Add context to log records Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Pass an array of data as the second argument to logging methods to include contextual information. ```php info('Adding a new user', ['username' => 'Seldaek']); ``` -------------------------------- ### Resetting the Logger State Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Use this method to flush buffers and reset internal state, effectively clearing memory between background tasks. ```php $logger->reset(); ``` -------------------------------- ### Using Level enum Source: https://github.com/seldaek/monolog/blob/main/UPGRADE.md Replace Logger constants with the Level enum cases or their integer values. ```php Level::Warning Level::Warning->value ``` -------------------------------- ### Converting LogRecord to array Source: https://github.com/seldaek/monolog/blob/main/UPGRADE.md Use toArray() to retrieve a Monolog 1/2 style array from a LogRecord object. ```php $record->toArray() ``` -------------------------------- ### Accessing formatted log records Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Handlers typically store the final string representation of a log entry in the formatted property of the record. ```php $record->formatted ``` -------------------------------- ### Add extra data with processors Source: https://github.com/seldaek/monolog/blob/main/doc/01-usage.md Use a callable processor to modify the record's extra data globally or per handler. ```php pushProcessor(function ($record) { $record->extra['dummy'] = 'Hello world!'; return $record; }); ``` -------------------------------- ### Accessing LogRecord properties Source: https://github.com/seldaek/monolog/blob/main/UPGRADE.md Use object property access instead of array access for log records in Monolog 3.0.0. ```php $record->context ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.