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

Add messageFormat #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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');

Expand All @@ -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:
Expand Down
65 changes: 40 additions & 25 deletions lib/winston-papertrail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
};
Expand All @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions test/papertrail-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down