mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
refactor: appenders/logstashUDP.js
This commit is contained in:
parent
97de2cc333
commit
5dbe639d80
@ -1,68 +1,72 @@
|
||||
"use strict";
|
||||
var layouts = require('../layouts')
|
||||
, dgram = require('dgram')
|
||||
, util = require('util');
|
||||
'use strict';
|
||||
const layouts = require('../layouts');
|
||||
const dgram = require('dgram');
|
||||
const util = require('util');
|
||||
|
||||
function logstashUDP(config, layout) {
|
||||
var udp = dgram.createSocket('udp4');
|
||||
var type = config.logType ? config.logType : config.category;
|
||||
const udp = dgram.createSocket('udp4');
|
||||
const type = config.logType ? config.logType : config.category;
|
||||
layout = layout || layouts.dummyLayout;
|
||||
|
||||
if (!config.fields) {
|
||||
config.fields = {};
|
||||
}
|
||||
return function log(loggingEvent) {
|
||||
|
||||
return function log(loggingEvent) {
|
||||
/*
|
||||
https://gist.github.com/jordansissel/2996677
|
||||
{
|
||||
"message" => "hello world",
|
||||
"@version" => "1",
|
||||
"@timestamp" => "2014-04-22T23:03:14.111Z",
|
||||
"type" => "stdin",
|
||||
"host" => "hello.local"
|
||||
'message' => 'hello world',
|
||||
'@version' => '1',
|
||||
'@timestamp' => '2014-04-22T23:03:14.111Z',
|
||||
'type' => 'stdin',
|
||||
'host' => 'hello.local'
|
||||
}
|
||||
@timestamp is the ISO8601 high-precision timestamp for the event.
|
||||
@version is the version number of this json schema
|
||||
Every other field is valid and fine.
|
||||
*/
|
||||
|
||||
/* eslint no-prototype-builtins:1,no-restricted-syntax:[1, "ForInStatement"] */
|
||||
if (loggingEvent.data.length > 1) {
|
||||
var secondEvData = loggingEvent.data[1];
|
||||
for (var k in secondEvData) {
|
||||
config.fields[k] = secondEvData[k];
|
||||
const secondEvData = loggingEvent.data[1];
|
||||
for (const key in secondEvData) {
|
||||
if (secondEvData.hasOwnProperty(key)) {
|
||||
config.fields[key] = secondEvData[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
config.fields.level = loggingEvent.level.levelStr;
|
||||
|
||||
var logObject = {
|
||||
"@version": "1",
|
||||
"@timestamp": (new Date(loggingEvent.startTime)).toISOString(),
|
||||
"type": config.logType ? config.logType : config.category,
|
||||
"message": layout(loggingEvent),
|
||||
"fields": config.fields
|
||||
const logObject = {
|
||||
'@version': '1',
|
||||
'@timestamp': (new Date(loggingEvent.startTime)).toISOString(),
|
||||
type: type,
|
||||
message: layout(loggingEvent),
|
||||
fields: config.fields
|
||||
};
|
||||
sendLog(udp, config.host, config.port, logObject);
|
||||
};
|
||||
}
|
||||
|
||||
function sendLog(udp, host, port, logObject) {
|
||||
var buffer = new Buffer(JSON.stringify(logObject));
|
||||
udp.send(buffer, 0, buffer.length, port, host, function (err, bytes) {
|
||||
const buffer = new Buffer(JSON.stringify(logObject));
|
||||
|
||||
/* eslint no-unused-vars:0 */
|
||||
udp.send(buffer, 0, buffer.length, port, host, (err, bytes) => {
|
||||
if (err) {
|
||||
console.error(
|
||||
"log4js.logstashUDP - %s:%p Error: %s", host, port, util.inspect(err)
|
||||
);
|
||||
console.error('log4js.logstashUDP - %s:%p Error: %s', host, port, util.inspect(err));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function configure(config) {
|
||||
var layout;
|
||||
let layout;
|
||||
if (config.layout) {
|
||||
layout = layouts.layout(config.layout.type, config.layout);
|
||||
}
|
||||
|
||||
return logstashUDP(config, layout);
|
||||
}
|
||||
|
||||
exports.appender = logstashUDP;
|
||||
exports.configure = configure;
|
||||
module.exports.appender = logstashUDP;
|
||||
module.exports.configure = configure;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user