Skip to content

Commit

Permalink
Post all compact Seq events, and added test for compact format
Browse files Browse the repository at this point in the history
  • Loading branch information
Wedvich committed Dec 24, 2016
1 parent 5a40a65 commit 93b3f6b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
9 changes: 5 additions & 4 deletions dist/structured-log-seq-sink.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ var SeqSink = function () {

this.emit = function (events, done) {
var seqEvents = _this.compact ? events.reduce(function (s, e) {
return JSON.stringify(_extends({
return s + JSON.stringify(_extends({
'@l': mapLogLevel(e.level),
'@mt': e.messageTemplate.raw,
'@t': e.timestamp
}, e.properties)) + '\n';
}, '') : events.map(function (e) {
}, '').replace(/\s+$/g, '') : events.map(function (e) {
return {
'Level': e.level,
'MessageTemplate': e.messageTemplate.raw,
Expand Down Expand Up @@ -112,12 +112,13 @@ function postToSeq(url, apiKey, compact, body, storageKey, done) {
}

function mapLogLevel(logLevel) {
// If the log isn't numeric (structured-log < 0.1.0), just return it

// If the log level isn't numeric (structured-log < 0.1.0), return it as-is.
if (isNaN(logLevel)) {
return logLevel;
}

// Parse numeric log level (structured-log >= 0.1.0)
// Parse numeric log level (structured-log >= 0.1.0).
switch (logLevel) {
case 0:
return 'Fatal';
Expand Down
9 changes: 5 additions & 4 deletions dist/structured-log-seq-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ var SeqSink = function () {

this.emit = function (events, done) {
var seqEvents = _this.compact ? events.reduce(function (s, e) {
return JSON.stringify(_extends({
return s + JSON.stringify(_extends({
'@l': mapLogLevel(e.level),
'@mt': e.messageTemplate.raw,
'@t': e.timestamp
}, e.properties)) + '\n';
}, '') : events.map(function (e) {
}, '').replace(/\s+$/g, '') : events.map(function (e) {
return {
'Level': e.level,
'MessageTemplate': e.messageTemplate.raw,
Expand Down Expand Up @@ -118,12 +118,13 @@ function postToSeq(url, apiKey, compact, body, storageKey, done) {
}

function mapLogLevel(logLevel) {
// If the log isn't numeric (structured-log < 0.1.0), just return it

// If the log level isn't numeric (structured-log < 0.1.0), return it as-is.
if (isNaN(logLevel)) {
return logLevel;
}

// Parse numeric log level (structured-log >= 0.1.0)
// Parse numeric log level (structured-log >= 0.1.0).
switch (logLevel) {
case 0:
return 'Fatal';
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ class SeqSink {

emit = (events, done) => {
const seqEvents = this.compact ? events.reduce((s, e) => {
return JSON.stringify({
return s + JSON.stringify({
'@l': mapLogLevel(e.level),
'@mt': e.messageTemplate.raw,
'@t': e.timestamp,
...e.properties
}) + '\n';
}, ''): events.map(e => {
}, '').replace(/\s+$/g, '') : events.map(e => {
return {
'Level': e.level,
'MessageTemplate': e.messageTemplate.raw,
'Properties': e.properties,
'Timestamp': e.timestamp
};
});


const body = this.compact ? seqEvents : JSON.stringify({
'Events': seqEvents
Expand Down
79 changes: 52 additions & 27 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var structuredLog = require('structured-log');
var seqSink = require('../dist/structured-log-seq-sink');

var fetchMock = require('fetch-mock');
Expand Down Expand Up @@ -36,38 +35,64 @@ describe('toString', function () {
});

describe('emit', function () {
beforeEach(fetchMock.reset);

it('should POST a well-formatted Seq event', function () {
var called = false;
var sink = seqSink({ url: 'http://mock' });
var originalEmit = sink.emit;
var stubEmit = sinon.stub(sink, 'emit', function (events, done) {
originalEmit.apply(sink, arguments).then(function () {
called = true;

var events = [{
timestamp: new Date().toISOString(),
messageTemplate: { raw: 'Event example with a template parameter: {@sample}' },
level: 'Warning',
properties: { sample: { count: 5 } }
}];

var emitPromise = Promise.resolve();
return emitPromise
.then(sink.emit(events, emitPromise.resolve))
.then(function () {
var requestBody = JSON.parse(fetchMock.lastCall()[1].body);
assert.property(requestBody, 'Events');
var logEvent = requestBody.Events[0];
assert.propertyVal(logEvent, 'Level', 'Warning');
assert.propertyVal(logEvent, 'MessageTemplate', 'Event example with a template parameter: {@sample}');
assert.deepPropertyVal(logEvent, 'Properties.sample.count', 5);
assert.property(logEvent, 'Timestamp');
});
});
});

it('should POST a well-formatted compact Seq event', function () {
var sink = seqSink({ url: 'http://mock', compact: true });

var logger = structuredLog.configure()
.writeTo(sink)
.create();
var events = [{
timestamp: new Date().toISOString(),
messageTemplate: { raw: 'Event example with a template parameter: {@sample}' },
level: 'Warning',
properties: { sample: { count: 5 } }
}, {
timestamp: new Date().toISOString(),
messageTemplate: { raw: 'Event example with a template parameter: {counter}' },
level: 'Debug',
properties: { counter: 21 }
}];

return new Promise(function (resolve) {
var emitPromise = Promise.resolve();
return emitPromise
.then(sink.emit(events, emitPromise.resolve))
.then(function () {
var logEvents = fetchMock.lastCall()[1].body.split('\n').map(JSON.parse);

logger.warn('Event example with a template parameter: {@Sample}', { Count: 5 });
var logEvent1 = logEvents[0];
assert.propertyVal(logEvent1, '@l', 'Warning');
assert.propertyVal(logEvent1, '@mt', 'Event example with a template parameter: {@sample}');
assert.deepPropertyVal(logEvent1, 'sample.count', 5);
assert.property(logEvent1, '@t');

var t = setTimeout(function cb() {
if (called) {
clearTimeout(t);
var requestBody = JSON.parse(fetchMock.lastCall()[1].body);
assert.isTrue(requestBody.hasOwnProperty('Events'));
var logEvent = requestBody.Events[0];
assert.equal(logEvent.Level, 'WARN');
assert.equal(logEvent.MessageTemplate, 'Event example with a template parameter: {@Sample}');
assert.deepEqual(logEvent.Properties, { Sample: { Count: 5 } });
assert.isTrue(logEvent.hasOwnProperty('Timestamp'));
resolve();
}
setTimeout(cb, 10);
}, 1);
});
var logEvent2 = logEvents[1];
assert.propertyVal(logEvent2, '@l', 'Debug');
assert.propertyVal(logEvent2, '@mt', 'Event example with a template parameter: {counter}');
assert.propertyVal(logEvent2, 'counter', 21);
assert.property(logEvent2, '@t');
});
});
});

0 comments on commit 93b3f6b

Please sign in to comment.