Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
/*
2
* transport.js: Base Transport object for all Winston transports.
3
*
4
* (C) 2010 Charlie Robbins
5
* MIT LICENCE
6
*
7
*/
8
9
var events = require('events'),
10
util = require('util');
11
12
//
13
// ### function Transport (options)
14
// #### @options {Object} Options for this instance.
15
// Constructor function for the Tranport object responsible
16
// base functionality for all winston transports.
17
//
18
var Transport = exports.Transport = function (options) {
19
events.EventEmitter.call(this);
20
21
options = options || {};
22
this.level = options.level || 'info';
23
this.silent = options.silent || false;
24
this.raw = options.raw || false;
25
26
this.handleExceptions = options.handleExceptions || false;
27
};
28
29
//
30
// Inherit from `events.EventEmitter`.
31
//
32
util.inherits(Transport, events.EventEmitter);
33
34
//
35
// ### function formatQuery (query)
36
// #### @query {string|Object} Query to format
37
// Formats the specified `query` Object (or string) to conform
38
// with the underlying implementation of this transport.
39
//
40
Transport.prototype.formatQuery = function (query) {
41
return query;
42
};
43
44
//
45
// ### function normalizeQuery (query)
46
// #### @options {string|Object} Query to normalize
47
// Normalize options for query
48
//
49
Transport.prototype.normalizeQuery = function (options) {
50
//
51
// Use options similar to loggly.
52
// [See Loggly Search API](http://wiki.loggly.com/retrieve_events#optional)
53
//
54
55
options = options || {};
56
57
// limit
58
options.rows = options.rows || options.limit || 10;
59
60
// starting row offset
61
options.start = options.start || 0;
62
63
// now - 24
64
options.from = options.from || new Date - (24 * 60 * 60 * 1000);
65
if (typeof options.from !== 'object') {
66
options.from = new Date(options.from);
67
}
68
69
// now
70
options.until = options.until || new Date;
71
if (typeof options.until !== 'object') {
72
options.until = new Date(options.until);
73
}
74
75
// 'asc' or 'desc'
76
options.order = options.order || 'desc';
77
78
// which fields to select
79
options.fields = options.fields;
80
81
return options;
82
};
83
84
//
85
// ### function formatResults (results, options)
86
// #### @results {Object|Array} Results returned from `.query`.
87
// #### @options {Object} **Optional** Formatting options
88
// Formats the specified `results` with the given `options` accordinging
89
// to the implementation of this transport.
90
//
91
Transport.prototype.formatResults = function (results, options) {
92
return results;
93
};
94
95
//
96
// ### function logException (msg, meta, callback)
97
// #### @msg {string} Message to log
98
// #### @meta {Object} **Optional** Additional metadata to attach
99
// #### @callback {function} Continuation to respond to when complete.
100
// Logs the specified `msg`, `meta` and responds to the callback once the log
101
// operation is complete to ensure that the event loop will not exit before
102
// all logging has completed.
103
//
104
Transport.prototype.logException = function (msg, meta, callback) {
105
var self = this;
106
107
function onLogged () {
108
self.removeListener('error', onError);
109
callback();
110
}
111
112
function onError () {
113
self.removeListener('logged', onLogged);
114
callback();
115
}
116
117
this.once('logged', onLogged);
118
this.once('error', onError);
119
this.log('error', msg, meta, function () { });
120
};
121
122