Writers

A Writer is an object that inherits from Zend\Log\Writer\AbstractWriter. A Writer’s responsibility is to record log data to a storage backend.

Writing to Streams

Zend\Log\Writer\Stream sends log data to a PHP stream.

To write log data to the PHP output buffer, use the URL php://output. Alternatively, you can send log data directly to a stream like STDERR (php://stderr).

1
2
3
4
5
$writer = new Zend\Log\Writer\Stream('php://output');
$logger = new Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

To write data to a file, use one of the Filesystem URLs:

1
2
3
4
5
$writer = new Zend\Log\Writer\Stream('/path/to/logfile');
$logger = new Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

By default, the stream opens in the append mode (“a”). To open it with a different mode, the Zend\Log\Writer\Stream constructor accepts an optional second parameter for the mode.

The constructor of Zend\Log\Writer\Stream also accepts an existing stream resource:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$stream = @fopen('/path/to/logfile', 'a', false);
if (! $stream) {
    throw new Exception('Failed to open stream');
}

$writer = new Zend\Log\Writer\Stream($stream);
$logger = new Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

You cannot specify the mode for existing stream resources. Doing so causes a Zend\Log\Exception to be thrown.

Writing to Databases

Zend\Log\Writer\Db writes log information to a database table using Zend\Db\Adapter\Adapter. The constructor of Zend\Log\Writer\Db receives a Zend\Db\Adapter\Adapter instance, a table name, an optional mapping of event data to database columns, and an optional string contains the character separator for the log array:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$dbconfig = array(
    // Sqlite Configuration
    'driver' => 'Pdo',
    'dsn' => 'sqlite:' . __DIR__ . '/tmp/sqlite.db',
);
$db = new Zend\Db\Adapter\Adapter($dbconfig);

$writer = new Zend\Log\Writer\Db($db, 'log_table_name');
$logger = new Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

The example above writes a single row of log data to the database table named ‘log_table_name’ table. The database column will be created according to the event array generated by the Zend\Log\Logger instance.

If we specify the mapping of the events with the database columns the log will store in the database only the selected fields.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$dbconfig = array(
    // Sqlite Configuration
    'driver' => 'Pdo',
    'dsn' => 'sqlite:' . __DIR__ . '/tmp/sqlite.db',
);
$db = new Zend\Db\Adapter\Adapter($dbconfig);

$mapping = array(
    'timestamp' => 'date',
    'priority'  => 'type',
    'message'   => 'event'
);
$writer = new Zend\Log\Writer\Db($db, 'log_table_name', $mapping);
$logger = new Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

The previous example will store only the log information timestamp, priority and message in the database fields date, type and event.

The Zend\Log\Writer\Db has a fourth optional parameter in the constructor. This parameter is the character separator for the log events managed by an array. For instance, if we have a log that contains an array extra fields, this will be translated in ‘extra-field’, where ‘-‘ is the character separator (default) and field is the subname of the specific extra field.

Writing to FirePHP

Zend\Log\Writer\FirePHP writes log information to the FirePHP Firefox extension. In order to use this you have to install the FirePHPCore Server Library and the FirePHP browser extension.

To install the FirePHPCore Library you can use composer. Add the repository and the required line to your topmost composer.json:

{
   [ .. ]


   "repositories": [{
      "type" : "pear",
      "url" : "pear.firephp.org",
      "vendor-alias" : "firephp"
   }],
   "minimum-stability": "dev",
   "require" : {
      [ ... ]
      "firephp/FirePHPCore" : "*"
   }
}

Stubbing Out the Writer

The Zend\Log\Writer\Null is a stub that does not write log data to anything. It is useful for disabling logging or stubbing out logging during tests:

1
2
3
4
5
6
$writer = new Zend\Log\Writer\Null;
$logger = new Zend\Log\Logger();
$logger->addWriter($writer);

// goes nowhere
$logger->info('Informational message');

Testing with the Mock

The Zend\Log\Writer\Mock is a very simple writer that records the raw data it receives in an array exposed as a public property.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$mock = new Zend\Log\Writer\Mock;
$logger = new Zend\Log\Logger();
$logger->addWriter($mock);

$logger->info('Informational message');

var_dump($mock->events[0]);

// Array
// (
//    [timestamp] => 2007-04-06T07:16:37-07:00
//    [message] => Informational message
//    [priority] => 6
//    [priorityName] => INFO
// )

To clear the events logged by the mock, simply set $mock->events = array().

Compositing Writers

There is no composite Writer object. However, a Log instance can write to any number of Writers. To do this, use the addWriter() method:

1
2
3
4
5
6
7
8
9
$writer1 = new Zend\Log\Writer\Stream('/path/to/first/logfile');
$writer2 = new Zend\Log\Writer\Stream('/path/to/second/logfile');

$logger = new Zend\Log\Logger();
$logger->addWriter($writer1);
$logger->addWriter($writer2);

// goes to both writers
$logger->info('Informational message');

You can also specify the priority number for each writer to change the order of writing. The priority number is an integer number (greater or equal to 1) passed as second parameter in the addWriter() method.

Table Of Contents

Previous topic

Overview of Zend\Log

Next topic

Filters

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Writers on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.