Skip to content

Instantly share code, notes, and snippets.

@mrister
Last active January 19, 2019 17:49
Show Gist options
  • Save mrister/095654082b0fe263dd23f930eda69834 to your computer and use it in GitHub Desktop.
Save mrister/095654082b0fe263dd23f930eda69834 to your computer and use it in GitHub Desktop.
Morgan loggly stream custom class
const {Writable} = require('stream');
const loggly = require('node-loggly-bulk');
class MorganLogglyLoggerStream extends Writable {
/**
* Create a new instance of MorganLogglyLogger
* @param options {Object}
* @param options.TOKEN {String} your loggly token
* @param options.SUBDOMAIN {String} your loggly SUBDOMAIN
*/
constructor(options) {
// allows use to use any JS object instead of only string or Buffer
super({objectMode: true});
this.client = loggly.createClient({
token: options.TOKEN, // your loggly token
subdomain: options.SUBDOMAIN, // your loggly subdomain
tags: ['NodeJS'],
json: false // we will log only strings
});
// make sure write function has proper context (this)
this._write = this._write.bind(this);
}
// override the function from Writable
_write(message, encoding, callback) {
this.client.log(message, (err, {response}) => {
if (err) return callback(err);
// remove next if block because it is only here for demonstration
if (response === 'ok') {
console.info('Logged a message to loggly!');
}
// pass down the callback function to get err or response from client
return callback(null, response);
});
}
}
module.exports = MorganLogglyLoggerStream;
@DanielTamkin
Copy link

Line 31: Shouldn't it say "allows us to use any[...]", instead of saying "allows use to use any[...]". No?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment