Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / prompt / node_modules / winston / lib / winston / transports / webhook.js
1 /*
2  * webhook.js: Transport for logging to remote http endpoints ( POST / RECEIVE webhooks )
3  *
4  * (C) 2011 Marak Squires
5  * MIT LICENCE
6  *
7  */
8
9 var events = require('events'),
10     http = require('http'),
11     https = require('https'),
12     util = require('util'),
13     common = require('../common'),
14     Transport = require('./transport').Transport; 
15
16 //
17 // ### function WebHook (options)
18 // #### @options {Object} Options for this instance.
19 // Constructor function for the Console transport object responsible
20 // for making arbitrary HTTP requests whenever log messages and metadata
21 // are received.
22 //
23 var Webhook = exports.Webhook = function (options) {
24   Transport.call(this, options);
25
26   this.name   = 'webhook'; 
27   this.host   = options.host   || 'localhost';
28   this.port   = options.port   || 8080;
29   this.method = options.method || 'POST';
30   this.path   = options.path   || '/winston-log';
31
32   if (options.auth) {
33     this.auth = {};
34     this.auth.username = options.auth.username || '';
35     this.auth.password = options.auth.password || '';
36   }
37   
38   if (options.ssl) {
39     this.ssl      = {};
40     this.ssl.key  = options.ssl.key  || null;
41     this.ssl.cert = options.ssl.cert || null;
42     this.ssl.ca   = options.ssl.ca;
43   }
44 };
45
46 //
47 // Inherit from `winston.Transport`.
48 //
49 util.inherits(Webhook, Transport);
50
51 //
52 // Expose the name of this Transport on the prototype
53 //
54 Webhook.prototype.name = 'webhook';
55
56 //
57 // ### function log (level, msg, [meta], callback)
58 // #### @level {string} Level at which to log the message.
59 // #### @msg {string} Message to log
60 // #### @meta {Object} **Optional** Additional metadata to attach
61 // #### @callback {function} Continuation to respond to when complete.
62 // Core logging method exposed to Winston. Metadata is optional.
63 //
64 Webhook.prototype.log = function (level, msg, meta, callback) {
65   if (this.silent) {
66     return callback(null, true);
67   }
68   
69   var self = this,
70       message = common.clone(meta),
71       options,
72       req;
73       
74   message.level = level;
75   message.message = msg;
76
77   // Prepare options for outgoing HTTP request
78   options = {
79     host: this.host,
80     port: this.port,
81     path: this.path,
82     method: this.method,
83     headers: { 'Content-Type': 'application/json' }
84   };
85
86   if (this.ssl) {
87     options.ca = this.ssl.ca;
88     options.key = this.ssl.key;
89     options.cert = this.ssl.cert;
90   }
91   
92   if (this.auth) {
93     // Encode `Authorization` header used by Basic Auth
94     options.headers['Authorization'] = 'Basic ' + new Buffer(
95       this.auth.username + ':' + this.auth.password, 'utf8'
96     ).toString('base64');
97   }
98
99   // Perform HTTP logging request
100   req = (self.ssl ? https : http).request(options, function (res) {
101     //
102     // No callback on request, fire and forget about the response
103     //
104     self.emit('logged');
105   }); 
106
107   req.on('error', function (err) {
108     //
109     // Propagate the `error` back up to the `Logger` that this
110     // instance belongs to.
111     //
112     self.emit('error', err);
113   });
114   
115   //
116   // Write logging event to the outgoing request body
117   //
118   // jsonMessage is currently conforming to JSON-RPC v1.0, 
119   // but without the unique id since there is no anticipated response 
120   // see: http://en.wikipedia.org/wiki/JSON-RPC
121   // 
122   req.write(JSON.stringify({ 
123     method: 'log', 
124     params: { 
125       timestamp: new Date(),
126       msg: msg, 
127       level: level, 
128       meta: meta 
129     } 
130   }));
131   
132   req.end();
133   
134   // Always return true, regardless of any errors
135   callback(null, true);
136 };