Writers

The Slim application’s log object has a log writer. The log writer is responsible for sending a logged message tothe appropriate output (e.g. STDERR, a log file, a remote web service, Twitter, or a database). Out of the box,the Slim application’s log object has a log writer of class \Slim\LogFileWriter; this log writer directs outputto the resource handle referenced by the application environment’s slim.errors key (by default, this is“php://stderr”). You may also define and use a custom log writer.

How to use a custom log writer

A custom log writer must implement the following public interface:

  1. <?php
  2. public function write(mixed $message);

You must tell the Slim application’s log object to use your custom log writer. You can do so in your application’ssettings during instantiation like this:

  1. <?php
  2. $app = new \Slim\Slim(array(
  3. 'log.writer' => new MyLogWriter()
  4. ));

You may also set a custom log writer with middleware like this:

  1. <?php
  2. class CustomLogWriterMiddleware extends \Slim\Middleware
  3. {
  4. public function call()
  5. {
  6. //Set the new log writer
  7. $this->app->log->setWriter(new \MyLogWriter());
  8. //Call next middleware
  9. $this->next->call();
  10. }
  11. }

You can set the log writer similarly in an application hook or route callback like this:

  1. <?php
  2. $app->hook('slim.before', function () use ($app) {
  3. $app->log->setWriter(new \MyLogWriter());
  4. });

If you only need to redirect error output to a different resource handle, use the Slim application’s default log writer;it writes log messages to a resource handle. All you need to do is set the slim.errors environment variable to avalid resource handle.