From c42ddd5d1790354f51553280265c2d9a6279c494 Mon Sep 17 00:00:00 2001 From: Cymen Vig Date: Mon, 4 Jan 2016 16:21:58 -0800 Subject: [PATCH] Add messageFormat --- README.md | 31 +++++++++++++++++++ lib/winston-papertrail.js | 65 ++++++++++++++++++++++++--------------- test/papertrail-test.js | 30 ++++++++++++++++++ 3 files changed, 101 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 99a1b51..f051178 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ There are a number of optional settings: - `program` - The program for your transport, defaults to `default` - `facility` - The syslog facility for this transport, defaults to `daemon` - `logFormat` - A function to format your log message before sending, see below +- `messageFormat` - A function to format entire message before sending, see below - `colorize` - Enable colors in Papertrail, defaults to `false` - `inlineMeta` - Inline multi-line messages, defaults to `false` - `handleExceptions` - Tell this Transport to handle exceptions, defaults to `false` @@ -70,6 +71,8 @@ There are also a number of settings for connection failure and retry behavior For more some advanced logging, you can take advantage of custom formatting for Papertrail: +### logFormat + ``` js var winston = require('winston'); @@ -94,6 +97,34 @@ Papertrail: logger.info('this is my message'); ``` +Note that internally, `winston-papertrial` splits a multiple line message into multiple log entries (one per line). `logFormat` will be applied after the split. If you want to not split the message by line or have access to the meta data, see `messageFormat` below. + +### messageFormat + +``` js + var winston = require('winston'); + + // + // Requiring `winston-papertrail` will expose + // `winston.transports.Papertrail` + // + require('winston-papertrail').Papertrail; + + var logger = new winston.Logger({ + transports: [ + new winston.transports.Papertrail({ + host: 'logs.papertrailapp.com', + port: 12345, + messageFormat: function(level, message, meta) { + return 'message: ' + message + ', meta: ' + meta; + } + }) + ] + }); + + logger.info('this is my message'); +``` + ## Transport Events The Papertrail transport is also capable of emitting events for `error` and `connect` so you can log to other transports: diff --git a/lib/winston-papertrail.js b/lib/winston-papertrail.js index 438d5f5..c4289da 100644 --- a/lib/winston-papertrail.js +++ b/lib/winston-papertrail.js @@ -39,6 +39,8 @@ var os = require('os'), * * @param {Function} [options.logFormat] function to format your log message before sending * + * @param {Function} [options.messageFormat] function to format your whole message before sending + * * @param {Number} [options.attemptsBeforeDecay] how many reconnections should * be attempted before backing of (5) * @@ -97,6 +99,9 @@ var Papertrail = exports.Papertrail = function (options) { return level + ' ' + message; }; + // Format the whole message as one log message prior to delivery + self.messageFormat = options.messageFormat; + // Number of attempts before decaying reconnection self.attemptsBeforeDecay = options.attemptsBeforeDecay || 5; @@ -298,7 +303,7 @@ Papertrail.prototype.log = function (level, msg, meta, callback) { } } - this.sendMessage(this.hostname, this.program, level, output); + this.sendMessage(this.hostname, this.program, level, output, meta); callback(null, true); }; @@ -313,41 +318,51 @@ Papertrail.prototype.log = function (level, msg, meta, callback) { * @param {String} level Log level of the message * @param {String} message The message to deliver */ -Papertrail.prototype.sendMessage = function (hostname, program, level, message) { - +Papertrail.prototype.sendMessage = function (hostname, program, level, message, meta) { var self = this, lines = [], msg = '', gap = ''; - // Only split if we actually have a message - if (message) { - lines = message.split('\n'); + if (this.messageFormat) { + msg = this.producer.produce({ + severity: level, + host: hostname, + appName: program, + date: new Date(), + message: self.messageFormat(level, message, meta) + }) + '\r\n'; } else { - lines = ['']; - } + // Only split if we actually have a message + if (message) { + lines = message.split('\n'); + } + else { + lines = ['']; + } - // If the incoming message has multiple lines, break them and format each - // line as it's own message - for (var i = 0; i < lines.length; i++) { + // If the incoming message has multiple lines, break them and format each + // line as it's own message + for (var i = 0; i < lines.length; i++) { - // don't send extra message if our message ends with a newline - if ((lines[i].length === 0) && (i == lines.length - 1)) { - break; - } + // don't send extra message if our message ends with a newline + if ((lines[i].length === 0) && (i == lines.length - 1)) { + break; + } - if (i == 1) { - gap = ' '; - } + if (i == 1) { + gap = ' '; + } - msg += self.producer.produce({ - severity: level, - host: hostname, - appName: program, - date: new Date(), - message: self.logFormat(self.colorize ? winston.config.colorize(level) : level, gap + lines[i]) - }) + '\r\n'; + msg += self.producer.produce({ + severity: level, + host: hostname, + appName: program, + date: new Date(), + message: self.logFormat(self.colorize ? winston.config.colorize(level) : level, gap + lines[i]) + }) + '\r\n'; + } } if (this.stream && this.stream.writable) { diff --git a/test/papertrail-test.js b/test/papertrail-test.js index b25ab59..d64e686 100644 --- a/test/papertrail-test.js +++ b/test/papertrail-test.js @@ -218,6 +218,36 @@ describe('connection tests', function() { } }); + it('should support messageFormat', function (done) { + var pt = new Papertrail({ + host: 'localhost', + port: 23456, + attemptsBeforeDecay: 0, + connectionDelay: 10000, + messageFormat: function(level, message, meta) { + return 'message: ' + message + ', meta: ' + meta; + } + }); + + pt.on('error', function (err) { + should.not.exist(err); + }); + + pt.on('connect', function () { + (function() { + pt.log('info', 'some message', 'some meta', function () { + + }); + }).should.not.throw(); + }); + + listener = function (data) { + should.exist(data); + data.toString().indexOf('message: some message some meta, meta: some meta\r\n').should.not.equal(-1); + done(); + } + }); + // TODO need to fix the TLS Server to reject new sockets that are not over tls it.skip('should fail to connect without tls', function (done) { var pt = new Papertrail({