Custom Handlers and Advanced Functionality

concrete5's logging system also contains support for handlers. Handlers are the ways that logged messages make their way to people interested in their contents. By default, concrete5 adds a database handler to every Logger, and uses this handler for debug messages and up. That ensures that every logged message makes its way into the Dashboard logs report. However, if you want more control over certain types of log messages, you can use custom handlers.

By default, all messages are logged to concrete5's database adapter. However, let's say we want to take messages that have the severity of alert or higher, and pipe those to a log file (which we consume in some other way.) We just make sure this code exists in application/bootstrap/app.php, or in the on_start() method of a package that's installed:

// This gets the logger singleton that's used throughout concrete5.
$logger = Core::make('log');    
$logger->pushHandler(
    new \Monolog\Handler\RotatingFileHandler(
    '/path/to/file.log', 
    0, 
    \Monolog\Logger::ALERT, 
    false)
);

Then, when a message is logged – whether it comes from our custom code or from concrete5's core code – if it's an alert or more severe, it will skip the database log and go directly into /path/to/file-date.log. If we want to have this message logged to both places, simply omit the last false argument in the RotatingFileHandler constructor. This is the $bubble parameter.

Since concrete5's Logger class subclasses Monolog's Logger class, it supports all the handlers that Monolog supports. That's a long list. You can connect concrete5's logging system to systems like MongoDB, Slack, Sendgrid, Hipchat, and others. For information on handlers, and other Monolog functionality like log processors and line formatters, check out the Monolog documentation.

Loading Conversation