d21e98d9d366b2a036601fea408b5fe7229a739b.svn-base
26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!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 … <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">¶</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">'util'</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">'crypto'</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">'loggly'</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">'./config'</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">¶</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">¶</a> </div> <p>Define prototype methods for each log level
e.g. target.log('info', msg) <=> 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">'function'</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">¶</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">¶</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">¶</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">'function'</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">' - '</span> <span class="o">:</span> <span class="s1">''</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">': '</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">&&</span> <span class="k">typeof</span> <span class="nx">meta</span> <span class="o">===</span> <span class="s1">'object'</span> <span class="o">&&</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">></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">' '</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">¶</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">'sha1'</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">'hex'</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">¶</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">'Jan'</span><span class="p">,</span> <span class="s1">'Feb'</span><span class="p">,</span> <span class="s1">'Mar'</span><span class="p">,</span> <span class="s1">'Apr'</span><span class="p">,</span>
<span class="s1">'May'</span><span class="p">,</span> <span class="s1">'Jun'</span><span class="p">,</span> <span class="s1">'Jul'</span><span class="p">,</span> <span class="s1">'Aug'</span><span class="p">,</span>
<span class="s1">'Sep'</span><span class="p">,</span> <span class="s1">'Oct'</span><span class="p">,</span> <span class="s1">'Nov'</span><span class="p">,</span> <span class="s1">'Dec'</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">¶</a> </div> <h3>function pad (n)</h3>
<p>Returns a padded string if <code>n < 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"><</span> <span class="mi">10</span> <span class="o">?</span> <span class="s1">'0'</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">¶</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">':'</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">' '</span><span class="p">);</span>
<span class="p">};</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>