c727b3a92629647d63f99cd82eff344b66f386cd.svn-base 19 KB
<!DOCTYPE html>  <html> <head>   <title>couchdb.js</title>   <meta http-equiv="content-type" content="text/html; charset=UTF-8">   <link rel="stylesheet" media="all" href="../../docco.css" /> </head> <body>   <div id="container">     <div id="background"></div>            <div id="jump_to">         Jump To &hellip;         <div id="jump_wrapper">           <div id="jump_page">                                           <a class="source" href="../..//winston/common.html">                 winston/common.html               </a>                                           <a class="source" href="../..//winston/config/cli-config.html">                 winston/config/cli-config.html               </a>                                           <a class="source" href="../..//winston/config/npm-config.html">                 winston/config/npm-config.html               </a>                                           <a class="source" href="../..//winston/config/syslog-config.html">                 winston/config/syslog-config.html               </a>                                           <a class="source" href="../..//winston/config.html">                 winston/config.html               </a>                                           <a class="source" href="../..//winston/exception.html">                 winston/exception.html               </a>                                           <a class="source" href="../..//winston/logger.html">                 winston/logger.html               </a>                                           <a class="source" href="../..//winston/transports/console.html">                 winston/transports/console.html               </a>                                           <a class="source" href="../..//winston/transports/couchdb.html">                 winston/transports/couchdb.html               </a>                                           <a class="source" href="../..//winston/transports/file.html">                 winston/transports/file.html               </a>                                           <a class="source" href="../..//winston/transports/loggly.html">                 winston/transports/loggly.html               </a>                                           <a class="source" href="../..//winston/transports/transport.html">                 winston/transports/transport.html               </a>                                           <a class="source" href="../..//winston/transports/webhook.html">                 winston/transports/webhook.html               </a>                                           <a class="source" href="../..//winston/transports.html">                 winston/transports.html               </a>                                           <a class="source" href="../..//winston.html">                 winston.html               </a>                        </div>         </div>       </div>          <table cellpadding="0" cellspacing="0">       <thead>         <tr>           <th class="docs">             <h1>               couchdb.js             </h1>           </th>           <th class="code">           </th>         </tr>       </thead>       <tbody>                               <tr id="section-1">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-1">&#182;</a>               </div>                            </td>             <td class="code">               <div class="highlight"><pre><span class="cm">/*</span>
<span class="cm"> * Couchdb.js: Transport for logging to Couchdb</span>
<span class="cm"> *</span>
<span class="cm"> * (C) 2011 Max Ogden</span>
<span class="cm"> * MIT LICENSE</span>
<span class="cm"> *</span>
<span class="cm"> */</span>

<span class="kd">var</span> <span class="nx">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;events&#39;</span><span class="p">),</span>
    <span class="nx">http</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;http&#39;</span><span class="p">),</span>
    <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;util&#39;</span><span class="p">),</span>
    <span class="nx">common</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../common&#39;</span><span class="p">),</span>
    <span class="nx">Transport</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./transport&#39;</span><span class="p">).</span><span class="nx">Transport</span><span class="p">;</span> </pre></div>             </td>           </tr>                               <tr id="section-2">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-2">&#182;</a>               </div>               <h3>function Couchdb (options)</h3>

<h4>@options {Object} Options for this instance.</h4>

<p>Constructor function for the Console transport object responsible
for making arbitrary HTTP requests whenever log messages and metadata
are received.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Couchdb</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Couchdb</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">Transport</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">options</span><span class="p">);</span>

  <span class="k">this</span><span class="p">.</span><span class="nx">name</span>   <span class="o">=</span> <span class="s1">&#39;Couchdb&#39;</span><span class="p">;</span> 
  <span class="k">this</span><span class="p">.</span><span class="nx">db</span>     <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">db</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">user</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">user</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">pass</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">pass</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">host</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span>   <span class="o">||</span> <span class="s1">&#39;localhost&#39;</span><span class="p">;</span>
  <span class="k">this</span><span class="p">.</span><span class="nx">port</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span>   <span class="o">||</span> <span class="mi">5984</span><span class="p">;</span>

  <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">auth</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-3">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-3">&#182;</a>               </div>               <p>TODO: add http basic auth options for outgoing HTTP requests</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="p">}</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">ssl</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-4">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-4">&#182;</a>               </div>               <p>TODO: add ssl support for outgoing HTTP requests</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="p">}</span>  
<span class="p">};</span></pre></div>             </td>           </tr>                               <tr id="section-5">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-5">&#182;</a>               </div>               <p>Inherit from <code>winston.Transport</code>.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">util</span><span class="p">.</span><span class="nx">inherits</span><span class="p">(</span><span class="nx">Couchdb</span><span class="p">,</span> <span class="nx">Transport</span><span class="p">);</span></pre></div>             </td>           </tr>                               <tr id="section-6">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-6">&#182;</a>               </div>               <p>Expose the name of this Transport on the prototype</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Couchdb</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s1">&#39;Couchdb&#39;</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-7">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-7">&#182;</a>               </div>               <h3>function log (level, msg, [meta], callback)</h3>

<h4>@level {string} Level at which to log the message.</h4>

<h4>@msg {string} Message to log</h4>

<h4>@meta {Object} <strong>Optional</strong> Additional metadata to attach</h4>

<h4>@callback {function} Continuation to respond to when complete.</h4>

<p>Core logging method exposed to Winston. Metadata is optional.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">Couchdb</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">log</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">level</span><span class="p">,</span> <span class="nx">msg</span><span class="p">,</span> <span class="nx">meta</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">silent</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
  <span class="p">}</span>
  
  <span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
      <span class="nx">message</span> <span class="o">=</span> <span class="nx">common</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">meta</span><span class="p">),</span>
      <span class="nx">options</span><span class="p">,</span>
      <span class="nx">req</span><span class="p">;</span>
      
  <span class="nx">message</span><span class="p">.</span><span class="nx">level</span> <span class="o">=</span> <span class="nx">level</span><span class="p">;</span>
  <span class="nx">message</span><span class="p">.</span><span class="nx">message</span> <span class="o">=</span> <span class="nx">msg</span><span class="p">;</span></pre></div>             </td>           </tr>                               <tr id="section-8">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-8">&#182;</a>               </div>               <p>Prepare options for outgoing HTTP request</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">options</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nx">host</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">host</span><span class="p">,</span>
    <span class="nx">port</span><span class="o">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span>
    <span class="nx">path</span><span class="o">:</span> <span class="s2">&quot;/&quot;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">db</span><span class="p">,</span>
    <span class="nx">method</span><span class="o">:</span> <span class="s2">&quot;POST&quot;</span><span class="p">,</span>
    <span class="nx">headers</span><span class="o">:</span> <span class="p">{</span><span class="s2">&quot;content-type&quot;</span><span class="o">:</span> <span class="s2">&quot;application/json&quot;</span><span class="p">}</span>
  <span class="p">};</span>
  
  <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">user</span> <span class="o">&amp;&amp;</span> <span class="nx">options</span><span class="p">.</span><span class="nx">pass</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">options</span><span class="p">.</span><span class="nx">headers</span><span class="p">[</span><span class="s2">&quot;Authorization&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Basic &quot;</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">Buffer</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">user</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</span> <span class="o">+</span> <span class="nx">options</span><span class="p">.</span><span class="nx">pass</span><span class="p">).</span><span class="nx">toString</span><span class="p">(</span><span class="s1">&#39;base64&#39;</span><span class="p">);</span>
  <span class="p">}</span>
  </pre></div>             </td>           </tr>                               <tr id="section-9">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-9">&#182;</a>               </div>               <p>Perform HTTP logging request</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">req</span> <span class="o">=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">request</span><span class="p">(</span><span class="nx">options</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">res</span><span class="p">)</span> <span class="p">{</span> </pre></div>             </td>           </tr>                               <tr id="section-10">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-10">&#182;</a>               </div>               <p>No callback on request, fire and forget about the response</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;logged&#39;</span><span class="p">,</span> <span class="nx">res</span><span class="p">);</span>
  <span class="p">});</span> 

  <span class="nx">req</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span></pre></div>             </td>           </tr>                               <tr id="section-11">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-11">&#182;</a>               </div>               <p>Propagate the <code>error</code> back up to the <code>Logger</code> that this
instance belongs to.</p>             </td>             <td class="code">               <div class="highlight"><pre>    <span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">&#39;error&#39;</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
  <span class="p">});</span>
  </pre></div>             </td>           </tr>                               <tr id="section-12">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-12">&#182;</a>               </div>               <p>Write logging event to the outgoing request body</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">req</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">({</span> 
    <span class="nx">method</span><span class="o">:</span> <span class="s1">&#39;log&#39;</span><span class="p">,</span> 
    <span class="nx">params</span><span class="o">:</span> <span class="p">{</span> 
      <span class="nx">timestamp</span><span class="o">:</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(),</span> <span class="c1">// RFC3339/ISO8601 format instead of common.timestamp()</span>
      <span class="nx">msg</span><span class="o">:</span> <span class="nx">msg</span><span class="p">,</span> 
      <span class="nx">level</span><span class="o">:</span> <span class="nx">level</span><span class="p">,</span> 
      <span class="nx">meta</span><span class="o">:</span> <span class="nx">meta</span> 
    <span class="p">}</span> 
  <span class="p">}));</span>
  
  <span class="nx">req</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span>
  </pre></div>             </td>           </tr>                               <tr id="section-13">             <td class="docs">               <div class="pilwrap">                 <a class="pilcrow" href="#section-13">&#182;</a>               </div>               <p>Always return true, regardless of any errors</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
<span class="p">};</span>

</pre></div>             </td>           </tr>                </tbody>     </table>   </div> </body> </html>