GELF appender can add customFields to config for every message
This commit is contained in:
parent
be754f0c0e
commit
837d007de3
@ -34,7 +34,7 @@ levelMapping[levels.FATAL] = LOG_CRIT;
|
|||||||
* @param hostname - hostname of the current host (default:os hostname)
|
* @param hostname - hostname of the current host (default:os hostname)
|
||||||
* @param facility - facility to log to (default:nodejs-server)
|
* @param facility - facility to log to (default:nodejs-server)
|
||||||
*/
|
*/
|
||||||
function gelfAppender (layout, host, port, hostname, facility) {
|
function gelfAppender (layout, host, port, hostname, facility, customFields) {
|
||||||
|
|
||||||
host = host || 'localhost';
|
host = host || 'localhost';
|
||||||
port = port || 12201;
|
port = port || 12201;
|
||||||
@ -42,6 +42,9 @@ function gelfAppender (layout, host, port, hostname, facility) {
|
|||||||
facility = facility || 'nodejs-server';
|
facility = facility || 'nodejs-server';
|
||||||
layout = layout || layouts.messagePassThroughLayout;
|
layout = layout || layouts.messagePassThroughLayout;
|
||||||
|
|
||||||
|
var defaultCustomFields = customFields || {};
|
||||||
|
console.log(defaultCustomFields)
|
||||||
|
|
||||||
var client = dgram.createSocket("udp4");
|
var client = dgram.createSocket("udp4");
|
||||||
|
|
||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
@ -55,9 +58,22 @@ function gelfAppender (layout, host, port, hostname, facility) {
|
|||||||
* @param msg
|
* @param msg
|
||||||
*/
|
*/
|
||||||
function addCustomFields(loggingEvent, msg){
|
function addCustomFields(loggingEvent, msg){
|
||||||
|
|
||||||
|
/* append defaultCustomFields firsts */
|
||||||
|
var keys = Object.keys(defaultCustomFields);
|
||||||
|
for (var i in keys){
|
||||||
|
var key = keys[i];
|
||||||
|
// skip _id field for graylog2, skip keys not starts with UNDERSCORE
|
||||||
|
if (!key.match(/^_/) || key === "_id") continue;
|
||||||
|
msg[key] = defaultCustomFields[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* append custom fields per message */
|
||||||
var data = loggingEvent.data;
|
var data = loggingEvent.data;
|
||||||
if (!Array.isArray(data) || data.length === 0) return;
|
if (!Array.isArray(data) || data.length === 0) return;
|
||||||
var firstData = data[0];
|
var firstData = data[0];
|
||||||
|
|
||||||
if (!firstData['GELF']) return; // identify with GELF field defined
|
if (!firstData['GELF']) return; // identify with GELF field defined
|
||||||
var keys = Object.keys(firstData);
|
var keys = Object.keys(firstData);
|
||||||
for (var i in keys){
|
for (var i in keys){
|
||||||
@ -66,9 +82,9 @@ function gelfAppender (layout, host, port, hostname, facility) {
|
|||||||
if (!key.match(/^_/) || key === "_id") continue;
|
if (!key.match(/^_/) || key === "_id") continue;
|
||||||
msg[key] = firstData[key];
|
msg[key] = firstData[key];
|
||||||
}
|
}
|
||||||
loggingEvent.data.shift(); // remove the first object
|
|
||||||
//console.log('=== msg ===');
|
/* the custom field object should be removed, so it will not be looged by the later appenders */
|
||||||
//console.log(msg);
|
loggingEvent.data.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
function preparePacket(loggingEvent) {
|
function preparePacket(loggingEvent) {
|
||||||
@ -112,7 +128,7 @@ function configure(config) {
|
|||||||
if (config.layout) {
|
if (config.layout) {
|
||||||
layout = layouts.layout(config.layout.type, config.layout);
|
layout = layouts.layout(config.layout.type, config.layout);
|
||||||
}
|
}
|
||||||
return gelfAppender(layout, config.host, config.port, config.hostname, config.facility);
|
return gelfAppender(layout, config.host, config.port, config.hostname, config.facility, config.customFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.appender = gelfAppender;
|
exports.appender = gelfAppender;
|
||||||
|
Loading…
Reference in New Issue
Block a user