use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('app'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG)); // add records to the log $log->info('User logged in'); $log->error('Oops, something went wrong');
// add records to the log with different levels $log->debug('This is a debug message'); $log->info('This is an info message'); $log->notice('This is a notice message'); $log->warning('This is a warning message'); $log->error('This is an error message'); $log->critical('This is a critical message'); $log->alert('This is an alert message'); $log->emergency('This is an emergency message');
use Monolog\Handler\SyslogHandler; // create a log channel with a syslog handler $log = new Logger('app'); $log->pushHandler(new SyslogHandler('myapp')); // add records to the log $log->error('Oops, something went wrong');In this example, we create a logger with the name 'app' and add a log handler that writes log entries to syslog with the identifier 'myapp'. We then add a log entry using the logger. Overall, Monolog is a powerful and flexible package library for PHP app logging.