mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
refactor: appenders/loggly.js
This commit is contained in:
parent
312f8e0a14
commit
97de2cc333
@ -1,36 +1,37 @@
|
||||
'use strict';
|
||||
var layouts = require('../layouts')
|
||||
, loggly = require('loggly')
|
||||
, os = require('os')
|
||||
, passThrough = layouts.messagePassThroughLayout;
|
||||
const layouts = require('../layouts');
|
||||
const loggly = require('loggly');
|
||||
const os = require('os');
|
||||
const passThrough = layouts.messagePassThroughLayout;
|
||||
|
||||
|
||||
function isAnyObject(value) {
|
||||
return value !== null && (typeof value === 'object' || typeof value === 'function');
|
||||
}
|
||||
|
||||
function numKeys(o) {
|
||||
var res = 0;
|
||||
for (var k in o) {
|
||||
if (o.hasOwnProperty(k)) res++;
|
||||
/* eslint no-prototype-builtins:1,no-restricted-syntax:[1, "ForInStatement"] */
|
||||
function numKeys(obj) {
|
||||
let res = 0;
|
||||
for (const key in obj) {
|
||||
if (obj.hasOwnProperty(key)) res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param msg - array of args for logging.
|
||||
* @returns { deTaggedMsg: [], additionalTags: [] }
|
||||
* @param msgListArgs
|
||||
* @returns Object{ deTaggedMsg: [...], additionalTags: [...] }
|
||||
*/
|
||||
function processTags(msgListArgs) {
|
||||
var msgList = (msgListArgs.length === 1 ? [msgListArgs[0]] : Array.apply(null, msgListArgs));
|
||||
const msgList = (msgListArgs.length === 1 ? [msgListArgs[0]] : msgListArgs);
|
||||
|
||||
return msgList.reduce(function (accum, element, currentIndex, array) {
|
||||
if (isAnyObject(element) && Array.isArray(element.tags) && numKeys(element) == 1) {
|
||||
accum.additionalTags = accum.additionalTags.concat(element.tags);
|
||||
return msgList.reduce((accumulate, element) => {
|
||||
if (isAnyObject(element) && Array.isArray(element.tags) && numKeys(element) === 1) {
|
||||
accumulate.additionalTags = accumulate.additionalTags.concat(element.tags);
|
||||
} else {
|
||||
accum.deTaggedData.push(element);
|
||||
accumulate.deTaggedData.push(element);
|
||||
}
|
||||
return accum;
|
||||
return accumulate;
|
||||
}, { deTaggedData: [], additionalTags: [] });
|
||||
}
|
||||
|
||||
@ -38,7 +39,7 @@ function processTags(msgListArgs) {
|
||||
* Loggly Appender. Sends logging events to Loggly using node-loggly, optionally adding tags.
|
||||
*
|
||||
* This appender will scan the msg from the logging event, and pull out any argument of the
|
||||
* shape `{ tags: [] }` so that it's possibleto add tags in a normal logging call.
|
||||
* shape `{ tags: [] }` so that it's possible to add tags in a normal logging call.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
@ -55,21 +56,21 @@ function processTags(msgListArgs) {
|
||||
* @param layout a function that takes a logevent and returns a string (defaults to objectLayout).
|
||||
*/
|
||||
function logglyAppender(config, layout) {
|
||||
var client = loggly.createClient(config);
|
||||
const client = loggly.createClient(config);
|
||||
if (!layout) layout = passThrough;
|
||||
|
||||
return function (loggingEvent) {
|
||||
var result = processTags(loggingEvent.data);
|
||||
var deTaggedData = result.deTaggedData;
|
||||
var additionalTags = result.additionalTags;
|
||||
return loggingEvent => {
|
||||
const result = processTags(loggingEvent.data);
|
||||
const deTaggedData = result.deTaggedData;
|
||||
const additionalTags = result.additionalTags;
|
||||
|
||||
// Replace the data property with the deTaggedData
|
||||
loggingEvent.data = deTaggedData;
|
||||
|
||||
var msg = layout(loggingEvent);
|
||||
const msg = layout(loggingEvent);
|
||||
|
||||
client.log({
|
||||
msg: msg,
|
||||
msg,
|
||||
level: loggingEvent.level.levelStr,
|
||||
category: loggingEvent.categoryName,
|
||||
hostname: os.hostname().toString(),
|
||||
@ -78,13 +79,13 @@ function logglyAppender(config, layout) {
|
||||
}
|
||||
|
||||
function configure(config) {
|
||||
var layout;
|
||||
let layout;
|
||||
if (config.layout) {
|
||||
layout = layouts.layout(config.layout.type, config.layout);
|
||||
}
|
||||
return logglyAppender(config, layout);
|
||||
}
|
||||
|
||||
exports.name = 'loggly';
|
||||
exports.appender = logglyAppender;
|
||||
exports.configure = configure;
|
||||
module.exports.name = 'loggly';
|
||||
module.exports.appender = logglyAppender;
|
||||
module.exports.configure = configure;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user