@beforeyoubid/logger-adapter
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -7,2 +7,3 @@ "use strict"; | ||
const util_1 = require("../util"); | ||
const flush_1 = require("../flush"); | ||
let loggerObject; | ||
@@ -28,4 +29,4 @@ const logParams = params_1.getLogParams(); | ||
init, | ||
flushAll: util_1.flushAll, | ||
flushAll: flush_1.flushAll, | ||
}; | ||
exports.consoleLogger = consoleLogger; |
import { consoleLogger } from './console'; | ||
import { logger } from './logger'; | ||
import { getLogParams } from './params'; | ||
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './util'; | ||
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './flush'; | ||
export { consoleLogger, logger, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback }; |
@@ -10,5 +10,5 @@ "use strict"; | ||
Object.defineProperty(exports, "getLogParams", { enumerable: true, get: function () { return params_1.getLogParams; } }); | ||
const util_1 = require("./util"); | ||
Object.defineProperty(exports, "flushAll", { enumerable: true, get: function () { return util_1.flushAll; } }); | ||
Object.defineProperty(exports, "ensureFlushAll", { enumerable: true, get: function () { return util_1.ensureFlushAll; } }); | ||
Object.defineProperty(exports, "ensureFlushAllCallback", { enumerable: true, get: function () { return util_1.ensureFlushAllCallback; } }); | ||
const flush_1 = require("./flush"); | ||
Object.defineProperty(exports, "flushAll", { enumerable: true, get: function () { return flush_1.flushAll; } }); | ||
Object.defineProperty(exports, "ensureFlushAll", { enumerable: true, get: function () { return flush_1.ensureFlushAll; } }); | ||
Object.defineProperty(exports, "ensureFlushAllCallback", { enumerable: true, get: function () { return flush_1.ensureFlushAllCallback; } }); |
@@ -1,2 +0,1 @@ | ||
import { LambdaHandlerWithAsyncFunction, LambdaHandlerWithCallbackFunction } from './types'; | ||
/** | ||
@@ -7,22 +6,2 @@ * Handy function to check if we are sending to LogDNA | ||
*/ | ||
declare const isLogDNAEnabled: (logDNAKey: string, sendToRemote: boolean) => boolean; | ||
/** | ||
* Send all log messages to LogDNA before lambda is terminated | ||
*/ | ||
declare const flushAll: () => Promise<void>; | ||
/** | ||
* Lambda wrapper to ensure we flush messages before returning the response | ||
* @param lambdaHandler | ||
*/ | ||
declare const ensureFlushAll: (lambdaHandler: LambdaHandlerWithAsyncFunction) => LambdaHandlerWithAsyncFunction; | ||
/** | ||
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated | ||
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed. | ||
* This means after a callback function is called, a Lambda function still run | ||
* and we can catch the final clean up. | ||
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html | ||
* | ||
* @param lambdaHandler | ||
*/ | ||
declare const ensureFlushAllCallback: (lambdaHandler: LambdaHandlerWithCallbackFunction) => LambdaHandlerWithCallbackFunction; | ||
export { isLogDNAEnabled, flushAll, ensureFlushAll, ensureFlushAllCallback }; | ||
export declare const isLogDNAEnabled: (logDNAKey: string, sendToRemote: boolean) => boolean; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ensureFlushAllCallback = exports.ensureFlushAll = exports.flushAll = exports.isLogDNAEnabled = void 0; | ||
const LogDNALogger = require('@logdna/logger'); | ||
const debug = require('util').debuglog('logger-adapter'); | ||
const { once } = require('events'); | ||
const { FALLBACK_TIMEOUT_AFTER_FLUSHING } = require('./consts'); | ||
const { getLogParams } = require('./params'); | ||
// timeout | ||
let timeout; | ||
exports.isLogDNAEnabled = void 0; | ||
/** | ||
@@ -25,76 +9,2 @@ * Handy function to check if we are sending to LogDNA | ||
*/ | ||
const isLogDNAEnabled = (logDNAKey, sendToRemote) => !!logDNAKey && sendToRemote; | ||
exports.isLogDNAEnabled = isLogDNAEnabled; | ||
/** | ||
* REturn if we need to suppress flushAll on a local machine | ||
*/ | ||
const suppressFlushAll = () => { var _a; return ((_a = getLogParams()) === null || _a === void 0 ? void 0 : _a.logDNASuppressFlushAll) || false; }; | ||
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)); | ||
/** | ||
* Send all log messages to LogDNA before lambda is terminated | ||
*/ | ||
const flushAll = () => __awaiter(void 0, void 0, void 0, function* () { | ||
debug('Flushing all logs...'); | ||
LogDNALogger.flush(); | ||
yield once(LogDNALogger, 'cleared'); | ||
// Everything clear. Did Lambda buffer anything? | ||
yield sleep(1000); | ||
LogDNALogger.flush(); | ||
yield once(LogDNALogger, 'cleared'); | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
debug('Completed flushing all log messages'); | ||
}); | ||
exports.flushAll = flushAll; | ||
/** | ||
* Fallback promise just in | ||
*/ | ||
const timeoutPromise = () => { | ||
return new Promise(resolve => { | ||
timeout = setTimeout(() => { | ||
debug('Flushing timeout has reached. Ignore flushing messages...'); | ||
resolve(); | ||
}, FALLBACK_TIMEOUT_AFTER_FLUSHING); | ||
}); | ||
}; | ||
/** | ||
* Lambda wrapper to ensure we flush messages before returning the response | ||
* @param lambdaHandler | ||
*/ | ||
const ensureFlushAll = (lambdaHandler) => { | ||
return (event, context) => __awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
const result = yield lambdaHandler(event, context); | ||
if (!suppressFlushAll()) | ||
yield Promise.race([flushAll(), timeoutPromise()]); | ||
return result; | ||
} | ||
catch (e) { | ||
if (!suppressFlushAll()) | ||
yield Promise.race([flushAll(), timeoutPromise()]); | ||
throw e; | ||
} | ||
}); | ||
}; | ||
exports.ensureFlushAll = ensureFlushAll; | ||
/** | ||
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated | ||
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed. | ||
* This means after a callback function is called, a Lambda function still run | ||
* and we can catch the final clean up. | ||
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html | ||
* | ||
* @param lambdaHandler | ||
*/ | ||
const ensureFlushAllCallback = (lambdaHandler) => { | ||
return (event, context, callback) => { | ||
const thisCallback = (error, result) => { | ||
callback(error, result); | ||
if (!suppressFlushAll()) | ||
flushAll(); | ||
}; | ||
lambdaHandler(event, context, thisCallback); | ||
}; | ||
}; | ||
exports.ensureFlushAllCallback = ensureFlushAllCallback; | ||
exports.isLogDNAEnabled = (logDNAKey, sendToRemote) => !!logDNAKey && sendToRemote; |
{ | ||
"name": "@beforeyoubid/logger-adapter", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "A platform logger module to send the log messages to LogDNA.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,3 +5,4 @@ import { ILogDNAParams } from '../types'; | ||
import { getLogger } from './logger'; | ||
import { isLogDNAEnabled, flushAll } from '../util'; | ||
import { isLogDNAEnabled } from '../util'; | ||
import { flushAll } from '../flush'; | ||
@@ -8,0 +9,0 @@ let loggerObject: any; |
import { consoleLogger } from './console'; | ||
import { logger } from './logger'; | ||
import { getLogParams } from './params'; | ||
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './util'; | ||
import { flushAll, ensureFlushAll, ensureFlushAllCallback } from './flush'; | ||
export { consoleLogger, logger, getLogParams, flushAll, ensureFlushAll, ensureFlushAllCallback }; |
@@ -1,12 +0,1 @@ | ||
import { LambdaHandlerWithAsyncFunction, LambdaHandlerWithCallbackFunction, CallbackFunction } from './types'; | ||
const LogDNALogger = require('@logdna/logger'); | ||
const debug = require('util').debuglog('logger-adapter'); | ||
const { once } = require('events') | ||
const { FALLBACK_TIMEOUT_AFTER_FLUSHING } = require('./consts'); | ||
const { getLogParams } = require('./params'); | ||
// timeout | ||
let timeout: NodeJS.Timeout; | ||
/** | ||
@@ -17,78 +6,2 @@ * Handy function to check if we are sending to LogDNA | ||
*/ | ||
const isLogDNAEnabled = (logDNAKey: string, sendToRemote: boolean): boolean => !!logDNAKey && sendToRemote; | ||
/** | ||
* REturn if we need to suppress flushAll on a local machine | ||
*/ | ||
const suppressFlushAll = (): boolean => getLogParams()?.logDNASuppressFlushAll || false; | ||
const sleep = (time: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, time)); | ||
/** | ||
* Send all log messages to LogDNA before lambda is terminated | ||
*/ | ||
const flushAll = async (): Promise<void> => { | ||
debug('Flushing all logs...'); | ||
LogDNALogger.flush() | ||
await once(LogDNALogger, 'cleared') | ||
// Everything clear. Did Lambda buffer anything? | ||
await sleep(1000); | ||
LogDNALogger.flush() | ||
await once(LogDNALogger, 'cleared') | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
debug('Completed flushing all log messages'); | ||
}; | ||
/** | ||
* Fallback promise just in | ||
*/ | ||
const timeoutPromise = () => { | ||
return new Promise(resolve => { | ||
timeout = setTimeout(() => { | ||
debug('Flushing timeout has reached. Ignore flushing messages...'); | ||
resolve(); | ||
}, FALLBACK_TIMEOUT_AFTER_FLUSHING); | ||
}); | ||
}; | ||
/** | ||
* Lambda wrapper to ensure we flush messages before returning the response | ||
* @param lambdaHandler | ||
*/ | ||
const ensureFlushAll = (lambdaHandler: LambdaHandlerWithAsyncFunction): LambdaHandlerWithAsyncFunction => { | ||
return async (event: any, context: any): Promise<any> => { | ||
try { | ||
const result = await lambdaHandler(event, context); | ||
if (!suppressFlushAll()) await Promise.race([flushAll(), timeoutPromise()]); | ||
return result; | ||
} catch (e) { | ||
if (!suppressFlushAll()) await Promise.race([flushAll(), timeoutPromise()]); | ||
throw e; | ||
} | ||
}; | ||
}; | ||
/** | ||
* A Lambda wrapper to ensure we flush messages before a Lambda function get terminated | ||
* NOTE: For non-async handlers, AWS Lambda will wait until the event loop is completed. | ||
* This means after a callback function is called, a Lambda function still run | ||
* and we can catch the final clean up. | ||
* REF: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html | ||
* | ||
* @param lambdaHandler | ||
*/ | ||
const ensureFlushAllCallback = ( | ||
lambdaHandler: LambdaHandlerWithCallbackFunction | ||
): LambdaHandlerWithCallbackFunction => { | ||
return (event: any, context: any, callback: CallbackFunction): void => { | ||
const thisCallback = (error: any, result: any): void => { | ||
callback(error, result); | ||
if (!suppressFlushAll()) flushAll(); | ||
}; | ||
lambdaHandler(event, context, thisCallback); | ||
}; | ||
}; | ||
export { isLogDNAEnabled, flushAll, ensureFlushAll, ensureFlushAllCallback }; | ||
export const isLogDNAEnabled = (logDNAKey: string, sendToRemote: boolean): boolean => !!logDNAKey && sendToRemote; |
32835
32
633