public function call() { // Get reference to application $app = $this->app; // The Slim error handler doesn't work for middleware, so we have to wrap everything in a try/catch. // See https://github.com/codeguy/Slim/issues/841 try { $app->container->singleton('log', function ($c) { $env = $c['environment']; $logger = new UsfLogRegistry(); if (is_array($env['log.config'])) { $logger_list = []; // Setup the loggers and add them to the registry foreach ($env['log.config'] as $log_writer) { // Does this logger already exist? if (in_array($log_writer['name'], $logger_list)) { // Add a handler to an existing logger $logger->{$log_writer}['name']->addLogHandler($log_writer['type'], $log_writer['config']); } else { // Create a new logger $logger->addLogger($log_writer['name'], $log_writer['type'], $log_writer['config']); $logger_list[] = $log_writer['name']; } //Add Log processors if (array_key_exists('processor', $log_writer)) { $logger->{$log_writer}['name']->addLogProcessor($log_writer['processor']); } //Set default handler if (array_key_exists('default', $log_writer) && $log_writer['default'] == true) { $logger->setDefaultHandler($log_writer['name']); $env['slim.log.default'] = $log_writer['name']; } } $env['slim.log'] = $logger; //If the user didn't specify a default handler, use the first one in the config array if (empty($env['slim.log.default'])) { $logger->setDefaultHandler($env['log.config'][0]['name']); $env['slim.log.default'] = $env['log.config'][0]['name']; } return $logger; } else { throw \Exception('Environmental config "log.config" required!'); } }); // Run inner middleware and application $this->next->call(); } catch (Exception $e) { $this->handleException($app, $e); } }
<?php require_once '../vendor/autoload.php'; use USF\IdM\UsfLogRegistry; use USF\IdM\UsfConfig; $config = new UsfConfig('config'); // Create an instance of UsfLogRegistry $logger = UsfLogRegistry::getInstance(); // Calling addLogger with no options creates a loghandler named 'log' that writes messages to /var/log/usf-logger.log $logger->addLogger(); // Add a log processor that logs the method, class, file and line number of the call $logger->log - addLogProcessor('introspection'); // log an error message with extra array data $logger->log->warn('This is a test message.', ['foo' => 'bar']); // Add a logger that emails audit reports $logger->addLogger('audit', 'mail', $config->mailConfig); //Send an email $logger->audit->warn('Audit Test', ['foo' => 'bar', 'black' => 'white', 'yes' => 'no']); // Add an additional handler to the 'audit' logger that will send text messages through Twilio $logger->audit->addLogHandler('sms', $config->smsConfig); //This message will be emailed AND sent as a text $logger->audit->critical('Critical Problem!!', ['var1' => 'true', 'var2' => 'false']);