# Writers

A Writer is an object that inherits from Zend_Log_Writer_Abstract. 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`).

    $writer = new Zend_Log_Writer_Stream('php://output');
    $logger = new Zend_Log($writer);

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

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

    $writer = new Zend_Log_Writer_Stream('/path/to/logfile');
    $logger = new Zend_Log($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:

    $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($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. The constructor of Zend_Log_Writer_Db receives a Zend_Db_Adapter instance, a table name, and a mapping of database columns to event data items:

    $params = array ('host'     => '127.0.0.1',
                     'username' => 'malory',
                     'password' => '******',
                     'dbname'   => 'camelot');
    $db = Zend_Db::factory('PDO_MYSQL', $params);

    $columnMapping = array('lvl' => 'priority', 'msg' => 'message');
    $writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping);

    $logger = new Zend_Log($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 named 'lvl' receives the priority number and the column named 'msg' receives the log message.

## 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:

    $writer = new Zend_Log_Writer_Null;
    $logger = new Zend_Log($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.

    $mock = new Zend_Log_Writer_Mock;
    $logger = new Zend_Log($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:

    $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->addWriter($writer1);
    $logger->addWriter($writer2);

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