d21e98d9d366b2a036601fea408b5fe7229a739b.svn-base 26 KB
<!DOCTYPE html>  <html> <head>   <title>common.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>               common.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"> * common.js: Internal helper and utility functions for winston</span>
<span class="cm"> *</span>
<span class="cm"> * (C) 2010 Charlie Robbins</span>
<span class="cm"> * MIT LICENCE</span>
<span class="cm"> *</span>
<span class="cm"> */</span>

<span class="kd">var</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">crypto</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;crypto&#39;</span><span class="p">),</span>
    <span class="nx">loggly</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;loggly&#39;</span><span class="p">),</span>
    <span class="nx">config</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./config&#39;</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 setLevels (target, past, current)</h3>

<h4>@target {Object} Object on which to set levels.</h4>

<h4>@past {Object} Previous levels set on target.</h4>

<h4>@current {Object} Current levels to set on target.</h4>

<p>Create functions on the target objects for each level 
in current.levels. If past is defined, remove functions
for each of those levels.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">setLevels</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">target</span><span class="p">,</span> <span class="nx">past</span><span class="p">,</span> <span class="nx">current</span><span class="p">,</span> <span class="nx">isDefault</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">past</span><span class="p">)</span> <span class="p">{</span>
    <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">past</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">level</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">delete</span> <span class="nx">target</span><span class="p">[</span><span class="nx">level</span><span class="p">];</span>
    <span class="p">});</span>
  <span class="p">}</span>

  <span class="nx">target</span><span class="p">.</span><span class="nx">levels</span> <span class="o">=</span> <span class="nx">current</span> <span class="o">||</span> <span class="nx">config</span><span class="p">.</span><span class="nx">npm</span><span class="p">.</span><span class="nx">levels</span><span class="p">;</span>
  <span class="k">if</span> <span class="p">(</span><span class="nx">target</span><span class="p">.</span><span class="nx">padLevels</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">target</span><span class="p">.</span><span class="nx">levelLength</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">longestElement</span><span class="p">(</span><span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">target</span><span class="p">.</span><span class="nx">levels</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>Define prototype methods for each log level
 e.g. target.log('info', msg) &lt;=> target.info(msg)</p>             </td>             <td class="code">               <div class="highlight"><pre>  <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">target</span><span class="p">.</span><span class="nx">levels</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">level</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">target</span><span class="p">[</span><span class="nx">level</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">msg</span><span class="p">)</span> <span class="p">{</span>
      <span class="kd">var</span> <span class="nx">args</span>     <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">),</span>
          <span class="nx">callback</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">args</span><span class="p">[</span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">args</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span>
          <span class="nx">meta</span>     <span class="o">=</span> <span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">===</span> <span class="mi">2</span> <span class="o">?</span> <span class="nx">args</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span> <span class="o">:</span> <span class="kc">null</span><span class="p">;</span>

      <span class="k">return</span> <span class="nx">target</span><span class="p">.</span><span class="nx">log</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="p">});</span>
  
  <span class="k">return</span> <span class="nx">target</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>               <h3>function longestElement</h3>

<h4>@xs {Array} Array to calculate against</h4>

<p>Returns the longest element in the <code>xs</code> array.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">longestElement</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">xs</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span>
    <span class="kc">null</span><span class="p">,</span>
    <span class="nx">xs</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="nx">x</span><span class="p">.</span><span class="nx">length</span> <span class="p">})</span>
  <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>               <h3>function clone (obj)</h3>

<h4>@obj {Object} Object to clone.</h4>

<p>Helper method for deep cloning pure JSON objects
i.e. JSON objects that are either literals or objects (no Arrays, etc)</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">clone</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">copy</span> <span class="o">=</span> <span class="p">{};</span>
  <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">]))</span> <span class="p">{</span>
      <span class="nx">copy</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span>
      <span class="nx">copy</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nb">Object</span> <span class="o">?</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span> <span class="o">:</span> <span class="nx">obj</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="k">return</span> <span class="nx">copy</span><span class="p">;</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>               <h3>function log (options)</h3>

<h4>@options {Object} All information about the log serialization.</h4>

<p>Generic logging function for returning timestamped strings 
with the following options:</p>

<p>{
     level:     'level to add to serialized message',
     message:   'message to serialize',
     meta:      'additional logging metadata to serialize',
     colorize:  false, // Colorizes output (only if <code>.json</code> is false)
     timestamp: true   // Adds a timestamp to the serialized message
   }</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</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">options</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">timestampFn</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">:</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">timestamp</span><span class="p">,</span>
      <span class="nx">timestamp</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">?</span> <span class="nx">timestampFn</span><span class="p">()</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span>
      <span class="nx">meta</span>        <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">meta</span> <span class="o">?</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">meta</span><span class="p">)</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span>
      <span class="nx">output</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">json</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">output</span>         <span class="o">=</span> <span class="nx">meta</span> <span class="o">||</span> <span class="p">{};</span>
    <span class="nx">output</span><span class="p">.</span><span class="nx">level</span>   <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">level</span><span class="p">;</span>
    <span class="nx">output</span><span class="p">.</span><span class="nx">message</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">message</span><span class="p">;</span>
    
    <span class="k">if</span> <span class="p">(</span><span class="nx">timestamp</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">output</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">=</span> <span class="nx">timestamp</span><span class="p">;</span>
    <span class="p">}</span>
    
    <span class="k">return</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">output</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="nx">output</span> <span class="o">=</span> <span class="nx">timestamp</span> <span class="o">?</span> <span class="nx">timestamp</span> <span class="o">+</span> <span class="s1">&#39; - &#39;</span> <span class="o">:</span> <span class="s1">&#39;&#39;</span><span class="p">;</span>
  <span class="nx">output</span> <span class="o">+=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">colorize</span> <span class="o">?</span> <span class="nx">config</span><span class="p">.</span><span class="nx">colorize</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">level</span><span class="p">)</span> <span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">level</span><span class="p">;</span>
  <span class="nx">output</span> <span class="o">+=</span> <span class="p">(</span><span class="s1">&#39;: &#39;</span> <span class="o">+</span> <span class="nx">options</span><span class="p">.</span><span class="nx">message</span><span class="p">);</span>

  <span class="k">if</span> <span class="p">(</span><span class="nx">meta</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">meta</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span> <span class="o">&amp;&amp;</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">meta</span><span class="p">).</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
    <span class="nx">output</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nx">loggly</span><span class="p">.</span><span class="nx">serialize</span><span class="p">(</span><span class="nx">meta</span><span class="p">);</span>
  <span class="p">}</span>

  <span class="k">return</span> <span class="nx">output</span><span class="p">;</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 hash (str)</h3>

<h4>@str {string} String to hash.</h4>

<p>Utility function for creating unique ids
e.g. Profiling incoming HTTP requests on the same tick</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">hash</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">crypto</span><span class="p">.</span><span class="nx">createHash</span><span class="p">(</span><span class="s1">&#39;sha1&#39;</span><span class="p">).</span><span class="nx">update</span><span class="p">(</span><span class="nx">str</span><span class="p">).</span><span class="nx">digest</span><span class="p">(</span><span class="s1">&#39;hex&#39;</span><span class="p">);</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>               <h2>Borrowed from node.js core</h2>

<p>I wanted a universal lowercase header message, as opposed to the <code>DEBUG</code>
(i.e. all uppercase header) used only in <code>util.debug()</code></p>             </td>             <td class="code">               <div class="highlight"><pre><span class="kd">var</span> <span class="nx">months</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Jan&#39;</span><span class="p">,</span> <span class="s1">&#39;Feb&#39;</span><span class="p">,</span> <span class="s1">&#39;Mar&#39;</span><span class="p">,</span> <span class="s1">&#39;Apr&#39;</span><span class="p">,</span> 
              <span class="s1">&#39;May&#39;</span><span class="p">,</span> <span class="s1">&#39;Jun&#39;</span><span class="p">,</span> <span class="s1">&#39;Jul&#39;</span><span class="p">,</span> <span class="s1">&#39;Aug&#39;</span><span class="p">,</span> 
              <span class="s1">&#39;Sep&#39;</span><span class="p">,</span> <span class="s1">&#39;Oct&#39;</span><span class="p">,</span> <span class="s1">&#39;Nov&#39;</span><span class="p">,</span> <span class="s1">&#39;Dec&#39;</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>               <h3>function pad (n)</h3>

<p>Returns a padded string if <code>n &lt; 10</code>.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">pad</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">n</span> <span class="o">&lt;</span> <span class="mi">10</span> <span class="o">?</span> <span class="s1">&#39;0&#39;</span> <span class="o">+</span> <span class="nx">n</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">:</span> <span class="nx">n</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="mi">10</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>               <h3>function timestamp ()</h3>

<p>Returns a timestamp string for the current time.</p>             </td>             <td class="code">               <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">timestamp</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">d</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">();</span>
  <span class="kd">var</span> <span class="nx">time</span> <span class="o">=</span> <span class="p">[</span>
    <span class="nx">exports</span><span class="p">.</span><span class="nx">pad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getHours</span><span class="p">()),</span>
    <span class="nx">exports</span><span class="p">.</span><span class="nx">pad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getMinutes</span><span class="p">()),</span>
    <span class="nx">exports</span><span class="p">.</span><span class="nx">pad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getSeconds</span><span class="p">())</span>
  <span class="p">].</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">);</span>
              
  <span class="k">return</span> <span class="p">[</span><span class="nx">d</span><span class="p">.</span><span class="nx">getDate</span><span class="p">(),</span> <span class="nx">months</span><span class="p">[</span><span class="nx">d</span><span class="p">.</span><span class="nx">getMonth</span><span class="p">()],</span> <span class="nx">time</span><span class="p">].</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">);</span>
<span class="p">};</span>

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