02ed9862f7581567e59b373072170b6ea2907ee8.svn-base
21.9 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
<!DOCTYPE html> <html> <head> <title>loggly.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> loggly.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"> * loggly.js: Transport for logginh to remote Loggly API</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">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'events'</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">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">async</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'async'</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">'../common'</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">'./transport'</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">¶</a> </div> <h3>function Loggly (options)</h3>
<h4>@options {Object} Options for this instance.</h4>
<p>Constructor function for the Loggly transport object responsible
for persisting log messages and metadata to Loggly; 'LaaS'.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Loggly</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Loggly</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">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">subdomain</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'Loggly Subdomain is required'</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">inputToken</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">inputName</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'Target input token or name is required.'</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">.</span><span class="nx">auth</span> <span class="o">&&</span> <span class="nx">options</span><span class="p">.</span><span class="nx">inputName</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'Loggly authentication is required'</span><span class="p">);</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">'loggly'</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">logBuffer</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">this</span><span class="p">.</span><span class="nx">client</span> <span class="o">=</span> <span class="nx">loggly</span><span class="p">.</span><span class="nx">createClient</span><span class="p">({</span>
<span class="nx">subdomain</span><span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">subdomain</span><span class="p">,</span>
<span class="nx">auth</span><span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">auth</span> <span class="o">||</span> <span class="kc">null</span><span class="p">,</span>
<span class="nx">json</span><span class="o">:</span> <span class="nx">options</span><span class="p">.</span><span class="nx">json</span> <span class="o">||</span> <span class="kc">false</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">inputToken</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">inputToken</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">inputToken</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">ready</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">inputName</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">ready</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">inputName</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">inputName</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="k">this</span><span class="p">.</span><span class="nx">client</span><span class="p">.</span><span class="nx">getInput</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">inputName</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="nx">input</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">throw</span> <span class="nx">err</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">inputToken</span> <span class="o">=</span> <span class="nx">input</span><span class="p">.</span><span class="nx">input_token</span><span class="p">;</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">ready</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
<span class="p">});</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>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">Loggly</span><span class="p">,</span> <span class="nx">Transport</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> <p>Expose the name of this Transport on the prototype</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Loggly</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">'loggly'</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 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">Loggly</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">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>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">ready</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> <p>If we haven't gotten the input token yet
add this message to the log buffer.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">logBuffer</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">ready</span> <span class="o">&&</span> <span class="k">this</span><span class="p">.</span><span class="nx">logBuffer</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></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Otherwise if we have buffered messages
add this message to the buffer and flush them.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">logBuffer</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">message</span><span class="p">);</span>
<span class="k">this</span><span class="p">.</span><span class="nx">flush</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">else</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> <p>Otherwise just log the message as normal</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">client</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">inputToken</span><span class="p">,</span> <span class="nx">message</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'logged'</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">}</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></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <h3>function flush ()</h3>
<p>Flushes any buffered messages to the current <code>stream</code>
used by this instance.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Loggly</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">flush</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">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">logMsg</span> <span class="p">(</span><span class="nx">msg</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">client</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">inputToken</span><span class="p">,</span> <span class="nx">msg</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>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">next</span><span class="p">();</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> <p>Initiate calls to loggly for each message in the buffer</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">logBuffer</span><span class="p">,</span> <span class="nx">logMsg</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="nx">self</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'logged'</span><span class="p">);</span>
<span class="p">});</span>
<span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span><span class="p">(</span><span class="kd">function</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">¶</a> </div> <p>Then quickly truncate the list</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">logBuffer</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="p">};</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>