scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 use Monolog\Formatter\JsonFormatter;
15 use Monolog\Logger;
16  
17 /**
18 * CouchDB handler
19 *
20 * @author Markus Bachmann <markus.bachmann@bachi.biz>
21 */
22 class CouchDBHandler extends AbstractProcessingHandler
23 {
24 private $options;
25  
26 public function __construct(array $options = array(), $level = Logger::DEBUG, $bubble = true)
27 {
28 $this->options = array_merge(array(
29 'host' => 'localhost',
30 'port' => 5984,
31 'dbname' => 'logger',
32 'username' => null,
33 'password' => null,
34 ), $options);
35  
36 parent::__construct($level, $bubble);
37 }
38  
39 /**
40 * {@inheritDoc}
41 */
42 protected function write(array $record)
43 {
44 $basicAuth = null;
45 if ($this->options['username']) {
46 $basicAuth = sprintf('%s:%s@', $this->options['username'], $this->options['password']);
47 }
48  
49 $url = 'http://'.$basicAuth.$this->options['host'].':'.$this->options['port'].'/'.$this->options['dbname'];
50 $context = stream_context_create(array(
51 'http' => array(
52 'method' => 'POST',
53 'content' => $record['formatted'],
54 'ignore_errors' => true,
55 'max_redirects' => 0,
56 'header' => 'Content-type: application/json',
57 ),
58 ));
59  
60 if (false === @file_get_contents($url, null, $context)) {
61 throw new \RuntimeException(sprintf('Could not connect to %s', $url));
62 }
63 }
64  
65 /**
66 * {@inheritDoc}
67 */
68 protected function getDefaultFormatter()
69 {
70 return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
71 }
72 }