Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support winston 3 #47

Closed
wants to merge 20 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -17,9 +17,9 @@ A client implementation for Loggly in node.js. Check out Loggly's [Node logging
// Requiring `winston-loggly-bulk` will expose
// `winston.transports.Loggly`
//
require('winston-loggly-bulk');
var {Loggly} = require('winston-loggly-bulk');
winston.add(winston.transports.Loggly, options);
winston.add(new Loggly({options}));
```

The Loggly transport is based on [Nodejitsu's][2] [node-loggly][3] implementation of the [Loggly][0] API. If you haven't heard of Loggly before, you should probably read their [value proposition][4]. The Loggly transport takes the following options. Either 'inputToken' or 'inputName' is required:
@@ -38,9 +38,26 @@ The Loggly transport is based on [Nodejitsu's][2] [node-loggly][3] implementatio
- __retriesInMilliSeconds:__ Time in milliseconds to retry sending buffered logs.
* __timestamp:__ If false, library will not include timestamp in log events.
- __Note:__ Library includes timestamp by default when we do not set timestamp option.
* __networkErrorsOnConsole:__ The library keep track of different network errors and can log them to console. By default, logging errors on console is disabled and can be enabled easily by setting this parameter's value to `true`. If true, all the network errors will be logged to console.

*Metadata:* Logged in suggested [Loggly format][5]

## Sample Working Code Snippet

``` js
var winston = require('winston');
var {Loggly} = require('winston-loggly-bulk');
winston.add(new Loggly({
token: "TOKEN",
subdomain: "SUBDOMAIN",
tags: ["Winston-NodeJS"],
json: true
}));
winston.log('info', "Hello World from Node.js!");
```

## Buffer Support

This library has buffer support during temporary network outage. User can configure size of buffer (no. of logs to be stored during network outage).
@@ -61,11 +78,11 @@ Our library uses ajax requests to send logs to Loggly, and as ajax requests take
Here is an example of how to use the method:

``` js
var winston = require('winston'),
winlog = require('winston-loggly-bulk');
var winston = require('winston');
var {flushLogsAndExit} = require('winston-loggly-bulk');
winston.log("info", "hello World");
winlog.flushLogsAndExit();
winston.log("info", "Hello World from Node.js!");
flushLogsAndExit();
```

47 lib/winston-loggly.js 100644 → 100755
@@ -6,10 +6,11 @@
*
*/

var events = require('events'),
var clone = require('clone'),
loggly = require('node-loggly-bulk'),
util = require('util'),
winston = require('winston'),
Transport = require('winston-transport'),
Stream = require('stream').Stream;

//
@@ -35,7 +36,7 @@ var Loggly = exports.Loggly = function (options) {
options.token = options.inputToken;
}

winston.Transport.call(this, options);
Transport.call(this, options);
if (!options.subdomain) {
throw new Error('Loggly Subdomain is required');
}
@@ -52,12 +53,13 @@ var Loggly = exports.Loggly = function (options) {
this.client = loggly.createClient({
subdomain: options.subdomain,
auth: options.auth || null,
json: options.json || false,
json: options.json || false, //TODO: should be false
proxy: options.proxy || null,
token: options.token,
tags: tags,
isBulk: options.isBulk || false,
bufferOptions: options.bufferOptions || {size: 500, retriesInMilliSeconds: 30 * 1000}
bufferOptions: options.bufferOptions || {size: 500, retriesInMilliSeconds: 30 * 1000},
networkErrorsOnConsole: options.networkErrorsOnConsole || false
});

this.timestamp = options.timestamp === false ? false : true;
@@ -79,7 +81,7 @@ var flushLogsAndExit = exports.flushLogsAndExit = function () {
//
// Inherit from `winston.Transport`.
//
util.inherits(Loggly, winston.Transport);
util.inherits(Loggly, Transport);

//
// Define a getter so that `winston.transports.Loggly`
@@ -93,6 +95,16 @@ winston.transports.flushLogsAndExit = flushLogsAndExit;
//
Loggly.prototype.name = 'loggly';

const validateMetadata = (meta) => {
if (meta == null) {
return {};
} else if (typeof meta !== 'object') {
return { metadata: meta };
} else {
return clone(meta);
}
}

//
// ### function log (level, msg, [meta], callback)
// #### @level {string} Level at which to log the message.
@@ -102,21 +114,22 @@ Loggly.prototype.name = 'loggly';
// Core logging method exposed to Winston. Metadata is optional.
//
Loggly.prototype.log = function (level, msg, meta, callback) {

const message = validateMetadata(meta);

if (this.silent) {
return callback(null, true);
}

if (this.timestamp && (!meta || !meta.timestamp)) {
meta = meta || {};
meta.timestamp = (new Date()).toISOString();
if (this.timestamp && !message.timestamp) {
message.timestamp = (new Date()).toISOString();
}

if (this.stripColors) {
msg = ('' + msg).replace(code, '');
}

var message = winston.clone(meta || {}),
self = this;
const self = this;

message.level = level;
message.message = msg || message.message;
@@ -129,7 +142,7 @@ Loggly.prototype.log = function (level, msg, meta, callback) {
callback(err, true);
}

return meta && meta.tags
return (meta && meta.tags)
? this.client.log(message, meta.tags, logged)
: this.client.log(message, logged);
};
@@ -139,9 +152,9 @@ Loggly.prototype.log = function (level, msg, meta, callback) {
// #### @options {Object} Set stream options
// Returns a log stream.
//
Loggly.prototype.stream = function(options) {
Loggly.prototype.stream = function(maybeOptions) {
var self = this,
options = options || {},
options = maybeOptions || {},
stream = new Stream,
last,
start = options.start,
@@ -173,15 +186,15 @@ Loggly.prototype.stream = function(options) {
return setTimeout(check, 2000);
}

var result = res[res.length-1];
var result = results[results.length-1];
if (result && result.timestamp) {
if (last == null) {
last = result.timestamp;
return;
}
last = result.timestamp;
} else {
return func();
return;
}

results.forEach(function(log) {
@@ -234,10 +247,10 @@ Loggly.prototype.formatQuery = function (query) {
// ### function formatResults (results, options)
// #### @results {Object|Array} Results returned from `.query`.
// #### @options {Object} **Optional** Formatting options
// Formats the specified `results` with the given `options` accordinging
// Formats the specified `results` with the given `options` according
// to the implementation of this transport.
//
Loggly.prototype.formatResults = function (results, options) {
Loggly.prototype.formatResults = function (results, _options) {
return results;
};

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.