From d65d053bc123345aa8be6aff31913869e5827cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Charvet=20=E9=BB=91=E7=93=9C?= Date: Fri, 4 Jul 2014 14:03:08 +0800 Subject: [PATCH] Format message before adding loggly metadata Also removed a throw err inside an asynchronous callback. --- lib/appenders/loggly.js | 53 ++++++++--------------------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/lib/appenders/loggly.js b/lib/appenders/loggly.js index a946c0e..9cd19f3 100644 --- a/lib/appenders/loggly.js +++ b/lib/appenders/loggly.js @@ -1,7 +1,9 @@ 'use strict'; var layouts = require('../layouts') , loggly = require('loggly') -, os = require('os'); +, os = require('os') +, passThrough = layouts.messagePassThroughLayout; + /** * Loggly Appender. Sends logging events to Loggly using node-loggly @@ -17,8 +19,8 @@ var layouts = require('../layouts') function logglyAppender(config, layout) { var client = loggly.createClient(config); - var packageMessage = function (loggingEvent) { - var BaseItem = function(level, msg) { + function packageMessage(loggingEvent) { + function BaseItem(level, msg) { this.level = level || loggingEvent.level.toString(); this.category = loggingEvent.categoryName; this.hostname = os.hostname().toString(); @@ -26,48 +28,13 @@ function logglyAppender(config, layout) { this.msg = msg; }; - var packageItem = function (item) { - if (item instanceof Error) - return new BaseItem('ERROR', item.message); - - if (['string', 'number', 'boolean'].indexOf(typeof item) > -1 ) - return new BaseItem(undefined, item); - - var obj = new BaseItem(); - if (Array.isArray(item)) - return item.unshift(obj); //add base object as first item - - if (item && Object.prototype.toString.call(item) === '[object Object]') { - for (var key in item) { - if (item.hasOwnProperty(key)) { - obj[key] = item[key]; //don't do packageItem on nested items, because level, category and hostname are needed on top level items only. - } - } - } - - return obj; - }; - - if (loggingEvent.data.length === 1) { - return packageItem(loggingEvent.data[0]); - } - //length >1 - var msg = loggingEvent.data; - for (var i = 0, l = msg.length; i < l; i++) { - msg[i] = packageItem(msg[i]); - } - - return msg; + var formattedMsg = passThrough(loggingEvent); + return new BaseItem(formattedMsg); }; - + return function(loggingEvent) { var a = layout ? layout(loggingEvent) : packageMessage(loggingEvent); - //console.log('log now', a); - client.log(a, config.tags, function(err, result) { - if (err) { - throw err; - } - }); + client.log(a, config.tags); }; } @@ -81,4 +48,4 @@ function configure(config) { exports.name = 'loggly'; exports.appender = logglyAppender; -exports.configure = configure; \ No newline at end of file +exports.configure = configure;