forked from structured-log/structured-log-http-sink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
serilog-seq-sink.js
96 lines (80 loc) · 2.99 KB
/
serilog-seq-sink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
// Copyright 2014 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// UMD bolierplate based on https://github.com/umdjs/umd/blob/master/returnExports.js
// Supports node.js, AMD and the browser.
//
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.structuredLog.sink.seq = factory();
}
}(this, function () {
var SeqSink = function (options) {
// Argument checking.
if (!options) {
throw new Error("'options' parameter is required.");
}
if (!options.url) {
throw new Error("'options.url' parameter is required.");
}
var self = this;
self.toString = function () { return 'SeqSink'; };
options = options || {};
var levels = {
ERROR: "Error",
WARN: "Warning",
INFO: "Information",
DEBUG: "Debug",
VERBOSE: "Verbose"
};
self.emit = function (evt) {
var body = {
events: []
};
if (evt.constructor === Array) {
for (var i = 0; i < evt.length; i++) {
var renderedMsg = evt[i].messageTemplate.render(evt[i].properties);
body.events.push({
Timestamp: evt[i].timestamp,
Level: levels[evt[i].level],
MessageTemplate: evt[i].messageTemplate.raw,
RenderedMessage: renderedMsg,
Properties: evt[i].properties
});
}
}
else {
renderedMsg = evt.messageTemplate.render(evt.properties);
body.events.push({
Timestamp: evt.timestamp,
Level: levels[evt[i].level],
MessageTemplate: evt.messageTemplate.raw,
RenderedMessage: renderedMsg,
Properties: evt.properties
});
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", options.url + "/api/events/raw", true);
xmlHttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlHttp.send(JSON.stringify(body));
};
}
return function (options) {
return new SeqSink(options);
};
}));