Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 20 additions & 35 deletions lib/winston-loggly.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ Loggly.prototype.stream = function(options) {

Loggly.prototype.query = function (options, callback) {
var self = this,
meta = this.extractMeta(options),
context = this.extractContext(options);
options = this.loglify(options);
options = this.extend(options, context);

this.client
.search(this.loglify(options))
.context(context)
.meta(meta)
.search(options)
.run(function (err, logs) {
return err
? callback(err)
Expand Down Expand Up @@ -217,12 +216,13 @@ Loggly.prototype.formatResults = function (results, options) {
Loggly.prototype.extractContext = function (obj) {
var context = {};

['rows',
'start',

['start',
'from',
'until',
'order',
'callback',
'size',
'format',
'fields'].forEach(function (key) {
if (obj[key]) {
Expand All @@ -237,29 +237,11 @@ Loggly.prototype.extractContext = function (obj) {

context.from = context.from || '-1d';
context.until = context.until || 'now';
context.rows = context.rows || 50;
context.size = context.size || 50;

return context;
};

//
// ### function extractContext (obj)
// #### @obj {Object} Options has to extract Loggly 'meta' properties from
// Returns a separate object containing all Loggly 'meta' properties in
// the object supplied and removes those properties from the original object.
// [See Loggly Search Language Guide](http://wiki.loggly.com/searchguide)
//
Loggly.prototype.extractMeta = function (obj) {
var meta = {};
['ip', 'address', 'device', 'inputname', 'inputid'].forEach(function (key) {
if (obj[key]) {
meta[key] = obj[key];
delete obj[key];
}
});

return meta;
};

//
// ### function loglify (obj)
Expand All @@ -273,15 +255,19 @@ Loggly.prototype.loglify = function (obj) {

Object.keys(obj).forEach(function (key) {
if (key !== 'query') {
opts.push('json.' + key + ':' + obj[key]);
if (key == 'tag') {
opts.push(key + ':' + obj[key]);
}
else {
opts.push('json.' + key + ':' + obj[key]);
}
}
});

if (obj.query) {
opts.unshift(obj.query);
}

return opts.join(' AND ');
return {'query' : opts.join(' AND ')}
};

//
Expand All @@ -295,12 +281,11 @@ Loggly.prototype.loglify = function (obj) {
// 3. Input IDs
//
Loggly.prototype.sanitizeLogs = function (logs) {
logs.context.query = logs.context.query.replace(/\s*inputname\:\w+\s*/ig, '');
logs.data.forEach(function (item) {
delete item.ip;
delete item.inputId;
delete item.inputname
});

return logs;
};

Loggly.prototype.extend = function(destination,source) {
for (var property in source)
destination[property] = source[property];
return destination;
}