Writes an XML representation of this, and Event object to the provided Stream
, starting at the provided offset.
If this.data is undefined, or if there is an error writing to the provided Stream
, an error will be thrown.
Writes an XML representation of this, and Event object to the provided Stream
, starting at the provided offset.
If this.data is undefined, or if there is an error writing to the provided Stream
, an error will be thrown.
Name | Type | Description |
---|---|---|
stream | Object | A |
Event.prototype._writeTo = function(stream) {
if (!this.data) {
throw new Error("Events must have at least the data field set to be written to XML.");
}
var xmlEvent = ET.Element("event");
if (this.stanza) {
xmlEvent.set("stanza", this.stanza);
}
// Convert this.unbroken (a boolean) to a number (0 or 1), then to a string
xmlEvent.set("unbroken", (+this.unbroken).toString());
if (!utils.isUndefined(this.time) && this.time !== null) {
ET.SubElement(xmlEvent, "time").text = Event.formatTime(this.time).toString();
}
// If this.data is a JS object, stringify it
if (typeof this.data === "object") {
this.data = JSON.stringify(this.data);
}
var subElements = [
{tag: "source", text: this.source},
{tag: "sourcetype", text: this.sourcetype},
{tag: "index", text: this.index},
{tag: "host", text: this.host},
{tag: "data", text: this.data}
];
for (var i = 0; i < subElements.length; i++) {
var node = subElements[i];
if (node.text) {
ET.SubElement(xmlEvent, node.tag).text = node.text;
}
}
if (this.done || !utils.isUndefined(this.done)) {
ET.SubElement(xmlEvent, "done");
}
var eventString = ET.tostring(xmlEvent, {"xml_declaration": false});
// Throws an exception if there's an error writing to the stream.
stream.write(eventString);
};
module.exports = Event;
})();
Formats a time for Splunk, should be something like 1372187084.000
.
Name | Type | Description |
---|---|---|
time | Anything | The unformatted time in seconds or milliseconds, typically a String, Number, or |
The formatted time in seconds.
// When the time parameter is a string.
var stringTime = "1372187084";
var stringTimeFormatted = Event.formatTime(stringTime);
// When the time parameter is a number, no decimals.
var numericalTime = 1372187084;
var numericalTimeFormatted = Event.formatTime(numericalTime);
// When the time parameter is a number, with decimals.
var decimalTime = 1372187084.424;
var decimalTimeFormatted = Event.formatTime(decimalTime);
// When the time parameter is a Date object.
var dateObjectTime = Date.now();
var dateObjectTimeFormatted = Event.formatTime(dateObjectTime);
Event.formatTime = function(time) {
var cleanTime;
// If time is a Date object, return its value.
if (time instanceof Date) {
time = time.valueOf();
}
if (!time || time === null) {
return null;
}
// Values with decimals
if (time.toString().indexOf(".") !== -1) {
time = parseFloat(time).toFixed(3); // Clean up the extra decimals right away.
// A perfect time in milliseconds, with the decimal in the right spot.
if (time.toString().indexOf(".") >= 10) {
cleanTime = parseFloat(time.toString().substring(0,14)).toFixed(3);
}
// A time with fewer than expected digits, or with a decimal too far to the left.
else if (time.toString().length <= 13 || time.toString().indexOf(".") < 10) {
cleanTime = parseFloat(time).toFixed(3);
}
// Any other value has more digits than the expected time format, get the first 15.
else {
cleanTime = (parseFloat(time.toString().substring(0,14))/1000).toFixed(3);
}
}
// Values without decimals
else {
// A time in milliseconds, no decimal (ex: Date.now()).
if (time.toString().length === 13) {
cleanTime = (parseFloat(time)/1000).toFixed(3);
}
// A time with fewer than expected digits.
else if (time.toString().length <= 12) {
cleanTime = parseFloat(time).toFixed(3);
}
// Any other value has more digits than the expected time format, get the first 14.
else {
cleanTime = parseFloat(time.toString().substring(0, 13)/1000).toFixed(3);
}
}
return cleanTime;
};