Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Loggly appender should not make use of any layout #175

Merged
merged 1 commit into from

2 participants

@devotis

Stop making use of any layout by default, because they are intended to
format a line for human reading. Loggly indexes the values (of all
properties of objects) and makes them available for querying.

@devotis devotis Stop making use of any layout
Stop making use of any layout by default, because they are intended to
format a line for human reading. Loggly indexes the values (of all
properties of objects) and makes them available for querying.
ae1a55f
@nomiddlename

Sorry - don't know why I missed this one. Thanks for the PR.

@nomiddlename nomiddlename merged commit b2edbb1 into nomiddlename:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2014
  1. @devotis

    Stop making use of any layout

    devotis authored
    Stop making use of any layout by default, because they are intended to
    format a line for human reading. Loggly indexes the values (of all
    properties of objects) and makes them available for querying.
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 8 deletions.
  1. +55 −8 lib/appenders/loggly.js
View
63 lib/appenders/loggly.js
@@ -1,6 +1,6 @@
-"use strict";
-var layouts = require("../layouts")
-, loggly = require("loggly")
+'use strict';
+var layouts = require('../layouts')
+, loggly = require('loggly')
, os = require('os');
/**
@@ -12,15 +12,62 @@ var layouts = require("../layouts")
* subdomain: 'your-subdomain',
* tags: ['loggly-tag1', 'loggly-tag2', .., 'loggly-tagn']
* }
- * @param layout a function that takes a logevent and returns a string (defaults to basicLayout).
+ * @param layout a function that takes a logevent and returns a string (defaults to objectLayout).
*/
function logglyAppender(config, layout) {
- layout = layout || layouts.basicLayout;
-
var client = loggly.createClient(config);
+
+ var packageMessage = function (loggingEvent) {
+ var BaseItem = function(level, msg) {
+ this.level = level || loggingEvent.level.toString();
+ this.category = loggingEvent.categoryName;
+ this.hostname = os.hostname().toString();
+ if (typeof msg !== 'undefined')
+ 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;
+ };
return function(loggingEvent) {
- client.log(layout(loggingEvent), config.tags);
+ var a = layout ? layout(loggingEvent) : packageMessage(loggingEvent);
+ //console.log('log now', a);
+ client.log(a, config.tags, function(err, result) {
+ if (err) {
+ throw err;
+ }
+ });
};
}
@@ -32,6 +79,6 @@ function configure(config) {
return logglyAppender(config, layout);
}
-exports.name = "loggly";
+exports.name = 'loggly';
exports.appender = logglyAppender;
exports.configure = configure;
Something went wrong with that request. Please try again.