diff --git a/README.md b/README.md index b91b80d..cc618e7 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,13 @@ Options are the following: * __unique:__ Whether to log only the declared level and none above. *[boolean]* *[optional]* * __silent:__ Whether to suppress output. *[boolean]* *[optional]* * __disable_notification:__ Sends the message silently. *[boolean]* *[optional]* +* __template:__ Format output message. *[optional]* + +String template is based on named arguments: +``` js +'{level}' -> level of messages +'{message}' -> text of messages +``` ## Examples Using the Default Logger @@ -79,5 +86,24 @@ logger.error('All work and no play makes Jack a dull boy.'); logger.info('Come play with us, Danny. Forever... and ever... and ever.'); ``` +Using template output: +``` js +var winston = require('winston'); + +require('winston-telegram').Telegram; + +winston.add(winston.transports.Telegram, { + token : 'TELEGRAM_TOKEN', + chatid : 'CHAT_ID', + level : 'error', + unique : true, + template : '[{level}] [{message}]' + }); + +winston.log('error', 'Redrum. Redrum. Redrum.'); + +//Output: [error] [Redrum. Redrum. Redrum.] +``` + [0]: https://telegram.org/ [1]: https://github.com/flatiron/winston \ No newline at end of file diff --git a/lib/winston-telegram.js b/lib/winston-telegram.js index 5c0cc49..2ea86cc 100644 --- a/lib/winston-telegram.js +++ b/lib/winston-telegram.js @@ -8,6 +8,7 @@ var util = require('util'); var request = require('request'); var winston = require('winston'); +var nargs = /\{([0-9a-zA-Z_]+)\}/g; /** * @constructs @@ -27,6 +28,7 @@ var Telegram = exports.Telegram = function (options) { this.silent = options.silent || false; this.disable_notification = options.disable_notification || false; this.name = options.name || this.name; + this.template = options.template || '[{level}] {message}'; }; /** @extends winston.Transport */ @@ -62,7 +64,7 @@ Telegram.prototype.log = function (level, msg, meta, callback) { method : 'POST', json : { chat_id : this.chatid, - text : '['+level+'] '+msg, + text : format(this.template,{level : level, message : msg}), disable_notification : this.disable_notification } }, function(error, response, body){ @@ -75,4 +77,32 @@ Telegram.prototype.log = function (level, msg, meta, callback) { self.emit('logged'); callback(null, true); }); -}; \ No newline at end of file +}; + +function format(string) { + var args + if (arguments.length === 2 && typeof arguments[1] === 'object') { + args = arguments[1] + } else { + args = new Array(arguments.length - 1) + for (var i = 1; i < arguments.length; ++i) { + args[i - 1] = arguments[i] + } + } + if (!args || !args.hasOwnProperty) { + args = {} + } + return string.replace(nargs, function replaceArg(match, i, index) { + var result + if (string[index - 1] === '{' && + string[index + match.length] === '}') { + return i + } else { + result = args.hasOwnProperty(i) ? args[i] : null + if (result === null || result === undefined) { + return '' + } + return result + } + }) +} \ No newline at end of file diff --git a/package.json b/package.json index b97ce0a..6ee21b5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "winston-telegram", "description": "A Telegram transport for winston", - "version": "0.3.0", + "version": "0.4.0", "author": "Ivan Marban", "repository": { "type": "git", @@ -16,12 +16,9 @@ "bot" ], "dependencies": { - "request": "^2.73.0" + "request": "^2.75.0" }, "devDependencies": { - "winston": "" - }, - "peerDependencies": { "winston": "", "vows": "" },